URL Routing#

Tryton provides two ways to connect URL rules to a callable endpoint.

A decorator for small functions that won’t be modified by further modules.

A Router living in the Pool when the endpoint might be extended by another module.

The route decorator#

The simplest way to define a route is by using the decorator method route of the trytond.wsgi.app instance. This allows you to define a custom API based on HTTP that can be used to create specific user applications.

The decorator takes as first parameter a string which follow the Rule Format of Werkzeug and as second parameter sequence of HTTP methods.

Example:

from trytond.wsgi import app

@app.route('/hello', methods=['GET'])
def hello(request):
    return 'Hello world'

Router classes#

A more versatile way to expose entrypoints is by using a Router. A Router is a class registered in the Pool that contains a mapping between some Route and a method of the Router. A Route must contain at least one Rule which also follows the Rule Format of Werkzeug in order to map the path part of the URL to the endpoint parameters. A Route can also define a list of decorators that are applied to the corresponding endpoint.

Example:

from trytond.routing import Route, Router, Rule

class Example(Router):
    __name__ = 'example'

    @classmethod
    def __setup__(cls):
        super().__setup__()
        cls.__routes__.update({
                'hello': Route(
                    Rule('hello', methods={'GET'}),
                    Rule('hello/<name>', methods={'GET'}),
                    decorators=[
                        with_pool,
                        with_transaction(),
                        ],
                    ),
                })

    @classmethod
    def hello(cls, request, pool, name='World'):
        return f'Hello, {name}!'

Note

As the router are registered in the Pool, they can be extended.

Routing helpers#

The following converter is available:

base64

This converter accepts any Base64 string and transforms it into its corresponding bytes value.

Some decorators are provided in trytond.protocols.wrappers to ease the creation of routes:

set_max_request_size(size)

Changes the default limit size of the request.

allow_null_origin

Allows requests that have their Origin set to null.

with_pool

Takes the first parameter as database name and replaces it by the corresponding instance of the Pool.

with_transaction([readonly[, user[, context[, timeout]]]])

Starts a Transaction using the Pool from with_pool. If readonly is not set, the transaction will not be readonly for POST, PUT, DELETE and PATCH methods and readonly for all others.

user_application(name[, json])

Set the user from the Authorization header using the bearer type with the user application key, or the basic type without a username and with the user application key as the password.