API Documentation

This documentations is generated from kobin’s source code.

Kobin class

The Kobin instance are callable WSGI Application.

Usage

from kobin import Kobin, Response
app = Kobin()

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

This class is a WSGI application implementation. Create a instance, and run using WSGI Server.

kobin.app.current_config(key, default=None)[source]

Get the configurations of your Kobin’s application.

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)[source]
bind(environ=None)

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

environ

Thread-local property

class kobin.requests.Request(environ=None)[source]

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)[source]

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)[source]

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')[source]

Return the error message when raise this class.

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

Returns a HTML text from dict or OrderedDict.

class kobin.responses.RedirectResponse(url)[source]

Redirect the specified url.

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

Returns a plain text from unicode object.

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

Returns a html using jinja2 template engine

Routing

Kobin’s routing system may be slightly distinctive.

Rule Syntax

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

Reverse Routing

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)[source]

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)[source]

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, {})