Service and registry¶
Service class¶
The entity main in Resource API is called Service.
- class resource_api.service.Service¶
Entity responsible for holding a registry of all resources that are supposed to be exposed
Service has to be subclassed in order to implement usecase specific _get_context and _get_user methods.
NOTE: do not override any of the public methods - it may cause framework’s misbehavior.
- _get_context()¶
MUST BE OVERRIDEN IN A SUBCLASS
Must return an object holding all database connections, sockets etc. It is later on passed to all individual resources.
- _get_user(data)¶
MUST BE OVERRIDEN IN A SUBCLASS
Must return an object representing currently authenticated user. It is later on passed to individual can_?? methods of various resources for authorization purposes.
- get_entry_point(data)¶
Returns entry point
- data
- intormation to be used to construct user object via _get_user method
- get_schema(human=True)¶
Returns schema for all registered resources.
- human (bool = True)
- if True it returns schema with namespaces used during registration if False it returns schema with resource module names as namespaces
- register(resource, name=None)¶
Add resource to the registry
- resource (Resource subclass)
- entity to be added to the registry
- name (string)
- string to be used for resource registration, by default it is resource’s module name + class name with ”.” as a delimiter
- setup()¶
Finalizes resource registration.
MUST be called after all desired resources are registered.
Resource registration¶
Lets say that there are multiple resources declared somewhere. In this case they can be registered the following way:
class MultiSQLService(Service):
def _get_context(self):
return {
"db1": create_connection(...),
"db2": create_connection(...)
}
srv = MultiSQLService()
srv.register(Student)
srv.register(Teacher)
srv.register(Course)
srv.setup()
Entry point¶
In order to get access to the object interface user must call get_entry_point method.
entry_point = srv.get_entry_point({"username": "FOO"})