A collection of tools for APIs
Project description
Cocktail ApiKit
A collection of tools which will be used in all new API project, which including: Bottle, marshmallow, mongo and aws
Usage Example
1. Install cocktail apikit
pip install cocktail-apikit
2. Create a demo project
### 1. create a global scope configuration file "settings.py"
from cocktail_apikit import DefaultSettings
class Settings(DefaultSettings):
# specify configuration file names to load configuration from file
# Be aware, any configuration fields in configuration file should be
# declare in the settings class or any its super class, just
# to make us have better IDE auto-complete help
_config_files = ['config/database.ini']
### 2. create an application file 'application.py' included api endpoints
import uuid
from settings import Settings
from bottle import request, default_app
from marshmallow import fields
from cocktail_apikit import (
ResourcePlugin, FlexibleJsonPlugin, route_mark, BaseSchema, ValidationError, APP_ERROR_HANDLER, MongoDBManager, CorsPlugin, enable_cors,
BottleMongoQueryBuilder, Pagination
)
class DemoSchema(BaseSchema):
name = fields.Str()
demo_db = MongoDBManager(Settings.mongo_db_config('demo')) # specify a Config option name or be the given name
demo_schema = DemoSchema()
class DemoResource(ResourcePlugin):
# a simple demo endpoint
@route_mark('/index')
def index(self):
return 'hello cocktail apikit'
@route_mark('/demos')
@enable_cors # allow cors for endpoint
def list_demo(self):
mongo_query_builder = BottleMongoQueryBuilder(request, demo_schema)
mongo_query = mongo_query_builder.to_mongo_query()
results, count = demo_db.filter(mongo_query)
pagination = Pagination(mongo_query, results, count)
return pagination.serialize(demo_schema)
@route_mark('/demos', 'POST')
def create_demo(self):
payload = request.json
cleaned_obj, errors = demo_schema.load(payload)
if errors:
raise ValidationError(errors)
created_ids, errors = demo_db.create(cleaned_obj)
if errors:
raise ValidationError(errors)
return {
"ids": created_ids
}
app = default_app()
app.install(FlexibleJsonPlugin())
app.install(DemoResource())
app.install(CorsPlugin())
#config application object's error handlers
app.error_handler = APP_ERROR_HANDLER
if __name__ == "__main__":
app.run(port=8000, debug=True, reloader=True)
### 3 Then we can run 'python application.py', and access
# GET http://localhost:8000/demos: fetch all demos
# POST http://localhost:8000/demos: crate demo
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
cocktail-apikit-0.0.6.tar.gz
(14.9 kB
view hashes)
Built Distribution
Close
Hashes for cocktail_apikit-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e051e5389730877cd2b2477cf68fe906c29a098ff7b388c1764c34c1822ab021 |
|
MD5 | 17a0f3b3967f9353e6f1a62f0bae4cbc |
|
BLAKE2b-256 | df583db6311fe484f04d3a2fe192c92ccfd1023675c3067623a51fcb7be3c092 |