ramka.routing package#

Submodules#

ramka.routing.route module#

class ramka.routing.route.ResolvedRoute(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]], params: Dict[str, Any])#

Bases: Route

Resolved route representation.

A route defines a path (e.g. /users/) and a view (e.g. UserView).

A resolved route is a route with parameters resolved. It’s used to match a request to a view and to generate a response using the parameters.

Fields:

path (str): The path. view (Union[BaseView, Callable]): The view that will handle the path. methods (Optional[List[str]]): The HTTP methods supported by the view. params (Dict[str, Any]): Resolved parameters.

static from_route(route: Route, params: Dict[str, Any]) ResolvedRoute#

Create a resolved route from a route and parameters.

Parameters:
  • route (Route) – The route.

  • params (Dict[str, Any]) – The parameters.

class ramka.routing.route.Route(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]] = None)#

Bases: object

Route representation.

A single route defines a path (e.g. /users/) and a view (e.g. UserView).

The path should always be absolute which means it should start with a forward slash. It can consist of multiple arguments (e.g. /users/{id}/) and needs to be unique.

The view can be either a class (that inherits from ramka.views.BaseView) or a function that accepts two arguments: the request and the response. For function-based views, additional argument with all supported methods can be specified. By default, each view only supports GET requests. For class-based views, the view can have multiple methods (e.g. get, post, put, delete, etc.) and the handler will be selected based on the request method.

Each argument can have a type specified. For example, path /users/{id:d}/ means that the argument id should be a decimal number. For full list of supported types see https://github.com/r1chardj0n3s/parse#format-specification.

Fields:

path (str): The path. view (Union[BaseView, Callable]): The view that will handle the path. methods (Optional[List[str]]): The HTTP methods supported by the view.

get_handler(method: Optional[str] = 'get') Callable#

Get handler for the given method.

If the view is a class, the handler will be selected based on the request method. If the view is a function, the handler will be the view itself.

Parameters:

method (str) – Optional, the request method.

Returns:

The handler.

Return type:

Callable

Raises:
  • (ValueError) – If the method is not supported.

  • (AttributeError) – If the view is not a class or a function.

ramka.routing.router module#

class ramka.routing.router.BaseRouter#

Bases: ABC

Base router class.

The responsibility of the routes is to know how to handle given request.

Router, on the other hand, is responsible for storing all routes that have been defined in the application and finding correct routes for the requests.

There are two ways to add a new route to the router: by using the add_route method or by using the route decorator. It is the case to allow developers adding routes in a preffered way.

Fields:
routes (List[Route]): The list of routes that have been defined in the

application.

abstract add_route(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]] = None) None#

Add a route to the router.

It’s supposed to be used as a method.

Parameters:
  • path (str) – The path to add the route to.

  • view (Union[BaseView, Callable]) – The view to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

abstract has_route(path: str) bool#

Check if the router has a route for the given path.

Parameters:

path (str) – The path to check.

Returns:

True if the router has a route for the given path, False otherwise.

Return type:

bool

abstract resolve(path: str) ResolvedRoute#

Resolve the route for the given path.

Parameters:

path (str) – The path to resolve the route for.

Returns:

The resolved route.

Return type:

ResolvedRoute

abstract route(path: str, methods: Optional[List[str]] = None) Callable#

Add a route to the router.

It’s supposed to be used as a decorator.

Parameters:
  • path (str) – The path to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

class ramka.routing.router.SimpleRouter(force_trailing_slashes: bool = True)#

Bases: BaseRouter

Simple router class.

The responsibility of the routes is to know how to handle given request.

Router, on the other hand, is responsible for storing all routes that have been defined in the application and finding correct routes for the requests.

This router can resolve routes with or without trailing slashes (that behavior can be disabled).

Fields:
routes (List[Route]): The list of routes that have been defined in the

application.

add_route(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]] = None) None#

Add a route to the router.

It’s supposed to be used as a method.

Parameters:
  • path (str) – The path to add the route to.

  • view (Union[BaseView, Callable]) – The view to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

has_route(path: str) bool#

Check if the router has a route for the given path.

Parameters:

path (str) – The path to check.

Returns:

True if the router has a route for the given path, False otherwise.

Return type:

bool

resolve(path: str) ResolvedRoute#

Resolve the route for the given path.

Parameters:

path (str) – The path to resolve the route for.

Returns:

The resolved route.

Return type:

ResolvedRoute

route(path: str, methods: Optional[List[str]] = None) Callable#

Add a route to the router.

It’s supposed to be used as a decorator.

Parameters:
  • path (str) – The path to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

Returns:

The decorated function.

Return type:

Callable

Module contents#

class ramka.routing.BaseRouter#

Bases: ABC

Base router class.

The responsibility of the routes is to know how to handle given request.

Router, on the other hand, is responsible for storing all routes that have been defined in the application and finding correct routes for the requests.

There are two ways to add a new route to the router: by using the add_route method or by using the route decorator. It is the case to allow developers adding routes in a preffered way.

Fields:
routes (List[Route]): The list of routes that have been defined in the

application.

abstract add_route(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]] = None) None#

Add a route to the router.

It’s supposed to be used as a method.

Parameters:
  • path (str) – The path to add the route to.

  • view (Union[BaseView, Callable]) – The view to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

abstract has_route(path: str) bool#

Check if the router has a route for the given path.

Parameters:

path (str) – The path to check.

Returns:

True if the router has a route for the given path, False otherwise.

Return type:

bool

abstract resolve(path: str) ResolvedRoute#

Resolve the route for the given path.

Parameters:

path (str) – The path to resolve the route for.

Returns:

The resolved route.

Return type:

ResolvedRoute

abstract route(path: str, methods: Optional[List[str]] = None) Callable#

Add a route to the router.

It’s supposed to be used as a decorator.

Parameters:
  • path (str) – The path to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

class ramka.routing.ResolvedRoute(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]], params: Dict[str, Any])#

Bases: Route

Resolved route representation.

A route defines a path (e.g. /users/) and a view (e.g. UserView).

A resolved route is a route with parameters resolved. It’s used to match a request to a view and to generate a response using the parameters.

Fields:

path (str): The path. view (Union[BaseView, Callable]): The view that will handle the path. methods (Optional[List[str]]): The HTTP methods supported by the view. params (Dict[str, Any]): Resolved parameters.

static from_route(route: Route, params: Dict[str, Any]) ResolvedRoute#

Create a resolved route from a route and parameters.

Parameters:
  • route (Route) – The route.

  • params (Dict[str, Any]) – The parameters.

class ramka.routing.Route(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]] = None)#

Bases: object

Route representation.

A single route defines a path (e.g. /users/) and a view (e.g. UserView).

The path should always be absolute which means it should start with a forward slash. It can consist of multiple arguments (e.g. /users/{id}/) and needs to be unique.

The view can be either a class (that inherits from ramka.views.BaseView) or a function that accepts two arguments: the request and the response. For function-based views, additional argument with all supported methods can be specified. By default, each view only supports GET requests. For class-based views, the view can have multiple methods (e.g. get, post, put, delete, etc.) and the handler will be selected based on the request method.

Each argument can have a type specified. For example, path /users/{id:d}/ means that the argument id should be a decimal number. For full list of supported types see https://github.com/r1chardj0n3s/parse#format-specification.

Fields:

path (str): The path. view (Union[BaseView, Callable]): The view that will handle the path. methods (Optional[List[str]]): The HTTP methods supported by the view.

get_handler(method: Optional[str] = 'get') Callable#

Get handler for the given method.

If the view is a class, the handler will be selected based on the request method. If the view is a function, the handler will be the view itself.

Parameters:

method (str) – Optional, the request method.

Returns:

The handler.

Return type:

Callable

Raises:
  • (ValueError) – If the method is not supported.

  • (AttributeError) – If the view is not a class or a function.

class ramka.routing.SimpleRouter(force_trailing_slashes: bool = True)#

Bases: BaseRouter

Simple router class.

The responsibility of the routes is to know how to handle given request.

Router, on the other hand, is responsible for storing all routes that have been defined in the application and finding correct routes for the requests.

This router can resolve routes with or without trailing slashes (that behavior can be disabled).

Fields:
routes (List[Route]): The list of routes that have been defined in the

application.

add_route(path: str, view: Union[BaseView, Callable], methods: Optional[List[str]] = None) None#

Add a route to the router.

It’s supposed to be used as a method.

Parameters:
  • path (str) – The path to add the route to.

  • view (Union[BaseView, Callable]) – The view to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

has_route(path: str) bool#

Check if the router has a route for the given path.

Parameters:

path (str) – The path to check.

Returns:

True if the router has a route for the given path, False otherwise.

Return type:

bool

resolve(path: str) ResolvedRoute#

Resolve the route for the given path.

Parameters:

path (str) – The path to resolve the route for.

Returns:

The resolved route.

Return type:

ResolvedRoute

route(path: str, methods: Optional[List[str]] = None) Callable#

Add a route to the router.

It’s supposed to be used as a decorator.

Parameters:
  • path (str) – The path to add the route to.

  • methods (Optional[List[str]]) – The list of methods to add the route to.

Returns:

The decorated function.

Return type:

Callable