Object interface

Object interface is accessible via so called entry point. Check here to understand how entry points are obtained.

NOTE: Whenever user performs an operation and the operation fails one of built-in exceptions is raised.

class resource_api.service.EntryPoint(service, user)

Represents user specific means of access to object interface.

get_resource(resource_class)

resource_class (Resource subclass)

>>> entry_point.get_resource(Student)
<RootResourceCollection object>
get_resource_by_name(resource_name)
resource_name (string)
namespace + ”.” + resource_name, where namespace can be a custom namespace or resource’s module name
>>> entry_point.get_resource_by_name("school.Student")
<RootResourceCollection object>
>>> entry_point.get_resource_by_name("com.example.module.education.Student")
<RootResourceCollection object>
user

User object returned by Service._get_user method

Root resource collection

class resource_api.resource.RootResourceCollection(entry_point, resource_interface, 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 = entry_point.get_resource(Student)
>>> new_student = student_collection.create({"first_name": "John",
>>>                                          "last_name": "Smith",
>>>                                          "email": "foo@bar.com",
>>>                                          "birthday": "1987-02-21T22:22:22"},
>>>                                         {"courses": [{"@target": "Maths", "grade": 4},
>>>                                                      {"@target": "Sports"}]})
get(pk)
>>> student_collection = entry_point.get_resource(Student)
>>> existing_student = student_collection.get("john@example.com")

Resource collection

class resource_api.resource.ResourceCollection(entry_point, resource_interface, params=None)

The entity that represents a pile of resources.

>>> student_collection = entry_point.get_resource(Student)

The collection is iterable:

>>> for student in student_collection:
>>>    ...

If Resource.get_uris is implemented to return an indexable entity the collection elements can be accessed by index as well:

>>> student = student_collection[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 Resource.get_count

>>> 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 = entry_point.get_resource(Student)
>>> new_collection = student_collection.filter(params={"name__startswith": "Abr"})

Resource item

class resource_api.resource.ResourceInstance(entry_point, resource_interface, pk)

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"}