"""Common ows-sound-recordings connector.""" from owsrequest import request as requests from requests.exceptions import ConnectionError as RequestConnectionError from requests.exceptions import HTTPError from src.common.exceptions import exceptions SERVICE_NAME = 'ows-sound-recordings' def call(verb, path, app_name, environment, extra_args={}, retry_codes=[], headers=None): """Make base API request. Args: verb (str): HTTP method path (str): URI path app_name (str): client making request to ows environment (str): ows app running in environment extra_args (dict): kwargs for requests library (json, params, etc) retry_codes (list): HTTP codes that raise RetryableException Returns: requests.Response Raises: RetryableException: non-200 HTTP code returned, can retry HTTPError: non-200 HTTP code returned, not explictly retryable """ try: result = requests.process( application=app_name, environment=environment, method=verb.upper(), service_name=SERVICE_NAME, path=path, headers=headers, **extra_args ) result.raise_for_status() return result except RequestConnectionError as c_e: raise exceptions.RetryableException(f'Failed request: {c_e}') except HTTPError as e: if e.response.status_code in retry_codes: message_error = f'Unexpected response code from {SERVICE_NAME} HTTP:{e.response.status_code}' # noqa:E501 raise exceptions.RetryableException(message_error) else: raise e