Rafter API¶
rafter.app¶
-
class
Rafter
(**kwargs)¶ Bases:
sanic.app.Sanic
This class inherits Sanic’s default
Sanic
class. It provides an instance of your app, to which you can add resources or regular Sanic routes.Note
Please refer to Sanic API reference for init arguments.
-
default_filters
= [<function filter_transform_response>]¶ Default filters called on every resource route.
-
default_error_handlers
= ((<class 'rafter.exceptions.ApiError'>, <rafter.exceptions.ApiErrorHandler object>), (<class 'sanic.exceptions.SanicException'>, <rafter.exceptions.SanicExceptionHandler object>), (<class 'Exception'>, <rafter.exceptions.ExceptionHandler object>))¶ Default error handlers. It must be a list of tuples containing the exception type and a callable.
-
default_request_class
= <class 'rafter.http.Request'>¶ The default request class. If changed, it must inherit from
rafter.http.Request
.
-
add_resource
(handler, uri, methods=frozenset({'GET'}), **kwargs)¶ Register a resource route.
Parameters: - handler – function or class instance
- uri – path of the URL
- methods – list or tuple of methods allowed
- host –
- strict_slashes –
- version –
- name – user defined route name for url_for
- filters – List of callable that will filter request and response data
- validators – List of callable added to the filter list.
Returns: function or class instance
-
resource
(uri, methods=frozenset({'GET'}), **kwargs)¶ Decorates a function to be registered as a resource route.
Parameters: - uri – path of the URL
- methods – list or tuple of methods allowed
- host –
- strict_slashes –
- stream –
- version –
- name – user defined route name for url_for
- filters – List of callable that will filter request and response data
- validators – List of callable added to the filter list.
Returns: A decorated function
-
rafter.blueprints¶
-
class
Blueprint
(*args, **kwargs)¶ Bases:
sanic.blueprints.Blueprint
Create a new blueprint.
Parameters: - name – unique name of the blueprint
- url_prefix – URL to be prefixed before all route URLs
- strict_slashes – strict to trailing slash
-
add_resource
(handler, uri, methods=frozenset({'GET'}), host=None, strict_slashes=None, version=None, name=None, **kwargs)¶ Create a blueprint resource route from a function.
Parameters: - uri – endpoint at which the route will be accessible.
- methods – list of acceptable HTTP methods.
- host –
- strict_slashes –
- version –
- name – user defined route name for url_for
Returns: function or class instance
Accepts any keyword argument that will be passed to the app resource.
-
resource
(uri, methods=frozenset({'GET'}), host=None, strict_slashes=None, stream=False, version=None, name=None, **kwargs)¶ Create a blueprint resource route from a decorated function.
Parameters: - uri – endpoint at which the route will be accessible.
- methods – list of acceptable HTTP methods.
- host –
- strict_slashes –
- version –
- name – user defined route name for url_for
Returns: function or class instance
Accepts any keyword argument that will be passed to the app resource.
rafter.exceptions¶
Exceptions¶
-
exception
ApiError
(message: str, status_code: int = 500, **kwargs)¶ Bases:
sanic.exceptions.SanicException
-
data
¶ Returns the internal property
_data
. It can be overriden by specialized inherited exceptions.
-
to_primitive
() → dict¶ This methods is called by the error handler
ApiErrorHandler
and returns a dict of error data.
-
Error Handlers¶
-
class
ExceptionHandler
¶ Bases:
object
A generic
Exception
handler.The callable returns a JSON response with structured error data. The original error message is never returned. Use any type of SanicException if you need to do so.
-
__call__
(request, exception)¶ If the data’s status is superior or equal to 500, the exception is logged, and if the Rafter app runs in debug mode, the statck trace is also returned in the response.
-
-
class
SanicExceptionHandler
¶ Bases:
rafter.exceptions.ExceptionHandler
SanicException
handler.This handler returns the original error message in its data.
-
class
ApiErrorHandler
¶ Bases:
rafter.exceptions.SanicExceptionHandler
ApiError
handler.This handler returns all error data returned by
ApiError.to_primitive()
.
rafter.filters¶
-
filter_transform_response
(get_response, params)¶ This filter process the returned response. It does 3 things:
- If the response is a
sanic.response.HTTPResponse
and not arafter.http.Response
, return it immediately. - If the response is not a
rafter.http.Response
instance, turn it to arafter.http.Response
instance with the response as data. - Then, return the Response instance.
As the Response instance is not immediately serialized, you can still validate its data without any serialization / de-serialization penalty.
- If the response is a
rafter.http¶
-
class
Request
(*args, **kwargs)¶ Bases:
sanic.request.Request
This class is the default
rafter.app.Rafter
’s request object that will be transmitted to every route. It adds avalidated
attribute that will contains all of the validated values, if the route uses schemas.-
validated
¶ This property can contain the request data after validation and conversion by the filter.
-
-
class
Response
(body=None, status=200, headers=None, content_type='application/json')¶ Bases:
sanic.response.HTTPResponse
A response object that you can return in any route. It looks a lot like
sanic.response.json
function except that instead of immediately serialize the data, it just keeps the value. Serialization (to JSON) will only happen when the response’s body is retrieved.Example:
@app.resource('/') def main_route(request): return Response({'data': 'some data'})