Skip to main content

API Decorators

Project description

API Decorators

from api import jwt_auth, is_owner, get, insert, is_owner, has_role, update, push, pull, \
                validate, validate_push
from cerberus import Validator

schema = {
    'author': {'type': 'string'},
    'title': {'type': 'string'}
}

validator = Validator(schema)

schema_comments = {
    'author': {'type': 'string'},
    'message': {'type': 'string'}
}

validator_comments = Validator(schema_comments)

def set_routes_book(routes):

    @routes.get('/api/book')
    @jwt_auth
    #@is_owner('book')
    @get_many('book')
    async def get_many_book(query):  
        author = query["author"]
        return {"author": author}, 0, 10 # mongo query, skip and limit

    @routes.get('/api/book/{_id}')
    @jwt_auth
    #@is_owner('book')
    @get('book')
    async def get_book(document):      
        return document

    @routes.put('/api/book/{_id}/push/{push}')
    @jwt_auth
    #@is_owner('book')
    @validate_push(validator=validator_comments)
    @push('book')
    async def handle_push(document, request, payload):      
        return document

    @routes.put('/api/book/{_id}/pull/{pull}/{sub_id}')
    @jwt_auth
    #@is_owner('book')
    @pull('book')
    async def handle_pull(document, request, payload):      
        pass

    @routes.post('/api/book')
    @jwt_auth
    @validate(validator=validator)
    @insert('book')
    async def handle_post(document, request, payload):
        return document       

    @routes.put('/api/book/{_id}')
    @jwt_auth
    #@is_owner('book')
    @validate(update=True, validator=validator)
    @update('book')
    async def handle_put(document, request, payload):      
        return document    

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

apidecorators-0.0.3.tar.gz (3.1 kB view hashes)

Uploaded Source

Built Distribution

apidecorators-0.0.3-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

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