API Star

A smart Web API framework, designed for Python 3.

Bahattin Çiniç

Senior Software Developer @adphorus
http://bahattincinic.com
https://twitter.com/bahattincinic
https://github.com/bahattincinic

What is Api Star?

  • Small Web Framework
  • API Star was developed by Tom Christie. Also he was developed Django Rest Framework
  • Only works on Python 3.5, 3.6

Why use Api Star ?

  • Easy to use / Minimal
  • Build high-throughput non-blocking applications
  • Takes advantage of Python 3
  • Automatic schema documentation

Features

  • Supports WSGI and ASyncIO backend.
  • API documentation
  • Schema generation with coreapi (Swagger, RAML...)
  • Supports Python type system. 
  • non-blocking (ASyncIO)
  • Template rendering (Jinja)
  • Renderers & Parsers (JSON, HTML...)
  • Settings & Environment
  • Authentication & Permissions & Session
  • DB Backends (Django ORM, SQLAlchemy)
  • Unit tests (pytest)
  • Custom commands (Django management command)

Quickstart

$ 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

Hello World

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()

Type System & Validation

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),
]

Template Rendering

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)

Settings & Environments


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)

Testing

$ 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"}

Project Structure

.
├── 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

Demo

Question ?

Api Star

By Bahattin Çiniç

Api Star

A smart Web API framework, designed for Python 3.

  • 1,313