kristall package

Submodules

kristall.application module

class kristall.application.Application

Bases: object

Wrapper over WSGI application. This class provides simple route registration mechanism that allows to match code execution to request paths. It’s the central point of an app but does not provide much more than that.

Possible customizations include custom JSON encoder and decoder classes and possibility to specify custom request and response classes.

Variables
  • json_encoder – JSON encoder class, defaults to JSONEncoder

  • json_decoder – JSON decoder class, defaults to JSONDecoder

  • request_class – request encapsulation class, defaults to Request

  • response_class – response encapsulation class, defaults to Response

METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS']
add_after_request(func: Callable)

Add function to list of callables called after handling request. These functions should accept request and handler result, and return either None or thing that will be returned from dispatch.

Parameters

func (Callable) – callable to be called after request

add_before_request(func: Callable)

Add function to list of callables called before handling request. These functions should accept request object as parameter. These functions should either return None or instance of Response that will be returned as result of dispatch.

Parameters

func (Callable) – callable to be called before request

add_resource(path: str, resource: object)

Register resource under specified path. The resource is an instance that is expected to provide methods that correspond to HTTP words. If the instance provides endpoint attribute the it will be used as endpoint name, otherwise it will be registered under the name that is it’s class dotted path.

Parameters
  • path (str) – HTTP path which resource serves

  • resource (object) – resource instance

dispatch(request: kristall.request.Request) kristall.response.Response

Dispatch and service HTTP request.

Parameters

request (Request) – request wrapper

Raises

MethodNotAllowed – if resource that is registered for specified path does not support HTTP method

Returns

response that is either result of request handler or an error

Return type

Response

json_decoder

alias of json.decoder.JSONDecoder

json_encoder

alias of json.encoder.JSONEncoder

request_class

alias of kristall.request.Request

response_class

alias of kristall.response.Response

wsgi_app(environ: dict, start_response: Callable) kristall.response.Response

Wrapper over WSGI application that exposes WSGI application for further wrapping with WSGI middleware.

Parameters
  • environ (dict) – WSGI environ

  • start_response (Callable) – callable that is used to begin writing response

Returns

response object

Return type

Response

kristall.request module

class kristall.request.Request(environ: dict, populate_request: bool = True, shallow: bool = False, json_decoder: Optional[Type] = None)

Bases: werkzeug.wrappers.request.Request

Wrapper over Request that has built in support for JSON content. Maximum content length is set to 4 megabytes.

MAX_CONTENT_LENGTH = 4194304
environ: WSGIEnvironment

The WSGI environment containing HTTP headers and information from the WSGI server.

get_data(cache: bool = True, as_text: bool = True, parse_form_data: bool = False) Union[str, bytes]

Overwritten method that retrieves request data. Difference is that by default it fetches data as text. For complete description of call arguments see Werkzeug documentation for get_data(). This method raises RequestEntityTooLarge if content length exceeds allowed size (default is 4 megabytes).

get_json(decoder: Optional[Type] = None) dict

A method to retrieve JSON from request data, optionally using specified JSON decoder class. If not provided default decoder class is used.

Parameters

decoder (Optional[Type], optional) – JSON decoder class, defaults to None

Returns

request data as parsed JSON

Return type

dict

shallow: bool

Set when creating the request object. If True, reading from the request body will cause a RuntimeException. Useful to prevent modifying the stream from middleware.

kristall.response module

class kristall.response.Response(response: Optional[Union[Iterable[bytes], bytes, Iterable[str], str]] = None, status: Optional[Union[int, str, http.HTTPStatus]] = None, headers: Optional[Union[Mapping[str, Union[str, int, Iterable[Union[str, int]]]], Iterable[Tuple[str, Union[str, int]]]]] = None, mimetype: Optional[str] = None, content_type: Optional[str] = None, direct_passthrough: bool = False)

Bases: werkzeug.wrappers.response.Response

Thin wrapper over Response. The only difference is predefined response content type which is set to application/json.

default_mimetype = 'application/json'

the default mimetype if none is provided.

response: Union[Iterable[str], Iterable[bytes]]

The response body to send as the WSGI iterable. A list of strings or bytes represents a fixed-length response, any other iterable is a streaming response. Strings are encoded to bytes as UTF-8.

Do not set to a plain string or bytes, that will cause sending the response to be very inefficient as it will iterate one byte at a time.

kristall.utils module

kristall.utils.endpoint(item: Union[object, Type]) str

Endpoint generation function. Returns fully qualified class name in dotted notation.

Parameters

item (Union[object, Type]) – item to generate endpoint for, may be either instance or class

Returns

fully qualified class name, suitable for use as endpoint name

Return type

str

Module contents