unrest

UnRest

UnRest(self,
       app=None,
       session=None,
       path='/api',
       version='',
       framework=None,
       idiom=None,
       SerializeClass=None,
       DeserializeClass=None,
       RestClass=Rest,
       allow_options=True,
       serve_openapi_file=True,
       OpenApiClass=OpenApi,
       OptionsClass=Options,
       empty_get_as_404=False,
       info={})

A troubling rest api library for sqlalchemy models. This is the main entry point of unrest.

Common usage is as following:

rest = UnRest(app, session)  # app is your application
# When called, it instanciate a `Rest` object that will register the
# REST endpoints. See the `Rest` class.
rest(Model1)
rest(Model2)

Arguments

  • app: Your web application, can be set afterwards using init_app.
  • session: Your sqlalchemy session, can be set afterwards using the init_session method.
  • path: Default '/api', sets the root url path for your endpoints.
  • version: Adds a version to the root url path if specified (i.e. /api/v2).
  • framework: A specific framework class, defaults to auto detect.
  • idiom: An idiom class, defaults to unrest.idiom.unrest.
  • SerializeClass: A global alternative for Serialize class.
  • DeserializeClass: A global alternative for Deserialize class.
  • RestClass: Replace the default Rest class.
  • allow_options: Set it to False to disable OPTIONS requests.
  • serve_openapi_file: Set it to False to disable openapi file generation.
  • empty_get_as_404: If True return a 404 on get with id not found.
  • info: Additional info for the openapi metadata.

Frameworks

Unrest aims to be framework agnostic. It currently works with Flask out of the box and provides some other frameworks: Tornado and python http.server. See Framework.

all

Return all supported methods. Useful for the rest method keyword argument.

root_path

Return this API root path.

Property

UnRest.Property(name, type=None, formatter=None)

A Property wrapper used instead of a string in a Rest properties parameter.

Arguments

  • name: This property name
  • type: The sqlalchemy type used for type coercion
  • formatter: An optional function to transform the parameter as string

RestError

UnRest.RestError(status, message, extra=None)

Exception raised by rest methods. It's catched by the REST method wrapper and will return a status http error with the specified message.

ValidationError

UnRest.ValidationError(message)

Exception raised by rest validation methods.

init_app

UnRest.init_app(app)

Sets the app on UnRest if it was missing during instantiation.

init_session

UnRest.init_session(session)

Sets the sqlalchemy session on UnRest if it was missing during instantiation.

raise_error

UnRest.raise_error(status, message, extra=None)

Raise an error that will be handled by the rest wrapper, which will return a json response with status as HTTP status code and message as content.

Arguments

  • status: The HTTP status code corresponding to the error (404 for instance).
  • message: The message that will be returned in the json response.
  • extra: Mapping of extra fields to return in json response.

register_index

UnRest.register_index()

Register the API index GET route.

index

UnRest.index(request)

The API index GET route.

send_json

UnRest.send_json(data)

Send data as json.

Arguments

  • data: An object to send as json.

Returns

The Response containing the json data.

register_options

UnRest.register_options()

Register the API index OPTIONS route.

options

UnRest.options(request)

The API index OPTIONS route.

register_openapi

UnRest.register_openapi()

Register the openapi route.

openapi

UnRest.openapi(request)

The API openapi route.

call

UnRest.__call__(*args, **kwargs)

Return a Rest instance. See rest entry points.