Пример #1
0
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)
Пример #2
0
    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)
Пример #3
0
 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"]))
Пример #4
0
    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)
Пример #5
0
 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"]))
Пример #6
0
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"]
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
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()
Пример #10
0
 def base_api_path(self):
     return path_join(self._api_path, self._api_version)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
 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)
Пример #14
0
 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)
Пример #15
0
 def base_api_path(self):
     return path_join(self._api_path, self._api_version)