Reference

In class Meta we declare different resource’s options. There is a possibility to write your own behavior for certain methods .

class awokado.resource.BaseResource
class Response(resource: BaseResource, is_list: bool = False)

Response class helps to collect your data and prepare it in a readable format for the Frontend (or another API Client)

You can override it in your resource to change response format:

class MyResponse(Response):
    PAYLOAD_KEYWORD = "data"

class MyBaseResource(BaseResource):
    Response = MyResponse

Default serialization for list requests (/v1/book/):

{
  "payload": {
    "book": [
      {
        "name": "My Book",
        "authors": [1, 2]
      }
    ]
  },
  "meta": {
    "total": 1
  }
}

Default serialization for single object (/v1/book/123):

{
  "book": [
    {
      "name": "My Book",
      "authors": [1, 2]
    }
  ]
}
auth(*args, **kwargs) → awokado.utils.AuthBundle

This method should return (user_id, token) tuple

create(session: sqlalchemy.orm.session.Session, payload: dict, user_id: int) → dict

Create method

You can override it to add your logic.

First of all, data is prepared for creating: Marshmallow load method for data structure deserialization and then preparing data for SQLAlchemy create a query.

Inserts data to the database (Uses bulky library if there is more than one entity to create). Saves many-to-many relationships.

Returns created resources with the help of read_handler method.

delete(session: sqlalchemy.orm.session.Session, user_id: int, obj_ids: list)

Simply deletes objects with passed identifiers

on_delete(req: falcon.request.Request, resp: falcon.response.Response, resource_id: int = None)

Falcon method. DELETE-request entry point.

Here is a database transaction opening. This is where authentication takes place (if auth class is pointed in resource) Then delete method is run.

on_get(req: falcon.request.Request, resp: falcon.response.Response, resource_id: int = None)

Falcon method. GET-request entry point.

Here is a database transaction opening. This is where authentication takes place (if auth class is pointed in resource) Then read_handler method is run. It’s responsible for the whole read workflow.

on_patch(req: falcon.request.Request, resp: falcon.response.Response, *args, **kwargs)

Falcon method. PATCH-request entry point.

Here is a database transaction opening. This is where authentication takes place (if auth class is pointed in resource) Then update method is run.

on_post(req: falcon.request.Request, resp: falcon.response.Response)

Falcon method. POST-request entry point.

Here is a database transaction opening. This is where authentication takes place (if auth class is pointed in resource) Then create method is run.

update(session: sqlalchemy.orm.session.Session, payload: dict, user_id: int, *args, **kwargs) → dict

First of all, data is prepared for updating: Marshmallow load method for data structure deserialization and then preparing data for SQLAlchemy update query.

Updates data with bulk_update_mappings sqlalchemy method. Saves many-to-many relationships.

Returns updated resources with the help of read_handler method.

class awokado.meta.ResourceMeta
Parameters:
  • name – Resource name. Used for two resources connection by relation
  • model – represents sqlalchemy model or cte
  • methods – tuple of methods you want to allow
  • auth – awokado BaseAuth class for embedding authentication logic
  • skip_doc – set true if you don’t need to add the resource to documentation
  • disable_total – set false, if you don’t need to know returning objects amount in read-requests
  • id_field – you can specify your own primary key if it’s different from the ‘id’ field. Used in reading requests (GET)
  • select_from – provide data source here if your resource use another’s model fields (for example sa.outerjoin(FirstModel, SecondModel, FirstModel.id == SecondModel.first_model_id))
class awokado.auth.BaseAuth

CREATE = { ‘ROLE NAME’: Boolean value }

Example: ‘ADMIN’: True, ‘GUEST’: False

READ, UPDATE, DELETE

class awokado.response.Response

Response class helps to collect your data and prepare it in a readable format for the Frontend (or another API Client)

You can override it in your resource to change response format:

class MyResponse(Response):
    PAYLOAD_KEYWORD = "data"

class MyBaseResource(BaseResource):
    Response = MyResponse

Default serialization for list requests (/v1/book/):

{
  "payload": {
    "book": [
      {
        "name": "My Book",
        "authors": [1, 2]
      }
    ]
  },
  "meta": {
    "total": 1
  }
}

Default serialization for single object (/v1/book/123):

{
  "book": [
    {
      "name": "My Book",
      "authors": [1, 2]
    }
  ]
}
META_KEYWORD = 'meta'
PAYLOAD_KEYWORD = 'payload'
TOTAL_KEYWORD = 'total'
serialize() → dict
set_parent_payload(parent_payload: Optional[List[T]] = None) → None
set_total(total_objects_count: int)