From eeab650b2fc6322d9b517812e6d6bc753c59bcc3 Mon Sep 17 00:00:00 2001 From: Moritz Schott Date: Tue, 3 Sep 2024 21:28:53 +0200 Subject: [PATCH] move try except to separate functions --- ohsome/clients.py | 94 ++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/ohsome/clients.py b/ohsome/clients.py index 0ff62e6..81be036 100644 --- a/ohsome/clients.py +++ b/ohsome/clients.py @@ -13,7 +13,7 @@ import pandas as pd import requests import shapely -from requests import Session +from requests import Session, Response from requests.adapters import HTTPAdapter from requests.exceptions import RetryError, JSONDecodeError from urllib3 import Retry @@ -332,21 +332,55 @@ def _handle_request(self) -> OhsomeResponse: Handles request to ohsome API :return: """ - ohsome_exception = None - response = None + try: + response = self._post_request() + self._check_response(response) + data = self._get_response_data(response) + except OhsomeException as ohsome_exception: + if self.log: + ohsome_exception.log(self.log_dir) + raise ohsome_exception + + return OhsomeResponse(data=data, url=self._url) + + def _post_request(self) -> Response: try: response = self._session().post(url=self._url, data=self._parameters) - response.raise_for_status() - response.json() + except requests.exceptions.ConnectionError as e: + raise OhsomeException( + message="Connection Error: Query could not be sent. Make sure there are no network " + f"problems and that the ohsome API URL {self._url} is valid.", + url=self._url, + params=self._parameters, + response=e.response, + ) + except requests.exceptions.RequestException as e: + if isinstance(e, RetryError): + # retry one last time without retries, this will raise the original error instead of a cryptic retry + # error (or succeed) + self._OhsomeBaseClient__session = None + self._OhsomeBaseClient__retry = False + self._handle_request() + + raise OhsomeException( + message=str(e), + url=self._url, + params=self._parameters, + response=e.response, + ) + return response + def _check_response(self, response: Response) -> None: + try: + response.raise_for_status() except requests.exceptions.HTTPError as e: try: error_message = e.response.json()["message"] except json.decoder.JSONDecodeError: error_message = f"Invalid URL: Is {self._url} valid?" - ohsome_exception = OhsomeException( + raise OhsomeException( message=error_message, url=self._url, params=self._parameters, @@ -354,15 +388,17 @@ def _handle_request(self) -> OhsomeResponse: response=e.response, ) - except requests.exceptions.ConnectionError as e: - ohsome_exception = OhsomeException( - message="Connection Error: Query could not be sent. Make sure there are no network " - f"problems and that the ohsome API URL {self._url} is valid.", + except KeyboardInterrupt: + raise OhsomeException( + message="Keyboard Interrupt: Query was interrupted by the user.", url=self._url, params=self._parameters, - response=e.response, + error_code=440, ) + def _get_response_data(self, response: Response) -> dict: + try: + return response.json() except (ValueError, JSONDecodeError) as e: if response: error_code, message = extract_error_message_from_invalid_json( @@ -371,53 +407,21 @@ def _handle_request(self) -> OhsomeResponse: else: message = str(e) error_code = None - ohsome_exception = OhsomeException( + raise OhsomeException( message=message, url=self._url, error_code=error_code, params=self._parameters, response=response, ) - - except requests.exceptions.RequestException as e: - if isinstance(e, RetryError): - # retry one last time without retries, this will raise the original error instead of a cryptic retry - # error (or succeed) - self._OhsomeBaseClient__session = None - self._OhsomeBaseClient__retry = False - self._handle_request() - - ohsome_exception = OhsomeException( - message=str(e), - url=self._url, - params=self._parameters, - response=e.response, - ) - - except KeyboardInterrupt: - ohsome_exception = OhsomeException( - message="Keyboard Interrupt: Query was interrupted by the user.", - url=self._url, - params=self._parameters, - error_code=440, - ) - except AttributeError: - ohsome_exception = OhsomeException( + raise OhsomeException( message=f"Seems like {self._url} is not a valid endpoint.", url=self._url, error_code=404, params=self._parameters, ) - # If there has been an error and logging is enabled, write it to file - if ohsome_exception: - if self.log: - ohsome_exception.log(self.log_dir) - raise ohsome_exception - - return OhsomeResponse(data=data, url=self._url) - def _format_parameters(self, params): """ Check and format parameters of the query