async def async_fetch(self, station: str = None, lat: float = None, lon: float = None) -> str: """ Asynchronously fetch a report string from the service """ if station: valid_station(station) elif lat is None or lon is None: raise ValueError("No valid fetch parameters") url, params = self._make_url(station, lat, lon) try: async with aiohttp.ClientSession(timeout=_atimeout) as sess: async with getattr(sess, self.method.lower())(url, params=params) as resp: if resp.status != 200: raise SourceError( f"{self.__class__.__name__} server returned {resp.status}" ) text = await resp.text() except aiohttp.ClientConnectionError: raise ConnectionError( f"Unable to connect to {self.__class__.__name__} server") report = self._extract(text, station) # This split join replaces all *whitespace elements with a single space if isinstance(report, list): return dedupe(" ".join(r.split()) for r in report) return " ".join(report.split())
async def _fetch(self, station: str, url: str, params: dict, timeout: int) -> str: try: async with httpx.AsyncClient(timeout=timeout) as client: if self.method.lower() == "post": resp = await client.post(url, params=params, data=self._post_data(station)) else: resp = await client.get(url, params=params) if resp.status_code != 200: raise SourceError( f"{self.__class__.__name__} server returned {resp.status_code}" ) except ( httpx.ConnectTimeout, httpx.ReadTimeout, httpcore.ReadTimeout, ) as timeout_error: raise TimeoutError(f"Timeout from {self.__class__.__name__} server" ) from timeout_error except (gaierror, httpcore.ConnectError, httpx.ConnectError) as connect_error: raise ConnectionError( f"Unable to connect to {self.__class__.__name__} server" ) from connect_error except httpcore.NetworkError as network_error: raise ConnectionError( f"Unable to read data from {self.__class__.__name__} server" ) from network_error report = self._extract(resp.text, station) return self._clean_report(report)
def fetch(self, station: str = None, lat: float = None, lon: float = None) -> str: """ Fetches a report string from the service """ if station: valid_station(station) elif lat is None or lon is None: raise ValueError("No valid fetch parameters") try: url, params = self._make_url(station, lat, lon) url += "?" + urlencode(params) # Non-null data signals a POST request data = {} if self.method == "POST" else None resp = request.urlopen(url, data=data, timeout=10) if resp.status != 200: raise SourceError( f"{self.__class__.__name__} server returned {resp.status}") except URLError: raise ConnectionError( f"Unable to connect to {self.__class__.__name__} server") report = self._extract(resp.read().decode("utf-8"), station) # This split join replaces all *whitespace elements with a single space if isinstance(report, list): return dedupe(" ".join(r.split()) for r in report) return " ".join(report.split())
def fetch( self, station: str = None, lat: float = None, lon: float = None, timeout: int = 10, ) -> str: """ Fetches a report string from the service """ if station: valid_station(station) elif lat is None or lon is None: raise ValueError("No valid fetch parameters") try: url, params = self._make_url(station, lat, lon) if self.method.lower() == "post": resp = requests.post(url, params=params, data=self._post_data(station)) else: resp = requests.get(url, params) if resp.status_code != 200: raise SourceError( f"{self.__class__.__name__} server returned {resp.status_code}" ) except requests.exceptions.ConnectionError: raise ConnectionError( f"Unable to connect to {self.__class__.__name__} server") report = self._extract(resp.text, station) # This split join replaces all *whitespace elements with a single space if isinstance(report, list): return dedupe(" ".join(r.split()) for r in report) return " ".join(report.split())
def fetch( self, station: str = None, lat: float = None, lon: float = None, timeout: int = 10, ) -> str: """ Fetches a report string from the service """ if station: valid_station(station) elif lat is None or lon is None: raise ValueError("No valid fetch parameters") try: url, params = self._make_url(station, lat, lon) if self.method.lower() == "post": resp = httpx.post(url, params=params, data=self._post_data(station), timeout=timeout) else: resp = httpx.get(url, params=params, timeout=timeout) if resp.status_code != 200: raise SourceError( f"{self.__class__.__name__} server returned {resp.status_code}" ) except (httpx.ConnectTimeout, httpx.ReadTimeout): raise TimeoutError( f"Timeout from {self.__class__.__name__} server") except gaierror: raise ConnectionError( f"Unable to connect to {self.__class__.__name__} server") report = self._extract(resp.text, station) return self._clean_report(report)
def fetch(self, station: str) -> str: """ Fetches a report string from the service """ valid_station(station) try: resp = getattr(requests, self.method.lower())(self.url.format(self.rtype, station)) if resp.status_code != 200: raise SourceError(f'{self.__class__.__name__} server returned {resp.status_code}') except requests.exceptions.ConnectionError: raise ConnectionError(f'Unable to connect to {self.__class__.__name__} server') report = self._extract(resp.text, station) # This split join replaces all *whitespace elements with a single space return ' '.join(report.split())
def fetch(self, station: str) -> str: """ Fetches a report string from the service """ valid_station(station) try: resp = requests.get(self.url.format(self.rtype, station)) if resp.status_code != 200: raise SourceError( f'{self.__class__.__name__} server returned {resp.status_code}' ) except requests.exceptions.ConnectionError: raise ConnectionError( f'Unable to connect to {self.__class__.__name__} server') return self._extract(resp.text)
async def async_fetch( self, station: str = None, lat: float = None, lon: float = None, timeout: int = 10, ) -> str: """ Asynchronously fetch a report string from the service """ if station: valid_station(station) elif lat is None or lon is None: raise ValueError("No valid fetch parameters") url, params = self._make_url(station, lat, lon) try: async with httpx.AsyncClient(timeout=timeout) as client: if self.method.lower() == "post": resp = await client.post(url, params=params, data=self._post_data(station)) else: resp = await client.get(url, params=params) if resp.status_code != 200: raise SourceError( f"{self.__class__.__name__} server returned {resp.status_code}" ) except (ConnectTimeout, ReadTimeout): raise TimeoutError( f"Timeout from {self.__class__.__name__} server") except gaierror: raise ConnectionError( f"Unable to connect to {self.__class__.__name__} server") report = self._extract(resp.text, station) # This split join replaces all *whitespace elements with a single space if isinstance(report, list): return dedupe(" ".join(r.split()) for r in report) return " ".join(report.split())
async def _call( self, url: str, params: dict = None, headers: dict = None, data: Any = None, timeout: int = 10, ) -> str: name = self.__class__.__name__ try: async with httpx.AsyncClient(timeout=timeout) as client: if self.method.lower() == "post": resp = await client.post(url, params=params, headers=headers, data=data) else: resp = await client.get(url, params=params, headers=headers) if resp.status_code != 200: raise SourceError( f"{name} server returned {resp.status_code}") except ( httpx.ConnectTimeout, httpx.ReadTimeout, httpcore.ReadTimeout, ) as timeout_error: raise TimeoutError( f"Timeout from {name} server") from timeout_error except (gaierror, httpcore.ConnectError, httpx.ConnectError) as connect_error: raise ConnectionError( f"Unable to connect to {name} server") from connect_error except httpcore.NetworkError as network_error: raise ConnectionError( f"Unable to read data from {name} server") from network_error return resp.text