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: ...
- links¶
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"}
Link holder¶
- class resource_api_http_client.client.LinkHolder(client, url, schema)¶
Accessor for all the links associated with the resource
For link with cardinality “MANY” RootLinkCollection is returned:
>>> student.links.courses <RootLinkCollection object>
For link with cardinality “ONE” LinkToOne is returned:
>>> course.links.teacher <LinkToOne object>
Root link collection¶
- class resource_api_http_client.client.RootLinkCollection(client, base_url, target_name, name, params=None)¶
Root link collection is actually a normal link collection with two extra methods: create and get.
- create(data)¶
- data (dict)
- has to have at least one key called @target - its value must be a PK of target resource instance
>>> student_courses = student.links.courses >>> new_link_to_course = student_courses.create({"@target": "Maths"})
- get(target_pk)¶
- target_pk
- PK of target resource instance
>>> student_courses = student.links.courses >>> exisiting_link_to_course = student_courses.get("Biology")
Link collection¶
- class resource_api_http_client.client.LinkCollection(client, base_url, target_name, name, params=None)¶
The entity that represents a pile of resource links.
>>> student_courses = student.links.courses
The collection is iterable:
>>> for link in student_courses: >>> ...
Accessing items by index is also possible: >>> link = student_courses[15]
- 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_courses) 4569 >>> student_courses.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_courses = student.links.courses >>> new_link_collection = student_courses.filter(grade__gte=3)
Link instance¶
- class resource_api_http_client.client.LinkInstance(client, base_url, target_name, target_pk, data=None, unique=False)¶
Whenever creating new or fetching existing links link instances are returned. Link instances are also returned whenever iterating over link collections.
- data¶
Returns data associated with the link
>>> link.data {"grade": 3}
- delete()¶
Removes the link
>>> link.delete() >>> link.data ... DoesNotExist: ...
- target¶
Returns a ResourceInstance associated with target resource.
>>> link.target.pk "Maths"
Link to one¶
- class resource_api_http_client.client.LinkToOne(client, base_url, target_name, name)¶
Represents a relationship with cardinality ONE
- item¶
Returns LinkInstance if it exists, raises DoesNotExist error otherwise
>>> course.links.teacher.item.delete() >>> course.links.teacher.item ... DoesNotExist ...
- set(data)¶
Does the same thing as update method but CAN change the @target
>>> course.links.teacher.item.target.pk "Hades" >>> course.links.teacher.set({"@target": "Zeuz"}) >>> course.links.teacher.item.target.pk "Zeus"