def fetch_data(url, auth_header, object_ids, logger, time_metric, exception_metric): """ fetch objects based on ID in batches of 40 for given RESTful URL A batch size of 40 was chosen to fetch as many items per request as we can, but still keep some headroom in the URL length. """ BATCH_SIZE = 40 results = [] object_ids_to_fetch = object_ids while len(object_ids_to_fetch) > 0: object_id_batch = object_ids_to_fetch[:BATCH_SIZE] response_json = fetch_url( url % (",".join(object_id_batch)), auth_header, logger, time_metric, exception_metric, ) # older APIs sent data in "results", newer uses "data" if "data" in response_json: results += response_json["data"] elif "results" in response_json: results += response_json["results"] else: raise ServiceError("unparsable result returned from service") object_ids_to_fetch = object_ids_to_fetch[BATCH_SIZE:] return results
def _validate_service_response(response, logger, auth_header): """ Raise an exception if the response was not what we expected. """ if response.status_code == requests.codes.not_found: logger.info("%s error received from service: %s" % (response.status_code, response.text)) raise ItemNotReturned(response.text) if response.status_code in [ requests.codes.forbidden, requests.codes.unauthorized ]: logger.info("%s error received from service: %s" % (response.status_code, response.text)) # Log identity header if 401 (unauthorized) if response.status_code == requests.codes.unauthorized: if isinstance(auth_header, dict) and AUTH_HEADER_NAME in auth_header: logger.info("identity '%s'" % get_key_from_headers(auth_header)) else: logger.info("no identity or no key") raise RBACDenied(response.text) if response.status_code != requests.codes.ok: logger.warn("%s error received from service: %s" % (response.status_code, response.text)) raise ServiceError("Error received from backend service")
def test_comparison_report_api_500_backend(self, mock_fetch_systems): mock_fetch_systems.side_effect = ServiceError("oops") response = self.client.get( "api/drift/v1/comparison_report?" "system_ids[]=d6bba69a-25a8-11e9-81b8-c85b761454fa" "&system_ids[]=11b3cbce-25a9-11e9-8457-c85b761454fa", headers=fixtures.AUTH_HEADER, ) self.assertEqual(response.status_code, 500)
def test_comparison_report_api_500_errors_preference( self, mock_fetch_systems, mock_fetch_baselines): mock_fetch_systems.side_effect = ItemNotReturned("oops") mock_fetch_baselines.side_effect = ServiceError("oops") response = self.client.get( "api/drift/v1/comparison_report?" "system_ids[]=d6bba69a-25a8-11e9-81b8-c85b761454fa" "&system_ids[]=11b3cbce-25a9-11e9-8457-c85b761454fa" "&baseline_ids[]=ff35596c-f98e-11e9-aea9-98fa9b07d419", headers=fixtures.AUTH_HEADER, ) self.assertEqual(response.status_code, 500)
def _validate_service_response(response, logger): """ Raise an exception if the response was not what we expected. """ if response.status_code == requests.codes.not_found: logger.info("%s error received from service: %s" % (response.status_code, response.text)) raise ItemNotReturned(response.text) if response.status_code != requests.codes.ok: logger.warn("%s error received from service: %s" % (response.status_code, response.text)) raise ServiceError("Error received from backend service")
def fetch_data(url, auth_header, object_ids, logger, time_metric, exception_metric): """ fetch objects based on ID in batches of 40 for given RESTful URL A batch size of 40 was chosen to fetch as many items per request as we can, but still keep some headroom in the URL length. """ BATCH_SIZE = 40 # tags API call returns a dict in "results", so we now # need to handle either dict or list/array in "results" result_list = [] result_dict = {} object_ids_to_fetch = object_ids while len(object_ids_to_fetch) > 0: object_id_batch = object_ids_to_fetch[:BATCH_SIZE] response_json = fetch_url( url % (",".join(object_id_batch)), auth_header, logger, time_metric, exception_metric, ) # older APIs sent data in "results", newer uses "data" if "data" in response_json: result = response_json["data"] elif "results" in response_json: result = response_json["results"] else: raise ServiceError("unparsable result returned from service") if isinstance(result, dict): result_dict.update(result) else: result_list += result object_ids_to_fetch = object_ids_to_fetch[BATCH_SIZE:] results = result_dict if result_dict else result_list return results