HTTP client

HTTP clinet interface is similar in its design to object interface.

class resource_api_http_client.client.Client(base_url, transport_client)

Client side entry point.

It can be instanciated the following way with a URL of the HTTP service and authentication headers as parameters:

>>> client = Client.create(base_url="http://example.com/api",
                           auth_headers={"auth_token": "foo-bar-17"})
classmethod create(base_url, auth_headers=None)

Instanciates the client

base_url (string)
URL of Resource API server (e.g.: “http://example.com/api”)
auth_headers (dict || None)
Dictionary with fields that are later on used to construct user object <resource_api.service.Service_get_user>
get_resource_by_name(resource_name)
resource_name (string)
E.g.: “school.Student”
@return
<RootResourceCollection instance>
schema

Contains Resource API schema

Root resource collection

class resource_api_http_client.client.RootResourceCollection(client, name, params=None)

Root resource collection is actually a normal resource collection with two extra methods: create and get.

create(data, link_data=None)
>>> student_collection = client.get_resource_by_name("school.Student")
>>> new_student = student_collection.create({"first_name": "John", "last_name": "Smith", "email": "foo@bar.com",
>>>                                          "birthday": "1987-02-21T22:22:22"})
get(pk)
>>> student_collection = client.get_resource_by_name("school.Student")
>>> existing_student = student_collection.get("john@example.com")

Resource collection

class resource_api_http_client.client.ResourceCollection(client, name, params=None)

The entity that represents a pile of resources.

>>> student_collection = client.get_resource_by_name("school.Student")

The collection is iterable:

>>> for student in student_collection:
>>>    ...
count()

Returns count of all items within the system that satisfy filtering criterias.

NOTE: len(collection) is supposed to return the same result as collection.count(). The key difference between them is that len needs to fetch all items in the collection meanwhile collection.count() relies on /<ResourceName>:count URL

>>> len(student_collection)
4569
>>> student_collection.count()
4569
filter(params=None)

Filtering options can be applied to collections to return new collections that contain a subset of original items:

NOTE: filtering operations applied to root collections return normal collections

>>> student_collection = client.get_resource_by_name("school.Student")
>>> new_collection = student_collection.filter(params={"name__startswith": "Abr"})

Resource item

class resource_api_http_client.client.ResourceInstance(client, name, pk, data=None)

Whenever creating new or fetching existing resources resource instances are returned. Resource instances are also returned whenever iterating over resource collections.

data

Returns data associated with the resource

>>> student.data
{"first_name": "John", "last_name": "Smith", "email": "foo@bar.com", "birthday": "1987-02-21T22:22:22"}
delete()

Removes the resource

>>> student.delete()
>>> student.data
...
DoesNotExist: ...

Returns a link holder

pk

Returns PK of the resource

>>> student.pk
"foo@bar.com"
update(data)

Changes specified fields of the resource

>>> student.update({"first_name": "Looper"})
>>> student.data
{"first_name": "Looper", "last_name": "Smith", "email": "foo@bar.com", "birthday": "1987-02-21T22:22:22"}