def _list_objects(session, api_host, resource_path, filters, _extend_list=None): """ :param session: A session object to use to make the request :param api_host: The host where the API is located :param resource_path: The path where the objects are listed :param filters: Filters to apply to the request :return: The list of objects that are retrieved at this endpoint :rtype: list """ _extend_list = _extend_list if _extend_list is not None else [] if resource_path is None: return _extend_list for kw in API_ONLY_KW: assert kw not in filters, "You can't use the {0} pagination keyword".format(kw) #TODO: Accept on the first hit filters = _clean_related_filters(filters) res = session.get(path_join(api_host, resource_path), params=filters) res.raise_for_status() content = res.json() _extend_list.extend(content["objects"]) return _list_objects(session, api_host, content["meta"]["next"], filters, _extend_list)
def setUp(self): self.client = Client(TEST_ENDPOINT) # We'll never hit that anyway test_loc = path_join(TEST_ENDPOINT, "/created/loc/1") created = Response() created.status_code = 201 created.headers["location"] = test_loc self.adapter = PredictableTestAdapter([created, make_json_response(OBJ_RESPONSE)]) self.client._session.mount(TEST_ENDPOINT, self.adapter)
def get(self, **filters): matches = self.list(**filters) if not matches: raise NoSuchObject() if len(matches) > 1: raise MultipleObjectsReturned() # The API might return different results for a get and a list - we'll be safe and waste an API call here logger.debug("GET: %s", self.resource) return _get_by_url(self.client._session, path_join(self.client.host, matches.pop()["resource_uri"]))
def setUp(self): self.client = Client(TEST_ENDPOINT) # We'll never hit that anyway test_loc = path_join(TEST_ENDPOINT, "/created/loc/1") created = Response() created.status_code = 201 created.headers["location"] = test_loc self.adapter = PredictableTestAdapter( [created, make_json_response(OBJ_RESPONSE)]) self.client._session.mount(TEST_ENDPOINT, self.adapter)
def get(self, **filters): matches = self.list(**filters) if not matches: raise NoSuchObject() if len(matches) > 1: raise MultipleObjectsReturned() # The API might return different results for a get and a list - we'll be safe and waste an API call here logger.debug("GET: %s", self.resource) return _get_by_url( self.client._session, path_join(self.client.host, matches.pop()["resource_uri"]))
def _create_object(session, api_host, resource_path, kwargs): """ Create a new object in the API :param session: A session object to use to make the request :param api_host: The host where the API is located :param resource_path: The path where the objects are created :param resource: The resource to create :param kwargs: The arguments to use to create the object """ headers = {"Content-Type": "application/json"} res = session.post(path_join(api_host, resource_path), headers=headers, data=json.dumps(kwargs)) res.raise_for_status() return res.headers["location"]
def test_get_workflow(self): """ Test that the GET workflow is correct (list + retrieve) """ self.list_response["objects"] = [OBJ_1_PARTIAL] self.list_response["meta"]["total_count"] = 1 adapter = PredictableTestAdapter([make_json_response(self.list_response), make_json_response(OBJ_1)]) self.client._session.mount(TEST_ENDPOINT, adapter) self.client.measurements.get() self.assertEqual(2, len(adapter.requests)) l, retrieve = adapter.requests self.assertEqual("GET", l.method) self.assertEqual("GET", retrieve.method) self.assertEqual(path_join(TEST_ENDPOINT, "this/path"), retrieve.url)
def test_get_workflow(self): """ Test that the GET workflow is correct (list + retrieve) """ self.list_response["objects"] = [OBJ_1_PARTIAL] self.list_response["meta"]["total_count"] = 1 adapter = PredictableTestAdapter([ make_json_response(self.list_response), make_json_response(OBJ_1) ]) self.client._session.mount(TEST_ENDPOINT, adapter) self.client.measurements.get() self.assertEqual(2, len(adapter.requests)) l, retrieve = adapter.requests self.assertEqual("GET", l.method) self.assertEqual("GET", retrieve.method) self.assertEqual(path_join(TEST_ENDPOINT, "this/path"), retrieve.url)
def _update_object(session, api_host, obj): headers = {"Content-Type": "application/json"} res = session.patch(path_join(api_host, obj["resource_uri"]), headers=headers, data=json.dumps(obj)) res.raise_for_status()
def base_api_path(self): return path_join(self._api_path, self._api_version)
def list(self, **filters): logger.debug("LIST: %s", self.resource) return _list_objects(self.client._session, self.client.host, path_join(self.client.base_api_path, self.resource), filters)
def create(self, **kwargs): logger.debug("CREATE: %s", self.resource) location = _create_object(self.client._session, self.client.host, path_join(self.client.base_api_path, self.resource), kwargs) return _get_by_url(self.client._session, location)
def list(self, **filters): logger.debug("LIST: %s", self.resource) return _list_objects( self.client._session, self.client.host, path_join(self.client.base_api_path, self.resource), filters)
def create(self, **kwargs): logger.debug("CREATE: %s", self.resource) location = _create_object( self.client._session, self.client.host, path_join(self.client.base_api_path, self.resource), kwargs) return _get_by_url(self.client._session, location)