Skip to main content

The library for writing RESTful APIs with sanic

Project description

sanic-restful-resources

Simple library for creating RESTful APIs with sanic.

Features

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

Usage

python3 -m pip install sanic-restful-resources

Example

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

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:

    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:

    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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sanic_restful_resources-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

sanic_restful_resources-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file sanic_restful_resources-0.1.0.tar.gz.

File metadata

  • Download URL: sanic_restful_resources-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

File hashes

Hashes for sanic_restful_resources-0.1.0.tar.gz
Algorithm Hash digest
SHA256 296e3e9c2087164d82a35d32ba90bb93088898419e3f192ec659ef537530956c
MD5 26edfaea90180f826c40a6f361edc152
BLAKE2b-256 f723178bc6db846b9ffd573dfb2aa3b8319ac4256a8c2cf587e90835c9fbe2f3

See more details on using hashes here.

File details

Details for the file sanic_restful_resources-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sanic_restful_resources-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.8.0

File hashes

Hashes for sanic_restful_resources-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a7c0c35a58b48b2cc4de0bf9e58cfa34bb6f4c72a7b397ba6a5cae70ced6284
MD5 3df1b3c5c3f947bb0e82b55c41a4a749
BLAKE2b-256 5f1d3b3b11e0f2323e81216d2bb3257c2cdeacb8ed8dcd999c6bd6e7b9d2c2e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page