API Decorators
Project description
API Decorators
# book.py
from apidecorators.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
@collection('book')
@get_many
async def get_many_book(db, query, payload):
author = query["author"]
return db.find({"author": author}, {'title': 1}).skip(0).limit(10)
#return {"author": author, "__owner": payload['user']}, ['author'], 0, 10
@routes.delete('/api/book/{_id}')
@jwt_auth
@collection('book')
@delete # TODO
async def delete_book(query):
pass
@routes.get('/api/book/{_id}')
@jwt_auth
@collection('book')
@read_access(public)
@get
async def get_book(document):
return document
@routes.put('/api/book/{_id}/push/comments')
@jwt_auth
@collection('book')
@write_access({'__owner': ['*'], 'root': ['author']})
@validate(validator=validator_comments)
@push('comments')
async def handle_push(document, request, payload):
return document
# TODO permissions, quizas con un argumento array de usuarios en @pull
@routes.put('/api/book/{_id}/pull/{pull}/{sub_id}')
@jwt_auth
@pull
async def handle_pull(document, request, payload):
pass
@routes.post('/api/book')
@jwt_auth
@collection('book')
@write_access(public)
@validate(validator=validator)
@insert
async def handle_post(document, request, payload):
return document
@routes.put('/api/book/{_id}')
@jwt_auth
@collection('book')
@write_access({'__owner': ['*'], 'root': ['author']})
@validate(update=True, validator=validator)
@update
async def handle_put(document, request, payload):
return document
#app.py
import asyncio
from book import set_routes_book
from aiohttp import web
from apidecorators.api import cors_factory
from apidecorators.hooks import on_insert, on_update
@on_insert('book')
def on_insert_book(book):
print('insert', book)
@on_update('book')
def on_update_book(book):
print('update', book)
async def handle(loop):
app = web.Application(loop=loop, middlewares=[cors_factory])
routes = web.RouteTableDef()
set_routes_book(routes)
app.router.add_routes(routes)
app.router.add_static('/', './dist')
await loop.create_server(app.make_handler(), '0.0.0.0', 8089)
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(handle(loop))
print("Server started at port 8089")
loop.run_forever()
loop.close()
docker-compose.yml
version: '3'
services:
api:
build: .
environment:
- DB_URI=mongodb://<user>:<password>@url:port/data-base
- DB=data-base
- SECRET=secret
stdin_open: true
tty: true
ports:
- "8089:8089"
volumes:
- ./:/usr/src/app
command: python -m watchgod app.main
Project details
Release history Release notifications | RSS feed
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.8.tar.gz
(4.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apidecorators-0.0.8.tar.gz.
File metadata
- Download URL: apidecorators-0.0.8.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3cddb9a2cd0b22fe7a553596319f24fbf3e07edf1c4d266439cee99013edb470
|
|
| MD5 |
343cacfee72b10327df634cb611b04fa
|
|
| BLAKE2b-256 |
73ef3a7dda2e2c2c1d39bffd83973e8f3904f7cdc0d688866635ab0d491037ec
|
File details
Details for the file apidecorators-0.0.8-py3-none-any.whl.
File metadata
- Download URL: apidecorators-0.0.8-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
886e87cd5f9650c9df74617ab1b02ca3eba81936367aff4b9b222047a1563899
|
|
| MD5 |
3ec1e5384aa60cef617624cb9d56d45d
|
|
| BLAKE2b-256 |
d26d43cd98c0cf842ead7b67111ad1cb6a2fba59dda24af48d05a37290741cce
|