sanic-restful-resources 0.2.0
The library for writing RESTful APIs with sanic
Requires Python: >=3.5
Keywords: restful,rest,api,sanic,web,server

# sanic-restful-resources

[![PyPI version](https://badge.fury.io/py/sanic-restful-resources.svg)](https://badge.fury.io/py/sanic-restful-resources)
[![CodeFactor](https://www.codefactor.io/repository/github/michaelkrukov/sanic-restful-resources/badge)](https://www.codefactor.io/repository/github/michaelkrukov/sanic-restful-resources)
[![codebeat badge](https://codebeat.co/badges/3e8d5fda-c43d-4700-be78-11f5a2ad6dfa)](https://codebeat.co/projects/github-com-michaelkrukov-sanic-restful-resources-master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/315b7536ee7e4a59af03230b6738bde9)](https://www.codacy.com/manual/michaelkrukov/sanic-restful-resources?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=michaelkrukov/sanic-restful-resources&amp;utm_campaign=Badge_Grade)
[![Coverage Status](https://coveralls.io/repos/github/michaelkrukov/sanic-restful-resources/badge.svg?branch=master)](https://coveralls.io/github/michaelkrukov/sanic-restful-resources?branch=master)

> Simple library for creating RESTful APIs with sanic

## Features

- simplicity
- schematics integration
- exceptions handling
- unified response format
- 100% coverage

## Usage

```bash
python3 -m pip install sanic-restful-resources
```

## Example

```py
users = [{'name': 'Michael'}, {'name': 'Ivan'}]


class UserPostSchema(Model):
    name = StringType(required=True)


@resource('/users')
class Users:
    async def get(self, request):
        return users

    @validate(user_data=UserPostSchema)
    async def post(self, request, user_data):
        users.append({'name': user_data.name})
        return '', 204
```

More examples in [this folder](examples).

## Guide

- `resource(uri='')` - return decorator for classes that will be
  considered RESTful resources. This decorator automatically extends
  HTTPMethodView (refer to sanic documentation for details). You can
  provide resource URI through decorator or by class attribute `uri`.
  You can provide decorators for all the methods through class
  attribute `decorators`.

  Examples of possible return values for handlers:

  ```py
  return "data", 200, {"X-Custom-Header": "Value"}
  return "data", 200
  return "data"
  return {"arg": "val"}
  return ["val1", "val2"]
  return "", 201

  return sanic.response.json # .text, .html, e.t.c.
  ```

- `Api(name='API', url_prefix)` - class for aggregating resources
  and registrating them in the sanic application. Internally it uses
  blueprints. Basic workflow:

  ```py
  from sanic import Sanic
  from resources import User, Users

  app = Sanic()

  api = Api(url_prefix='/api/v1')
  api.add_resource(User)
  api.add_resource(Users)
  api.init_app(app)

  # ...
  ```

- `validate(**models)` - decorator for methods, that will validate
  incoming data with provided models from Schematics library. Successfully
  validated and parsed models will be passed as keyword arguments to
  the handler method. If any model fails to validate - handler will
  not be called.

- `error(description=None, details=None, status=400, **kwargs` - method
  for returning errors from handlers.

- `collect_args(request)` - method for getting data from all possible
  sources of data in the request.