Skip to main content

sanic-restful-resources

PyPI version CodeFactor codebeat badge Codacy Badge Coverage Status

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

More examples in this folder.

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.

Metadata

Release files for sanic-restful-resources 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sanic-restful-resources 0.2.0
File Size Uploaded
sanic_restful_resources-0.2.0.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sanic-restful-resources 0.2.0
File Interpreter ABI Platform
sanic_restful_resources-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.2 kB

Release files / sanic_restful_resources-0.2.0.tar.gz

Download URL sanic_restful_resources-0.2.0.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
52d912ee46e00ead072920e29891e702e1c16c75720a18fee810e7a1ef754fb4
BLAKE2b-256 checksum
How to use checksums
d0c593f8fac9fea374075a4e69137891db6f1569c7c85b0b8c523f6c0101d51a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

Release files / sanic_restful_resources-0.2.0-py3-none-any.whl

Download URL sanic_restful_resources-0.2.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
864c7c92a923153f1747236530ec552bc347bca4db8bde61ad5f94809d37bc14
BLAKE2b-256 checksum
How to use checksums
e057e53826f3a7ac9a0061c29a7d345d1bce904f997a68beca5de187fb2bdf19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page