Middlewares

the Rest Models Middlewares work like the django ones. it intercept all query that will be sent to the api, and can update the params, or bypass the api and return a result.

class rest_models.backend.middlewares.ApiMiddleware

a base class to implemente a middleware that will interact with a api query/response

data_response(data, status_code=None)

shortcut to return a response with 200 and data

Parameters:data – the data to insert in the response
Returns:a FakeApiResponse with the given data
empty_response()

shortcut to return a response with 204 status code and no data, which will be

Returns:a FakeApiResponse with no data and 204 status code
static make_response(data, status_code)

helper to make a response (returned by process_response or process_request) with given data

Parameters:
  • data – the data in the response (will be encoded in json to be compatible)
  • status_code – the status code of the response
Returns:

a FakeApiResponse that contains raw data

process_request(params, requestid, connection)

process the request. if return other than None, it will be the result of the request

Parameters:
  • params (dict) –
    the params given (and modified by previous middleware) for the _make_query.
    the params can be updated at will by side-effect.
    params contain
    • verb: the verb to execut the request (post, get, put)
    • url: the url in which the query will be executed
    • data: the data given to the query to post,put, etc
  • requestid (int) – the id of the current request done by this connection
  • connection – the connection used for this query
Returns:

the response if there is no need to pursue the query.

process_response(params, response, requestid)

process the response from a previous query. MUST return a response (result)

Parameters:
  • params – the params finaly given to query the api. same format as for process_request
  • response – the response, either the original one or modifier by preceding middleware
  • requestid (int) – the id of the current request done by this connection
Returns:

helper Middleware

class rest_models.test.PrintQueryMiddleware(stream=<colorama.ansitowin32.StreamWrapper object>, format_=None)

a middleware that print all intercepted query in a format usable for tests fixtures.

in settings:

DATABASES['api']['MIDDLEWARES'].append(
    'rest_models.test.PrintQueryMiddleware',
)
REST_API_OUTPUT_FORMAT = 'json' # or 'pprint' or 'null'

this will print all query in the json format. this print can be copy/posted into a json fixtures in the url list.

ie output:

## BEGIN GET http://localapi:8001/api/v2/user/3238/ =>
{'data': {'user': {'email': 'admin@exemple.com',
                   'first_name': '',
                   'id': 3238,
                   'last_name': '',
                   'links': {'poster': 'poster/'}}},
 'filter': {'json': None,
             'method': 'get',
             'params': {'exclude[]': {'*'}, 'include[]': {'id', 'first_name', 'email', 'last_name'}}},
 'status_code': 200}
## END GET http://localapi:8001/api/v2/user/3238/ <=

ie fixture:

{
    "user/3238/": [
        {'data': {'user': {'email': 'admin@exemple.com',
        'first_name': '',
        'id': 3238,
        'last_name': '',
        'links': {'poster': 'poster/'}}},
        'filter': {'json': None,
        'method': 'get',
        'params': {'exclude[]': {'*'}, 'include[]': {'id', 'first_name', 'email', 'last_name'}}},
        'status_code': 200}
    ]
}

the recipe is

{
    "url_part_after_database_name": [
        <past from output>
    ]
}