Resources

This section covers API documentation for all classes and functions related to constructing REST API resources. For more information on resources in TBone see Resources

Authentication

class tbone.resources.authentication.NoAuthentication

Base class for all authentication methods. Used as the default for all resouces. This is a no-op authenttication class which always returns True

coroutine is_authenticated(request)

This method is executed by the Resource class before executing the request. If the result of this method is False the request will not be executed and the response will be 401 un authorized. The basic implementation is no-op and always returns True

Formatters

class tbone.resources.formatters.Formatter

Base class for all formatters. Subclass this to create custom formatters

format(data: dict)

Formats python dict into a data string. Implement in derived classes for specific transport protocols

parse(body)

Parses a string data to python dict. Implement in derived classes for specific transport protocols

class tbone.resources.formatters.JSONFormatter

Implements JSON formatting and parsing

Base Resource

class tbone.resources.resources.ModelResource(*args, **kwargs)

A specialized resource class for using data models. Requires further implementation for data persistency

class tbone.resources.resources.Resource(*args, **kwargs)

Base class for all resources.

class Protocol

An enumeration.

add_hypermedia(obj)

Adds HATEOAS links to the resource. Adds href link to self. Override in subclasses to include additional functionality

classmethod as_detail(protocol=<Protocol.http: 10>, *args, **kwargs)

returns detail views

classmethod as_list(protocol=<Protocol.http: 10>, *args, **kwargs)

returns list views

classmethod as_view(endpoint, protocol, *init_args, **init_kwargs)

Used for hooking up the endpoints. Returns a wrapper function that creates a new instance of the resource class and calls the correct view method for it.

classmethod build_http_response(data, status=200)

Given some data, generates an HTTP response. If you’re integrating with a new web framework, other than sanic or aiohttp, you MUST override this method within your subclass.

Parameters:
  • data (string) – The body of the response to send
  • status (integer) – (Optional) The status code to respond with. Default is 200
Returns:

A response object

coroutine dispatch(*args, **kwargs)

This method handles the actual request to the resource. It performs all the neccesary checks and then executes the relevant member method which is mapped to the method name. Handles authentication and de-serialization before calling the required method. Handles the serialization of the response

dispatch_error(err)

Handles the dispatch of errors

format(method, data)

Calls format on list or detail

classmethod nested_routes(base_url, formatter: <built-in function callable> = None) → list

Returns an array of Route objects which define additional routes on the resource. Implement in derived resources to add additional routes to the resource

Parameters:
  • base_url – The URL prefix which will be prepended to all nested routes
  • formatter – The format method to be used when parsing url variables. By default the resource’s route_param method is used, which formats the url based on which HTTP libary is used.
parse(method, endpoint, body)

calls parse on list or detail

request_args()

Returns the arguments passed with the request in a dictionary. Returns both URL resolved arguments and query string arguments. Implemented for specific http libraries in derived classes

coroutine request_body()

Returns the body of the current request. Implemented for specific http libraries in derived classes

request_method()

Returns the HTTP method for the current request.

classmethod route_methods()

Returns the relevant representation of allowed HTTP methods for a given route. Implemented on the http library resource sub-class to match the requirements of the HTTP library

classmethod route_param(param, type=<class 'str'>)

Returns the route representation of a url param, pertaining to the web library used. Implemented on the http library resource sub-class to match the requirements of the HTTP library

classmethod wrap_handler(handler, protocol, **kwargs)

Wrap a request handler with the matching protocol handler

class tbone.resources.resources.ResourceOptions(meta=None)

A configuration class for Resources. Provides all the defaults and allows overriding inside the resource’s definition using the Meta class

Parameters:
  • name – Declare the resource’s name. If None the class name will be used. Default is None
  • object_class – Declare the class of the underlying data object. Used in MongoResource to bind the resource class to a Model
  • query – Define a query which the resource will apply to all list calls. Used in MongoResource to apply a default query fiter. Useful for cases where the entire collection is never queried.
  • sort – Define a sort directive which the resource will apply to GET requests without a unique identifier. Used in MongoResource to declare default sorting for collection.
  • hypermedia – Specify if the a Resource should format data and include HATEOAS directives, specifically link to itself. Defaults to True
  • fts_operator – Define the FTS (full text search) operator used in url parameters. Used in MongoResource to perform FTS on a collection. Default is set to q.
  • incoming_list – Define the methods the resource allows access to without a primary key. These are incoming request methods made to the resource. Defaults to a full access ['get', 'post', 'put', 'patch', 'delete']
  • incoming_detail – Same as incoming_list but for requests which include a primary key
  • outgoing_list – Define the resource events which will be emitted without a primary key. These are outgoing resource events which are emitted to subscribers. Defaults to these events ['created', 'updated', 'deleted']
  • outgoing_detail – Same as outgoing_list but for resource events which include a primary key
  • formatter – Provides an instance to a formatting class the resource will be using when formatting and parsing data. The default is JSONFormatter. Developers can subclass Formatter base class and provide implementations to other formats.
  • authentication – Provides and instance to the authentication class the resource will be using when authenticating requests. Default is NoAuthentication. Developers must subclass the NoAuthentication class to provide their own resource authentication, based on the application’s authentication choices.
  • channel – Defines the Channel class which the resource will emit events into. Defaults to in-memory
channel_class

alias of Channel

MongoDB Resource

AioHttp Mixin

Sanic Mixin

Router