A simple toolkit for building microservices with flask
Project description
Cruet
Cruet is a simple toolkit for building microservices with flask. It works with both, function based and class based views.
It is built on top of:
- flask
- marshmallow
- webargs
Example
from flask import Flask
from marshmallow import fields
from cruet import Cruet, use_args, ApiError, ApiResponse
app = Flask(__name__)
cruet = Cruet(app)
@app.route('/')
@use_args({'name': fields.Str(required=True)})
def index(args):
if len(args['name']) < 3:
raise ApiError('Name has to be at least 3 characters', 400)
return ApiResponse({'msg': 'Hello ' + args['name']})
if __name__ == '__main__':
app.run()
# curl http://localhost:5000/?name='World'
# {"data": {"msg": "Hello World"}}
Installation
Cruet is available from PyPi
pip install cruet
Response
Cruet provides the ApiResponse class for building json responses that are formatted consistently across all endpoints.
Error Handling
Cruet provides the ApiError exception class for raising an exception and returning a formatted json response with the error message.
Input Validation
Cruet uses the excellent webargs library for validating HTTP requests. It integrates the library in a very transparent way, which doesn't change the way webargs is being used and their documentation should be used as the reference. Cruet only adds its own error handling to webargs and provides convenient decorators for different HTTP request input types.
For general use, the use_args and use_kwargs decorators can be used. The example above uses use_args to check for a parameter called name. The webargs library uses the concept of locations to define where it should look for the HTTP request data.
Cruet provides a few convenient decorators for the most common locations.
Query
The query decorator looks for the data as part of the arguments in the URL query string.
from cruet import query
@app.route('/')
@query({'name': fields.Str(required=True)})
def index(args):
if len(args['name']) < 3:
raise ApiError('Name has to be at least 3 characters', 400)
return ApiResponse({'msg': 'Hello ' + args["name"]})
# curl http://localhost:5000/?name='World'
# {"data": {"msg": "Hello World"}}
Body
The body decorator looks for json formatted data in the request body.
from cruet import body
@app.route('/', methods=['POST'])
@body({'name': fields.Str(required=True)})
def index(args):
if len(args['name']) < 3:
raise ApiError('Name has to be at least 3 characters', 400)
return ApiResponse({'msg': 'Hello ' + args["name"]})
# curl -d '{"name":"World"}' -H "Content-Type: application/json" -X POST http://localhost:5000
# {"data": {"msg": "Hello World"}}
Form
The form decorator looks for form-urlencoded data.
from cruet import form
@app.route('/', methods=['POST'])
@form({'name': fields.Str(required=True)})
def index(args):
if len(args['name']) < 3:
raise ApiError('Name has to be at least 3 characters', 400)
return ApiResponse({'msg': 'Hello ' + args["name"]})
# curl -d "name=World" -X POST http://localhost:5000
# {"data": {"msg": "Hello World"}}
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
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 cruet-0.0.3.tar.gz.
File metadata
- Download URL: cruet-0.0.3.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
553ef0dfc0ef2f0e8f84b04c142deb89fa8bd5df1ac4368031a959dc20fa4d2c
|
|
| MD5 |
d4c9a67f2e352310e009161101c61441
|
|
| BLAKE2b-256 |
d31de4e3d16a1cd1bb7e5c7a3973f271460854f7e6d7323c42306e2173ad5d3e
|
File details
Details for the file cruet-0.0.3-py3-none-any.whl.
File metadata
- Download URL: cruet-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2310011e3541dd35fcaac9ed3097f812f7e882a2c1383c6672e80951ba139b4
|
|
| MD5 |
533b28e22c0fd5668524cdc0c19f1dfe
|
|
| BLAKE2b-256 |
0e2a3b99b05f67ae3c67688491fab4bd27d3064b4672b00d8b216656ac4f59d7
|