API ドキュメント

この文書はソースコードから自動生成された、KobinのAPIドキュメントです。

Kobinクラス

Kobinクラスのインスタンスが、WSGIのアプリケーションとなります。

使い方

from kobin import Kobin, Response
app = Kobin()

@app.route('/')
def index() -> Response:
    return Response('Hello World')
class kobin.app.Kobin(config=None)[ソース]

WSGI Applicationの実装です。インスタンスを生成して、WSGI Serverで実行しましょう。

kobin.app.current_config(key, default=None)[ソース]

Kobinアプリケーションの設定情報を取得

Request

When a page is requested, automatically created a Request object that contains metadata about the request. Since this object is global within the thread, you can freely import from anywhere and retrieve request information.

class kobin.requests.LocalRequest(environ=None)[ソース]
bind(environ=None)

Initialize self. See help(type(self)) for accurate signature.

environ

Thread-local property

class kobin.requests.Request(environ=None)[ソース]

A wrapper for WSGI environment dictionaries.

method

The REQUEST_METHOD value as an uppercase string.

path

The value of PATH_INFO with exactly one prefixed slash (to fix broken clients and avoid the "empty path" edge case).

kobin.requests.accept_best_match(accept_header, mimetypes)[ソース]

Return a mimetype best matched the accept headers.

>>> accept_best_match('application/json, text/html', ['application/json', 'text/plain'])
'application/json'
>>> accept_best_match('application/json;q=0.5, text/*', ['application/json', 'text/plain'])
'text/plain'

Response

In contrast to Request objects, which are created automatically, Response objects are your responsibility. Each view functions you write is responsible for instantiating and returning an Response or its child classes.

In addition to the Response class, Kobin provides TemplateResponse ,
JSONResponse , RedirectResponse and HTTPError.
class kobin.responses.BaseResponse(body=None, status=None, headers=None)[ソース]

Base class for Response.

headerlist

WSGI conform list of (header, value) tuples.

status

The HTTP status line as a string (e.g. 404 Not Found).

status_code

The HTTP status code as an integer (e.g. 404).

exception kobin.responses.HTTPError(body, status, headers=None, charset='utf-8')[ソース]

Return the error message when raise this class.

class kobin.responses.JSONResponse(dic, status=200, headers=None, charset='utf-8', **dump_args)[ソース]

Returns a HTML text from dict or OrderedDict.

class kobin.responses.RedirectResponse(url)[ソース]

Redirect the specified url.

class kobin.responses.Response(body='', status=None, headers=None, charset='utf-8')[ソース]

Returns a plain text from unicode object.

class kobin.responses.TemplateResponse(filename, status=200, headers=None, charset='utf-8', **tpl_args)[ソース]

Returns a html using jinja2 template engine

ルーティング

Kobinのルーティングシステムは少し特徴的かもしれません。

ルーティングの記述ルール

Kobin use decorator based URL dispatch.

  • Dynamic convert URL variables from Type Hints.
from kobin import Kobin, Response, RedirectResponse
app = Kobin()

@app.route('/')
def index() -> Response:
    return Response('Hello World')

@app.route('/users/{user_id}')
def index(user_id: str) -> Response:
    return Response('User List')

URLの逆引き

app.router.reverse function returns URL. The usage is like this:

from kobin import Kobin, Response
app = Kobin()

@app.route('/', 'top-page')
def index() -> Response:
    return Response('Hello World')

@app.route('/users/{user_id}', 'user-detail')
def user_detail(user_id: int) -> Response:
    return Response('Hello User{}'.format(user_id))

print(app.router.reverse('top-page'))
# http://hostname/

print(app.router.reverse('user-detail', user_id=1))
# http://hostname/users/1

Reverse Routing and Redirecting

RedirectResponse The usage is like this:

from kobin import Kobin, Response, RedirectResponse
app = Kobin()

@app.route('/', 'top-page')
def index() -> Response:
    return Response('Hello World')

@app.route('/404')
def user_detail() -> Response:
    top_url = app.router.reverse('top-page')
    return RedirectResponse(top_url)
kobin.routes.match_path(rule, path)[ソース]

Match path.

>>> match_path('/foo', '/foo')
(True, {})
>>> match_path('/foo', '/bar')
(False, {})
>>> match_path('/users/{user_id}', '/users/1')
(True, {'user_id': '1'})
>>> match_path('/users/{user_id}', '/users/not-integer')
(True, {'user_id': 'not-integer'})
kobin.routes.match_url_vars_type(url_vars, type_hints)[ソース]

Match types of url vars.

>>> match_url_vars_type({'user_id': '1'}, {'user_id': int})
(True, {'user_id': 1})
>>> match_url_vars_type({'user_id': 'foo'}, {'user_id': int})
(False, {})