Adapters

This library uses adapters to implement the “I/O parts”, namely reading and writing headers from HTTP requests and response. To adapt this library to a new framework it is neccessary to subclass BaseAdapter and then initialize unpoly.up.Unpoly it. Since unpoly.up.Unpoly is initialized once per request, it makes sense to pass the request to the adapter via init, but there is no requirement for this – the adapter could simply access the request via thread-locals or other means.

As an example the usage with the builtin Django Adapter looks like this:

from unpoly import Unpoly
from unpoly.contrib.django import DjangoAdapter
# The following code is run in a middleware, once per request
adapter = DjangoAdapter(request)
# Attach `Unpoly` to the request, so views can easily acces it
request.up = Unpoly(adapter)
# Actually execute the view and get the response object
response = handle_view(request)
# Tell `Unpoly` to set the relevant `X-Up-*` headers
request.up.finalize_reponse(response)
class unpoly.adapter.BaseAdapter

Provides the entrypoint for other frameworks to use this library.

Implements common functionality that is not often overriden as well as framework specific hooks.

deserialize_data(data: str)

Deserializes data passed in by Unpoly.

By default it simply reads it as JSON, but can be overriden if custom decoders are needed.

property location

Exposes the current request’s location (path including query params)

Needs to be implemented.

property method

Exposes the current request’s method (GET/POST etc)

Needs to be implemented.

redirect_uri(response) Optional[str]

Returns the redirect target of a response or None if the response is not a redirection (ie if it’s status code is not in the range 300-400).

Needs to be implemented.

request_headers() Mapping[str, str]

Reads the request headers from the current request.

Needs to be implemented.

request_params() Mapping[str, str]

Reads the GET params from the current request.

Needs to be implemented.

serialize_data(data) str

Serializes the data for passing it to Unpoly.

By default it simply serializes it as JSON, but can be overriden if custom encoders are needed.

Set or delete the _up_method cookie.

The implementation should set the cookie if needs_cookie is True and otherwise remove it if set.

Needs to be implemented.

set_headers(response, headers)

Set headers like X-Up-Location on the current response.

Needs to be implemented.

set_redirect_uri(response, uri)

Set a new redirect target for the current response. This is used to pass unpoly parameters via GET params through redirects.

Needs to be implemented.