aiohttp_json_api.abc package

Submodules

class aiohttp_json_api.abc.contoller.ControllerABC(context)[source]

Bases: abc.ABC

add_relationship(field, resource, data, sp, **kwargs)[source]

Adds the members specified in the JSON API relationship object data to the relationship, unless the relationships already exist.

Parameters:
  • field (FieldABC) – Relationship field.
  • resource – Resource instance fetched by fetch_resource() in handler.
  • data (str) – The JSON API relationship object with the update information.
  • sp (JSONPointer) – The JSON pointer to the source of data.
create_resource(data, **kwargs)[source]

Creates a new resource instance and returns it. You should override this method.

The default implementation passes the attributes, (dereferenced) relationships and meta data from the JSON API resource object data to the constructor of the resource class. If the primary key is writable on creation and a member of data, it is also passed to the constructor.

Parameters:data (dict) – The JSON API deserialized data by schema.
static default_add(field, resource, data, sp, **kwargs)[source]
static default_include(field, resources, **kwargs)[source]
static default_query(field, resource, **kwargs)[source]
static default_remove(field, resource, data, sp, **kwargs)[source]
delete_resource(resource_id, **kwargs)[source]

Deletes the resource. You must overridde this method.

Parameters:
  • resource_id – The id of the resource or the resource instance
  • context (JSONAPIContext) – Request context instance
fetch_compound_documents(field, resources, *, rest_path=None, **kwargs)[source]

Fetches the related resources. The default method uses the controller’s default_include(). Can be overridden.

Parameters:
  • field (FieldABC) – Relationship field.
  • resources – A list of resources.
  • context (JSONAPIContext) – Request context instance.
  • rest_path (list) – The name of the relationships of the returned relatives, which will also be included.
Return type:

list

Returns:

A list with the related resources. The list is empty or has exactly one element in the case of to-one relationships. If to-many relationships are paginated, the relatives from the first page should be returned.

fetch_resource(resource_id, **kwargs)[source]
query_collection(**kwargs)[source]

Fetches a subset of the collection represented by this schema. Must be overridden.

Parameters:context (JSONAPIContext) – Request context instance.
query_relatives(field, resource, **kwargs)[source]

Controller for the related endpoint of the relationship with then name relation_name.

Parameters:
query_resource(resource_id, **kwargs)[source]

Fetches the resource with the id id_. Must be overridden.

Parameters:
  • resource_id (str) – The id of the requested resource.
  • context (JSONAPIContext) – A request context instance
Raises:

ResourceNotFound – If there is no resource with the given id_.

remove_relationship(field, resource, data, sp, **kwargs)[source]

Deletes the members specified in the JSON API relationship object data from the relationship.

Parameters:
  • field (FieldABC) – Relationship field.
  • resource – Resource instance fetched by fetch_resource() in handler.
  • data (str) – The JSON API relationship object with the update information.
  • sp (JSONPointer) – The JSON pointer to the source of data.
  • context (JSONAPIContext) – Request context instance.
update_relationship(field, resource, data, sp, **kwargs)[source]

Updates the relationship with the JSON API name relation_name.

Parameters:
  • field (FieldABC) – Relationship field.
  • resource – Resource instance fetched by fetch_resource() in handler.
  • data (str) – The JSON API relationship object with the update information.
  • sp (JSONPointer) – The JSON pointer to the source of data.
update_resource(resource_id, data, sp, **kwargs)[source]

Updates an existing resource. You should overridde this method in order to save the changes in the database.

The default implementation uses the BaseField descriptors to update the resource.

Parameters:
  • resource_id – The id of the resource
  • data (dict) – The JSON API resource object with the update information
  • sp (JSONPointer) – The JSON pointer to the source of data.
  • context (JSONAPIContext) – Request context instance
class aiohttp_json_api.abc.contoller.ControllerMeta(name, bases, attrs)[source]

Bases: abc.ABCMeta, aiohttp_json_api.abc.processors.MetaProcessors

Field abstract base class

class aiohttp_json_api.abc.field.FieldABC[source]

Bases: abc.ABC

deserialize(schema, data, sp, **kwargs)[source]

Deserialize the raw data from the JSON API input document and returns it.

key
Return type:str
mapped_key
Return type:Optional[str]
name
Return type:Optional[str]
post_validate(schema, data, sp)[source]

Validates the decoded input data for this field. This method is called after deserialize().

Parameters:
  • schema (BaseSchema) – The schema this field has been defined on.
  • data – The decoded input data
  • sp (JSONPointer) – A JSON pointer to the source of data.
  • context (JSONAPIContext) – A JSON API request context instance
pre_validate(schema, data, sp)[source]

Validates the raw JSON API input for this field. This method is called before deserialize().

Parameters:
  • schema (BaseSchema) – The schema this field has been defined on.
  • data – The raw input data
  • sp (JSONPointer) – A JSON pointer to the source of data.
  • context (JSONAPIContext) – A JSON API request context instance
serialize(schema, data, **kwargs)[source]

Serialize the passed data.

sp
Return type:JSONPointer
class aiohttp_json_api.abc.processors.MetaProcessors[source]

Bases: object

Schema abstract base classes

class aiohttp_json_api.abc.schema.SchemaABC(context)[source]

Bases: abc.ABC

OPTIONS_CLASS

alias of SchemaOpts

class Options[source]

Bases: object

static default_getter(field, resource, **kwargs)[source]
static default_setter(field, resource, data, sp, **kwargs)[source]
deserialize_resource(data, sp, *, expected_id=None, validate=True, validation_steps=None)[source]

Decodes the JSON API resource object data and returns a dictionary which maps the key of a field to its decoded input data.

Parameters:
  • data – The received JSON API resource object
  • sp (JSONPointer) – The JSON pointer to the source of data.
  • context (JSONAPIContext) – Request context instance
  • expected_id (str) – If passed, then ID of resource will be compared with this value. This is required in update methods
  • validate (bool) – Is validation required?
  • validation_steps (tuple) – Required validation steps
Return type:

OrderedDict

Returns:

An ordered dictionary which maps a fields key to a two tuple (data, sp) which contains the input data and the source pointer to it.

classmethod get_field(key)[source]
Return type:FieldABC
classmethod get_relationship_field(relation_name, source_parameter=None)[source]
get_value(field, resource, **kwargs)[source]
opts = <aiohttp_json_api.abc.schema.SchemaOpts object>
post_validate_resource(data)[source]

Validates the decoded data of JSON API resource object.

Parameters:
pre_validate_field(field, data, sp)[source]

Validates the input data for a field, before it is deserialized. If the field has nested fields, the nested fields are validated first.

Parameters:
pre_validate_resource(data, sp, *, expected_id=None)[source]

Validates a JSON API resource object received from an API client:

schema.pre_validate_resource(
    data=request.json["data"], sp="/data"
)
Parameters:
  • data – The received JSON API resource object
  • sp (JSONPointer) – The JSON pointer to the source of data.
  • context (JSONAPIContext) – Request context instance
  • expected_id (str) – If passed, then ID of resrouce will be compared with this value. This is required in update methods
serialize_relationship(relation_name, resource, *, pagination=None)[source]

Creates the JSON API relationship object of the relationship relation_name.

Parameters:
  • relation_name (str) – The name of the relationship
  • resource – A resource object
  • pagination (PaginationABC) – Describes the pagination in case of a to-many relationship.
Return type:

dict

Returns:

The JSON API relationship object for the relationship relation_name of the resource

serialize_resource(resource, **kwargs)[source]
set_value(field, resource, data, sp, **kwargs)[source]
class aiohttp_json_api.abc.schema.SchemaMeta(name, bases, attrs)[source]

Bases: abc.ABCMeta, aiohttp_json_api.abc.processors.MetaProcessors

class aiohttp_json_api.abc.schema.SchemaOpts(options)[source]

Bases: object

class Meta options for the SchemaABC. Defines defaults.

aiohttp_json_api.abc.schema.issubclass(subclass, baseclass)[source]

Just like the built-in issubclass(), this function checks whether subclass is inherited from baseclass. Unlike the built-in function, this issubclass will simply return False if either argument is not suitable (e.g., if subclass is not an instance of type), instead of raising TypeError.

Args:
subclass (type): The target class to check. baseclass (type): The base class subclass will be checked against.
>>> class MyObject(object): pass
...
>>> issubclass(MyObject, object)  # always a fun fact
True
>>> issubclass('hi', 'friend')
False

Module contents