Metadata-Version: 2.1
Name: cocktail-apikit
Version: 0.0.5
Summary: A collection of tools for APIs
Home-page: http://gitlab.com/theo-l/cocktail_apikit
Author: Liang Guisheng
Author-email: theol.liang@truckpad.com.br
License: MIT
Keywords: cocktail_apikit backend apikit
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Description-Content-Type: text/markdown
Requires-Dist: apispec (==0.39.0)
Requires-Dist: boto3 (==1.9.115)
Requires-Dist: botocore (==1.12.115)
Requires-Dist: bottle (==0.12.16)
Requires-Dist: docutils (==0.14)
Requires-Dist: jmespath (==0.9.4)
Requires-Dist: marshmallow (==2.16.3)
Requires-Dist: pymongo (==3.7.2)
Requires-Dist: pyyaml (==5.1)
Requires-Dist: s3transfer (==0.2.0)
Requires-Dist: six (==1.12.0)
Requires-Dist: python-dateutil (==2.8.0) ; python_version >= "2.7"
Requires-Dist: urllib3 (==1.24.1) ; python_version >= "3.4"
Provides-Extra: dev
Requires-Dist: astroid (==2.2.5) ; extra == 'dev'
Requires-Dist: atomicwrites (==1.3.0) ; extra == 'dev'
Requires-Dist: attrs (==19.1.0) ; extra == 'dev'
Requires-Dist: autopep8 (==1.4.3) ; extra == 'dev'
Requires-Dist: beautifulsoup4 (==4.7.1) ; extra == 'dev'
Requires-Dist: isort (==4.3.15) ; extra == 'dev'
Requires-Dist: lazy-object-proxy (==1.3.1) ; extra == 'dev'
Requires-Dist: mccabe (==0.6.1) ; extra == 'dev'
Requires-Dist: pluggy (==0.9.0) ; extra == 'dev'
Requires-Dist: py (==1.8.0) ; extra == 'dev'
Requires-Dist: pycodestyle (==2.5.0) ; extra == 'dev'
Requires-Dist: pylint (==2.3.1) ; extra == 'dev'
Requires-Dist: pytest (==4.3.1) ; extra == 'dev'
Requires-Dist: rope (==0.12.0) ; extra == 'dev'
Requires-Dist: six (==1.12.0) ; extra == 'dev'
Requires-Dist: soupsieve (==1.8) ; extra == 'dev'
Requires-Dist: waitress (==1.2.1) ; extra == 'dev'
Requires-Dist: webob (==1.8.5) ; extra == 'dev'
Requires-Dist: webtest (==2.0.33) ; extra == 'dev'
Requires-Dist: wrapt (==1.11.1) ; extra == 'dev'
Requires-Dist: typed-ast (==1.3.1) ; (implementation_name == "cpython") and extra == 'dev'
Requires-Dist: more-itertools (==6.0.0) ; (python_version > "2.7") and extra == 'dev'

# 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

```python

### 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'))
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

```




