def validate_response(response): error_suffix = " response={!r}".format(response) if response.status_code in (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN): raise AuthError("operation=auth_error," + error_suffix, response) if response.status_code == status.HTTP_404_NOT_FOUND: raise NotFoundError("operation=not_found_error," + error_suffix, response) if status.is_client_error(code=response.status_code): raise ClientError("operation=client_error," + error_suffix, response) if status.is_server_error(code=response.status_code): raise ServerError("operation=server_error," + error_suffix, response)
def validate_status_code(status_code, content): exception_class = None if status_code in (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN): exception_class = AuthError(status_code, {'errorMessage': 'Authentication Error'}) elif status_code == status.HTTP_404_NOT_FOUND: exception_class = NotFound(status_code, {'errorMessage': 'Endpoint Not Found'}) elif status_code in range(status.HTTP_400_BAD_REQUEST, status.HTTP_417_EXPECTATION_FAILED + 1): exception_class = OandaError(status_code, content) elif status.is_server_error(status_code): exception_class = ServerError(status_code) if exception_class: return False, exception_class return True, exception_class
def _make_request(self, method, url, **kwargs): request_method = getattr(self.session, method.lower()) try: response = request_method(url, timeout=self.timeout, **kwargs) except (ConnectionError, Timeout) as exc: raise ClientConnectionError(exc) if status.is_client_error(code=response.status_code): raise ClientError(response) if status.is_server_error(code=response.status_code): raise ServerError(response) return response
def test_is_server_error(self): self.assertTrue(status.is_server_error(500))
def raise_for_status(res: HTTPResponse) -> None: if status.is_client_error(res.status) or status.is_server_error( res.status): raise HTTPError(f"{res.status}:{res.reason}")
def validate_response(response): error_suffix = ' response={!r}'.format(response) if status.is_client_error(code=response.status_code): raise ClientError('operation=client_error,' + error_suffix, response) if status.is_server_error(code=response.status_code): raise ServerError('operation=server_error,' + error_suffix, response)