Bahattin Çiniç
Software Developer
A smart Web API framework, designed for Python 3.
Senior Software Developer @adphorus
http://bahattincinic.com
https://twitter.com/bahattincinic https://github.com/bahattincinic
$ pip3 install apistar
$ apistar new .
app.py
tests.py
$ cat app.py
installation via pip
Create a new project
$ apistar run
Run the application
$ apistar test
Run the tests
from apistar import Include, Route
from apistar.frameworks.wsgi import WSGIApp as App
from apistar.handlers import docs_urls, static_urls
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_urls),
Include('/static', static_urls)
]
app = App(routes=routes)
if __name__ == '__main__':
app.main()
from apistar import Route, typesystem
class Blog(typesystem.Object):
properties = {
'name': typesystem.string(max_length=100),
'text': typesystem.string(),
'is_active': typesystem.Boolean,
}
required = ['name', 'test', 'is_active']
def create_blog(blog: Blog) -> Blog:
return Blog
routes = [
Route('/blogs', 'POST', create_blog),
]
from apistar import Route, annotate, render_template
from apistar.frameworks.wsgi import WSGIApp as App
from apistar.renderers import HTMLRenderer
@annotate(renderers=[HTMLRenderer()])
def hello(username: str):
return render_template('index.html', username=username)
routes = [
Route('/', 'GET', hello)
]
settings = {
'TEMPLATES': {
'ROOT_DIR': 'templates', # Include the 'templates/' directory.
'PACKAGE_DIRS': ['apistar'] # Include the built-in apistar templates.
}
}
app = App(routes=routes, settings=settings)
def debug_settings(settings: Settings):
return {'debug': settings.DEBUG, 'domain': settings.DOMAIN}
class Env(environment.Environment):
properties = {
'DEBUG': typesystem.boolean(default=False)
}
env = Env()
settings = {
'DOMAIN': 'http://google.com',
'DEBUG': env['DEBUG']
}
app = App(routes=routes, settings=settings)
$ apistar test
from app import app
from apistar import TestClient
def test_hello_world():
client = TestClient(app)
response = client.get('/hello_world/')
assert response.status_code == 200
assert response.json() == {"hello": "world"}
.
├── README.md
├── app.py
├── docs
│ ├── API_DOCUMENTATION.md
│ ├── INSTALLATION.md
│ ├── MYPY.md
│ ├── UNIT_TESTS.md
├── pytest.ini
├── requirements
│ ├── base.txt
│ └── test.txt
├── runtests.py
├── secretmarks
│ ├── __init__.py
│ ├── authentication.py
│ ├── encryption.py
│ ├── models.py
│ ├── routes.py
│ ├── schemas
│ │ ├── __init__.py
│ │ ├── entity.py
│ │ ├── fields.py
│ │ └── validators.py
│ ├── settings.py
│ └── views.py
└── tests.py
By Bahattin Çiniç
A smart Web API framework, designed for Python 3.