A smart WEB API framework designed for Python 3
$ pip3 install apistar
$ apistar new --layout minimal
Β
.
βββ __pycache__
β βββ app.cpython-36.pyc
β βββ tests.cpython-36.pyc
βββ app.py
βββ tests.py
1 directory, 4 files
Β
.
βββ __pycache__
βΒ Β βββ app.cpython-36.pyc
βββ app.py
βββ project
βΒ Β βββ __init__.py
βΒ Β βββ __pycache__
βΒ Β βΒ Β βββ __init__.cpython-36.pyc
βΒ Β βΒ Β βββ routes.cpython-36.pyc
βΒ Β βΒ Β βββ views.cpython-36.pyc
βΒ Β βββ routes.py
βΒ Β βββ views.py
βββ tests
βββ __pycache__
βΒ Β βββ test_app.cpython-36.pyc
βββ test_app.py
5 directories, 10 files
Β
Minimal Layout
from apistar.test import TestClient
from app import welcome
def test_welcome():
"""
Testing a view directly.
"""
data = welcome()
assert data == {
'message': 'Welcome to API Star!'
}
def test_http_request():
"""
Testing a view, using the test client.
"""
client = TestClient()
response = client.get('http://localhost/')
assert response.status_code == 200
assert response.json() == {
'message': 'Welcome to API Star!'
}
$ apistar test
============================= test session starts =============================
platform darwin -- Python 3.6.0, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /Users/anthonyfox/dev/pynash/api, inifile:
collected 2 items
tests/test_app.py ..
========================== 2 passed in 0.05 seconds ===========================
$ apistar run
Starting up...
* Running on http://localhost:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 292-929-856
Β
Minimal Layout
from apistar import App, Include, Route
from apistar.docs import docs_routes
from apistar.statics import static_routes
def welcome(name=None):
if name is None:
return {
'message': 'Welcome to API Star!'
}
return {
'message': 'Welcome to API Star, %s!' % name
}
routes = [
Route('/', 'GET', welcome),
Include('/docs', docs_routes),
Include('/static', static_routes)
]
app = App(routes=routes)
Views are just functions that handle data
Parameters are automatically passed into views from routes
def get_humidity(city=None):
if not city:
# return error
status = 'Humidity in {} is {}'.format(city, 84)
return {
'status': status
}
def get_humidity(city=None) -> JsonResponse:
if not city:
# return error
status = 'Humidity in {} is {}'.format(city, 84)
return JsonResponse({
'status': status
})
# Original View
def get_humidity(city=None):
if not city:
# return error
status = 'Humidity in {} is {}'.format(city, 84)
return {
'status': status
}
class City(schema.Object):
properties = {
'name': schema.String(max_length=100),
}
def get_humidity(city: City) -> JsonResponse:
status = 'Humidity in {} is {}'.format(city, 84)
return JsonResponse({
'status': status
})
# Original View
def get_humidity(city=None):
if not city:
# return error
status = 'Humidity in {} is {}'.format(city, 84)
return {
'status': status
}
class City(schema.Object):
properties = {
'name': schema.String(max_length=100),
}
def get_humidity(city: City) -> JsonResponse:
return JsonResponse({
'status': f'Humidity in {city} is {84}'
})
# Original View
def get_humidity(city=None):
if not city:
# return error
status = 'Humidity in {} is {}'.format(city, 84)
return {
'status': status
}
todo_items = ToDoTable.objects.all()
class ToDoTitle(schema.String):
min_length=1
def list_todo_items() -> schema.List[ToDo]:
return [ToDo(item) for item in todo_items]
def get_todo(todo_id: int) -> ToDo:
...
def add_todo(todo: ToDo) -> ToDo:
...
def delete_todo(todo_id: int) -> ToDo:
...
routes = [
Route('/todos', 'GET', list_todo_items),
Route('/todos', 'POST', add_todo),
Route('/todos/{todo_id}/', 'GET', get_todo),
Route('/todos/{todo_id}/', 'DELETE', delete_todo),
]
on a 2013 MacBook Air
Framework | Config | Reqs/sec | Avg Lat. |
---|---|---|---|
API Star | gunicorn + meinheld | 25,195 | 7.94ms |
Sanic | uvloop | 21,233 | 10.19ms |
Falcon | gunicorn + meinheld | 16,692 | 12.08ms |
Flask | gunicorn + meinheld | 5,238 | 38.28ms |