Resource Schemas

doctor.resource.HTTP_METHOD_TITLES = {'DELETE': 'Delete', 'GET': 'Retrieve', 'POST': 'Create', 'PUT': 'Update'}

A mapping of HTTP method to title that should be used for it in API documentation.

class doctor.resource.ResourceAnnotation(logic, http_method, title=None)[source]

Bases: object

Metadata about the types used for a given request method.

Parameters:
  • logic (Callable) – The logic function for the resource.
  • http_method (str) – The http method for this resource. One of DELETE, GET, POST or PUT.
  • title (Optional[str]) – The title to be used by the api documentation for this resource.
class doctor.resource.ResourceSchema(schema, handle_http=None, raise_response_validation_errors=False, **kwargs)[source]

Bases: doctor.schema.Schema

This class extends Schema with methods for generating HTTP handler functions that automatically parse and validate the request and response objects with a given schema.

_create_request_schema(params, required)[source]

Create a JSON schema for a request.

Parameters:
  • params (list) – A list of keys specifying which definitions from the base schema should be allowed in the request.
  • required (list) – A subset of the params that the requester must specify in the request.
Returns:

a JSON schema dict

class doctor.resource.ResourceSchemaAnnotation(logic, http_method, schema, request_schema, response_schema, title=None)[source]

Bases: object

Metadata about the schema used for a given request method.

An instance of this class is attached to each handler method in a _schema_annotation attribute. It can be used for introspection about the schemas, to generate things like API documentation and hyper schemas from the code.

Parameters:
  • logic (func) – Logic function which will handle the request.
  • http_method (str) – The HTTP request method for this request (e.g. GET).
  • schema (doctor.resource.ResourceSchema) – The resource schema object for this handler.
  • request_schema (dict) – The schema used to validate the request.
  • response_schema (dict) – The schema used to validate the response.
  • title (str) – A short title for the route. e.g. ‘Create Foo’ might be used for a POST method on a FooListHandler.
classmethod get_annotation(fn)[source]

Find the _schema_annotation attribute for the given function.

This will descend through decorators until it finds something that has the attribute. If it doesn’t find it anywhere, it will return None.

Parameters:fn (func) – Find the attribute on this function.
Returns:an instance of ResourceSchemaAnnotation or None.