No project description provided
Project description
Falibrary
Falcon add-on for API specification and validation.
Provide OpenAPI document and validation for flask service.
Mainly built for Machine Learning Model services.
If you're using Flask, check my another Python library Flaskerk.
Features
- Generate API document with Redoc UI or Swagger UI :yum:
- Less boilerplate code, annotations are really easy-to-use :sparkles:
- Validate query, JSON data, response data with pydantic :wink:
- Better HTTP exceptions for API services (default & customized) (JSON instead of HTML) :grimacing:
Quick Start
Install with pip install falibrary
(Python 3.6+)
Basic example
import falcon
from wsgiref import simple_server
from pydantic import BaseModel
from falibrary import Falibrary
api = Falibrary(
title='Demo Service',
version='0.1.2',
)
class Query(BaseModel):
text: str
class Demo():
@api.validate(query=Query)
def on_post(self, req, resp):
print(req.context.query)
pass
if __name__ == '__main__':
app = falcon.API()
app.add_route('/api/demo', Demo())
api.register(app)
httpd = simple_server.make_server('localhost', 8000, app)
httpd.serve_forever()
Changes you need to make:
- create model with pydantic
- decorate the route function with
Falibrary.validate()
- specify which part you need in
validate
query
(args in url)- builtin converters (int, uuid, dt)
data
(JSON data from request)resp
(response) this will be transformed to JSON data after validationx
(HTTP Exceptions list)
- register to Falcon application
After that, this library will help you validate the incoming request and provide API document in /apidoc
.
For more details, check the document.
More features
import falcon
from wsgiref import simple_server
from pydantic import BaseModel, Schema
from random import random
from falibrary import Falibrary
api = Falibrary(
title='Demo Service',
version='0.1.2',
)
class Query(BaseModel):
text: str = Schema()
class Response(BaseModel):
label: int
score: float = Schema(
...,
gt=0,
lt=1,
)
class Data(BaseModel):
uid: str
limit: int
vip: bool
class Classification():
@api.validate(query=Query, data=Data, resp=Response, x=[falcon.HTTP_403])
def on_post(self, req, resp, source, target):
print(f'{source} => {target}')
print(req.context.query)
print(req.context.data)
if random() < 0.5:
raise falcon.HTTPForbidden("Bad luck. You're fobidden.")
return Response(label=int(10 * random()), score=random())
if __name__ == '__main__':
app = falcon.API()
app.add_route('/api/{source}/{target}', Classification())
api.register(app)
httpd = simple_server.make_server('localhost', 8000, app)
httpd.serve_forever()
Try it with http POST ':8000/api/zh/en?text=hello' uid=0b01001001 limit=5 vip=true
.
Open the docs in http://127.0.0.1:8000/apidoc .
For more examples, check examples.
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
File details
Details for the file falibrary-0.3.8.tar.gz
.
File metadata
- Download URL: falibrary-0.3.8.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43585eceae9385a83b8f1d1b689d8279bb4138f0a711f786506c548b98a8e8c1 |
|
MD5 | efd04057726d13a0fc13e1237fa662f3 |
|
BLAKE2b-256 | 44412068710fb5a90d75c0f4169c15f857474757002dbd1776661b565074cfc9 |
File details
Details for the file falibrary-0.3.8-py3-none-any.whl
.
File metadata
- Download URL: falibrary-0.3.8-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97720e64726d6f3af5aec4d81d74b4501665253ccf40cedb363cf0f6430bc75d |
|
MD5 | 9493a8709df29cdbc7d5638e8c445f4a |
|
BLAKE2b-256 | eb99104bcc71e4fb84e084ed8edb164d8ed97bd770bb263ea30fa13e2942589c |