def check_api_error(api_response): #print(api_response) """Check if returned API response contains an error.""" if type(api_response) == dict and 'code' in api_response and api_response[ 'code'] <> 200: print("Server response code: %s" % api_response['code']) print("Server response: %s" % api_response) raise exceptions.HTTPError('Unexpected response', response=api_response) if type(api_response) == dict and (api_response.get('status') == 'failed'): if 'ProgrammingError' in api_response.get('exception_cls'): raise DatabaseError(message='PyBossa database error.', error=api_response) if ('DBIntegrityError' in api_response.get('exception_cls') and 'project' in api_response.get('target')): msg = 'PyBossa project already exists.' raise ProjectAlreadyExists(message=msg, error=api_response) if 'project' in api_response.get('target'): raise ProjectNotFound(message='PyBossa Project not found', error=api_response) if 'task' in api_response.get('target'): raise TaskNotFound(message='PyBossa Task not found', error=api_response) else: print("Server response: %s" % api_response) raise exceptions.HTTPError('Unexpected response', response=api_response)
def handle_response_content(self, resp): if self.is_auth_response(resp): raise exceptions.HTTPError("Authentication required") content = resp.json() if not resp.ok: raise exceptions.HTTPError(content['Errors'][0]) return LookbackResponse(content)
def setUp(self): super(TestHangupExceptionErrorHandler, self).setUp() self.handler = _HangupExceptionHandler(self.connection, s.user_uuid) self.exception_msg_map = { exceptions.HTTPError(response=Mock(status_code=401)): 'hangup_unauthorized', exceptions.ConnectionError(): 'service_unavailable', exceptions.HTTPError(response=Mock(status_code=503)): 'service_unavailable', }
def setUp(self): super(TestTransferExceptionErrorHandler, self).setUp() self.handler = _TransferExceptionHandler(self.connection, s.user_id, '1234') self.exception_msg_map = { exceptions.HTTPError(response=Mock(status_code=401)): 'transfer_unauthorized', exceptions.ConnectionError(): 'service_unavailable', exceptions.HTTPError(response=Mock(status_code=503)): 'service_unavailable', exceptions.HTTPError(response=Mock(status_code=400)): 'unreachable_extension:1234' }
def test_retrying_calls(self, get, post): get.side_effect = exceptions.HTTPError("Connection error: get") post.side_effect = exceptions.HTTPError("Connection error: post") pulp = Pulp(self.server_url, username=self.username, password=self.password) with self.assertRaises(exceptions.HTTPError): pulp.get_docker_repository_name("test") self.assertGreater(get.call_count, 1) with self.assertRaises(exceptions.HTTPError): pulp.get_content_set_by_repo_ids(['test1', 'test2']) self.assertGreater(post.call_count, 1)
def _DoHttpRequestAsync(self, sink_stack, deadline, stream, msg): if deadline: timeout = deadline - time.time() if timeout < 0: sink_stack.AsyncProcessResponseMessage( MethodReturnMessage(error=TimeoutError())) else: timeout = None try: response = self._MakeRequest(msg, stream, timeout) if self._raise_on_http_error and 400 <= response.status_code < 600: err_text = 'HTTP Error %d: %s.' % (response.status_code, response.reason) if response.text: err_text += '\nThe server returned:\n%s' % response.text err = exceptions.HTTPError(err_text, response=response) msg = MethodReturnMessage(error=err) else: self._ProcessResponse(response, sink_stack) return except exceptions.Timeout: msg = MethodReturnMessage(error=TimeoutError()) except Exception as ex: msg = MethodReturnMessage(error=ex) sink_stack.AsyncProcessResponseMessage(msg)
def test_post_notification_HTTPError(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.HTTPError( 'hello') notification_status, notification_error = post_notification( None, None, None) self.assertEqual(notification_status, Constants.FAILURE) self.assertEqual(notification_error, 'hello')
def push_updated_domain_records(domain, updated_records): global GANDI_API_TOKEN, DRY_RUN try: uri = "https://dns.api.gandi.net/api/v5/domains/{}/records".format( domain) headers = { 'X-Api-Key': GANDI_API_TOKEN, 'Content-Type': 'application/json' } data = {'items': updated_records} if not DRY_RUN: r = put(uri, headers=headers, data=json.dumps(data)) r.raise_for_status() logger.info("Records updated for domain {}".format(domain)) else: logger.info("URI: {}".format(uri)) logger.info("headers: {}".format(headers)) logger.info("data: {}".format(data)) except exceptions.HTTPError: raise exceptions.HTTPError("HTTP error: {}: {}".format( r.status_code, r.json())) except exceptions.RequestException as e: raise Exception( "Failed to push the updated records for domain {}: {}".format( domain, e)) except Exception as e: raise Exception( "Unhandled exception while trying to push the updated records for domain {}: {}" .format(domain, e))
def execute(self, http_method, path, params=None, data=None, headers=None, allow_redirects=False, urlencode=True, files=None, stream=False, clear_cookies=False, timeout=conf.REST_CONN_TIMEOUT.get()): """ Submit an HTTP request. @param http_method: GET, POST, PUT, DELETE @param path: The path of the resource. Unsafe characters will be quoted. @param params: Key-value parameter data. @param data: The data to attach to the body of the request. @param headers: The headers to set for this request. @param allow_redirects: requests should automatically resolve redirects. @param urlencode: percent encode paths. @param files: for posting Multipart-Encoded files @param stream: Bool to stream the response @param clear_cookies: flag to force clear any cookies set in the current session @return: The result of urllib2.urlopen() """ if urlencode: path = urllib_quote(smart_str(path)) url = self._make_url(path, params) if http_method in ("GET", "DELETE"): if data is not None: self.logger.warn("GET and DELETE methods do not pass any data. Path '%s'" % path) data = None if headers and 'timeout' in headers: timeout = int(headers['timeout']) LOG.debug("Overriding timeout xxx via header value %d" % timeout) request_kwargs = {'allow_redirects': allow_redirects, 'timeout': timeout} if headers: request_kwargs['headers'] = headers if data: request_kwargs['data'] = data if files: request_kwargs['files'] = files if stream: request_kwargs['stream'] = True if self._cookies and not clear_cookies: request_kwargs['cookies'] = self._cookies if clear_cookies: self._session.cookies.clear() try: resp = getattr(self._session, http_method.lower())(url, **request_kwargs) if resp.status_code >= 300: resp.raise_for_status() raise exceptions.HTTPError(response=resp) self._cookies = resp.cookies # Cache request cookie for the next http_client call. return resp except (exceptions.ConnectionError, exceptions.HTTPError, exceptions.RequestException, exceptions.URLRequired, exceptions.TooManyRedirects) as ex: raise self._exc_class(ex)
def __wait_for_connection(self, retries: int = 200, delay: int = 0.1) -> None: """ __wait_for_connection [summary]. Args: retries (int, optional): [description]. Defaults to 20. delay (int, optional): [description]. Defaults to 0.1. Raises: ReqException.HTTPError: [description] """ while (retries > 0): try: sleep(delay) response = get(self.url) response.raise_for_status() if response.status_code == 200: response_json = response.json() if 'info' in response_json: if response_json['info'] != "system rebooting": return else: return else: raise ReqException.HTTPError(response.status_code) except (Exception, ReqException.ConnectionError, ReqException.Timeout, ReqException.RequestException, ReqException.BaseHTTPError, ReqException.ConnectTimeout, ReqException.RetryError) as e: pass finally: retries -= retries
def test_should_return_bad_request_when_nonexistent_product_id_is_passed(self, mock_get_product_by_id): response_mock = MagicMock() type(response_mock).status_code = PropertyMock(return_value=404) mock_get_product_by_id.side_effect = exceptions.HTTPError( '404 Client Error: Not Found for url: http://api.products.com/api/product/okok', response=response_mock ) request_body = { 'product_id': 'okok' } request = self.factory.post( self.uri, request_body, HTTP_AUTHORIZATION='Token {}'.format(self.token.key) ) request.user = self.user response = self.view(request, customer_id=self.default_customer_id) expected_response_status_code = 400 expected_response_body = { 'detail': 'The requested Product doesn’t exist.' } self.utils.assert_response( response, expected_response_status_code, expected_response_body )
def test_post_notification_HTTPError_408(self, mock_post): mock_post.return_value.raise_for_status.side_effect = req_exc.HTTPError( 'hello') mock_post.return_value.status_code = 408 notification_status, notification_error = post_notification( None, None, None) self.assertEqual(notification_status, Constants.PENDING) self.assertEqual(notification_error, 'hello')
def test_http_error(self): url = 'http://example.com/template' requests.get(url, stream=True).AndRaise(exceptions.HTTPError()) self.m.ReplayAll() self.assertRaises(urlfetch.URLFetchError, urlfetch.get, url) self.m.VerifyAll()
def test_parse_exception_request_exception(self): raw = req_exc.HTTPError(401, 'ERROR') ex = self.assertRaises(senlin_exc.InternalError, sdk.parse_exception, raw) self.assertEqual(401, ex.code) self.assertEqual('[Errno 401] ERROR', ex.message)
def safe_GET(url, **kwargs): resp = requests.get(url, **kwargs) if resp.status_code != 200: raise exceptions.HTTPError(resp.status_code, "error from spotify client") return resp
def _send(self, req: Request) -> Response: r = self._s.send(self._s.prepare_request(req)) if not r.ok: raise exceptions.HTTPError( f"failed with status code {r.status_code}: {r.reason}: {r.json()}", response=r, ) return r
def test_get_product_by_id_should_return_exception_when_repository_returns_unsuccessful_request( self, mock_repository_response): response_mock = MagicMock() response_mock.raise_for_status.side_effect = exceptions.HTTPError( '404 Client Error: Not Found for url: http://api.products.com/api/product/okok', response=response_mock) mock_repository_response.return_value = response_mock self.assertRaises(exceptions.HTTPError, ProductService.get_product_by_id, 1)
def test_container_kill_catches_when_response_is_409(mocker, client, running_container): # Given response = mocker.Mock() response.status_code = 409 running_container.kill = mocker.Mock( side_effect=exceptions.HTTPError('', response=response)) cntr = Container(Image(''), dockerclient=client) cntr.run() # When cntr.kill()
def process_response(self, response, request): '''process response and handle statues and exceptions''' if response.status_code <= 204: result = response.json() if "error" in result: msg = result.get("error") # populate exception messages.error(request, msg) if settings.DEBUG: LOG.exception(msg) return result else: if response.status_code == 401: raise exceptions.HTTPError('Unautorized 401') if response.status_code == 400: raise exceptions.HTTPError('Bad Request 400') if response.status_code == 500: LOG.exception(request.body) raise exceptions.HTTPError('Unexpected exception 500') return Exception(response.status_code)
def test_container_kill_raises_when_response_is_404(mocker, client, running_container): # Given response = mocker.Mock() response.status_code = 404 running_container.kill = mocker.Mock( side_effect=exceptions.HTTPError('', response=response)) cntr = Container(Image(''), dockerclient=client) cntr.run() # Ensure with pytest.raises(exceptions.HTTPError): # When cntr.kill()
def execute(self, http_method, path, params=None, data=None, headers=None, allow_redirects=False, urlencode=True): """ Submit an HTTP request. @param http_method: GET, POST, PUT, DELETE @param path: The path of the resource. Unsafe characters will be quoted. @param params: Key-value parameter data. @param data: The data to attach to the body of the request. @param headers: The headers to set for this request. @param allow_redirects: requests should automatically resolve redirects. @param urlencode: percent encode paths. @return: The result of urllib2.urlopen() """ # Prepare URL and params if urlencode: path = urllib.quote(smart_str(path)) url = self._make_url(path, params) if http_method in ("GET", "DELETE"): if data is not None: self.logger.warn( "GET and DELETE methods do not pass any data. Path '%s'" % path) data = None request_kwargs = {'allow_redirects': allow_redirects} if headers: request_kwargs['headers'] = headers if data: request_kwargs['data'] = data try: resp = getattr(self._session, http_method.lower())(url, **request_kwargs) if resp.status_code >= 300: resp.raise_for_status() raise exceptions.HTTPError(response=resp) return resp except (exceptions.ConnectionError, exceptions.HTTPError, exceptions.RequestException, exceptions.URLRequired, exceptions.TooManyRedirects), ex: raise self._exc_class(ex)
def create_opa_policy(url, file_path): """ This function creates a OPA policy on the server :param url: url address where policy is placed :param file_path: .rego file path :return: Rest Return """ policy_resp = None with open(file_path, 'r') as file: policy_data = file.read() policy_resp = RestClientApis.http_put_and_check_success(url, policy_data, headers={'Content-Type': 'text/plain'}, params={'file': file_path}) if policy_resp.http_status != HTTPStatus.OK: raise exceptions.HTTPError(policy_resp.message) return policy_resp
def _query_NASAPower_server(self, latitude, longitude, start_date, end_date): """Query the NASA Power server for data on given latitude-longitude and dates. Currently works only with daily data. Args: latitude (float): Latitude. longitude (float): Longitude. start_date (datetime.date): Starting date. end_date (datetime.date): Ending date. Raises: exceptions.HTTPError: Raises when fails to retrieve POWER data. Returns: dict: The response of the server in JSON format """ # Build URL for retrieving data server = "https://power.larc.nasa.gov/cgi-bin/v1/DataAccess.py" payload = { "request": "execute", "identifier": "SinglePoint", "parameters": ",".join(self.power_variables), "lat": latitude, "lon": longitude, "startDate": start_date.strftime("%Y%m%d"), "endDate": end_date.strftime("%Y%m%d"), "userCommunity": "AG", "tempAverage": 'DAILY', "outputList": "JSON", "user": "******" } print("Starting retrieval from NASA Power...") request = requests.get(server, params=payload) # Check if server didn't respond to HTTP code = 200 if request.status_code != self.HTTP_OK: raise exceptions.HTTPError( "Failed retrieving POWER data, server returned HTTP code: {} on following URL {}." .format(request.status_code, request.url)) # In other case is successful print("Successfully retrieved data from NASA Power!") return (request.json())
def _request(self, method, url, **kwargs): """ Utility to request an endpoint with the correct authentication setup, raises on errors and returns the JSON. @param method as str @param url as str @param kwargs as requests.request named args """ from requests import request, exceptions url = self.__api_endpoint.rstrip('/') + '/' + url.lstrip('/') raw_resp = request(method, url, auth=self.__auth, **kwargs) raw_resp.raise_for_status() if raw_resp.status_code == 304: http_error_msg = '%s Client Error: %s for url: %s' % ( raw_resp.status_code, raw_resp.reason, raw_resp.url) raise exceptions.HTTPError(http_error_msg, response=raw_resp) return raw_resp.json()
def _getHotelsFromSource(self, country, city): destination = country if not city is None: destination += ', ' + city response = self._apiClient.requestHotelsNear(destination, self._clientId, self._clientSecret) # to raise an HTTPError if the response is an http error response.raise_for_status() self._hotels = [] responseJson = response.json() # destination existence validation provided by the consumed api if responseJson['meta']['code'] == 400: raise ValueError('Error') elif responseJson['meta']['code'] != 200: raise exceptions.HTTPError('Error') hotels = responseJson['response']['venues'] for hotelData in hotels: self._hotels.append({'name' : hotelData['name'], 'address' : ', '.join(hotelData['location']['formattedAddress'])})
def get_domain_records(domain): global GANDI_API_TOKEN try: uri = "https://dns.api.gandi.net/api/v5/domains/{}/records".format( domain) headers = {'X-Api-Key': GANDI_API_TOKEN} r = get(uri, headers=headers) logger.debug("Gandi API response: {}".format(r.json())) if not r.status_code == 200: raise exceptions.HTTPError("HTTP error: {}: {}".format( r.status_code, r.reason)) else: return r.json() except exceptions.RequestException as e: raise Exception( "Failed to retrieve the records for domain {}: {}".format( domain, e)) except Exception as e: raise Exception( "Unhandled exception while trying to retrieve the records for domain {}: {}" .format(domain, e))
def check_url(self): try: self.r.raise_for_status() print('Successful connection!', self.url) except exceptions.HTTPError: raise exceptions.HTTPError( 'Something went wrong.Incorrect URL or check your internet connection!' '\nCheck if the address is correct') except exceptions.ConnectionError: raise exceptions.ConnectionError( f"Something went wrong. Server Error!\nCan't find {self.url}!" f"\nCheck if the address is correct") except exceptions.MissingSchema: raise exceptions.MissingSchema( f"Something went wrong. Invalid url!\nTry https://{self.url}!" f"\n'Check if the address is correct'") except exceptions.InvalidSchema: raise exceptions.InvalidSchema( f"Something went wrong. Invalid url {self.url}!" f"\nCheck if the address is correct") return self.url
def test_http_error(self): url = 'http://example.com/template' mock_get = self.patchobject(requests, 'get') mock_get.side_effect = exceptions.HTTPError() self.assertRaises(urlfetch.URLFetchError, urlfetch.get, url) mock_get.assert_called_once_with(url, stream=True)
def test_http_error(self): url = 'http://example.com/somedata' self.patchobject(requests, 'get', side_effect=exceptions.HTTPError()) self.assertRaises(utils.URLFetchError, utils.url_fetch, url)
def delete_policy(url): resp = RestClientApis.http_delete_and_check_success(url) if resp.http_status != HTTPStatus.OK: raise exceptions.HTTPError(resp.message) return