unrest.idiom

Idiom

Idiom(self, rest)

UnRest Idiom abstract class.

Idioms are a way to alter how UnRest speaks with the rest of the world. The default implementation UnRestIdiom parses json from request and generates json from response.

To implement an idiom you have to implement the request_to_payload and the data_to_response methods described below.

You can also override the alter_query method to add functionality to the idiom based on the request. (i.e. implementing sort / filter query parameters)

Arguments

  • rest: The current rest instance

request_to_payload

Idiom.request_to_payload(request)

This method takes a Request request parameter and returns a payload dict.

Arguments

Returns

A dict containing the request payload data.

data_to_response

Idiom.data_to_response(data, request, status=200)

This method takes a data dict returned by the route and return a Response object.

Arguments

  • data: The data dict returned by the route
  • request: The original Request request
  • status: The current status (500 if route raised an error)

Returns

An Response response object.

alter_query

Idiom.alter_query(request, query)

This method takes the request and the current query and returns a modified query from the request.

Arguments

  • data: The data dict returned by the route
  • request: The original Request request
  • status: The current status (500 if route raised an error)

Returns

The modified query.

unrest.idiom.unrest

UnRestIdiom

UnRestIdiom(self, rest)

The default UnRest implementation.

Parses request payload as json. Serialize data as json. Can return a 404 on empty GET if empty_get_as_404 is set as True in the Unrest instance.

unrest.idiom.yaml

YamlIdiom

YamlIdiom(self, rest)

This is a very basic yaml implementation.

It does the same thing as the UnRestIdiom but with yaml instead of json.

Requires pyyaml to be installed.

unrest.idiom.json_server

JsonServerIdiom

JsonServerIdiom(self, rest)

The JSON Server idiom implementation.

Currently support all basic queries and first-level (no nested props) filter, sort, pagination, slice, operators (_gte, _lte, _ne, _like) and q full-text search (which works better with SQLAlchemy-Searchable)