tribler.core.restapi.rest_manager

Attributes

ComponentsType

logger

RESTEndpointType

Classes

ApiKeyMiddleware

Middleware to check if REST requests include an API key.

RESTManager

This class is responsible for managing the startup and closing of the Tribler HTTP API.

Functions

wrap_tcp_keepalive(→ None)

A wrapper around aiohttp's tcp_keepalive that catches OSError 22 instances.

error_middleware(...)

Middleware to return nicely-formatted errors when common exceptions occur.

ui_middleware(...)

Forward request to a unknown pathname to /ui.

required_components_middleware(...)

Read a handler's required components and return HTTP_NOT_FOUND if they have not been set (yet).

Module Contents

tribler.core.restapi.rest_manager.ComponentsType
tribler.core.restapi.rest_manager.logger
tribler.core.restapi.rest_manager.RESTEndpointType
tribler.core.restapi.rest_manager.wrap_tcp_keepalive(transport: asyncio.Transport) None

A wrapper around aiohttp’s tcp_keepalive that catches OSError 22 instances.

See https://github.com/Tribler/tribler/issues/6429

class tribler.core.restapi.rest_manager.ApiKeyMiddleware(api_key: str)

Middleware to check if REST requests include an API key.

The key can be in:

  • The X-Api-Key header.

  • The apikey query parameter.

  • The api_key cookie.

api_key
async __call__(request: aiohttp.web_request.Request, handler: collections.abc.Callable[[aiohttp.web_request.Request], collections.abc.Awaitable[tribler.core.restapi.rest_endpoint.RESTResponse]]) tribler.core.restapi.rest_endpoint.RESTResponse

Call this middleware.

authenticate(request: aiohttp.web_request.Request) bool

Is the given request authenticated using an API key.

async tribler.core.restapi.rest_manager.error_middleware(request: aiohttp.web_request.Request, handler: collections.abc.Callable[[aiohttp.web_request.Request], collections.abc.Awaitable[tribler.core.restapi.rest_endpoint.RESTResponse]]) tribler.core.restapi.rest_endpoint.RESTResponse

Middleware to return nicely-formatted errors when common exceptions occur.

async tribler.core.restapi.rest_manager.ui_middleware(request: aiohttp.web_request.Request, handler: collections.abc.Callable[[aiohttp.web_request.Request], collections.abc.Awaitable[tribler.core.restapi.rest_endpoint.RESTResponse]]) tribler.core.restapi.rest_endpoint.RESTResponse

Forward request to a unknown pathname to /ui. This enables the GUI to request e.g. /index.html instead of using /ui/index.html.

async tribler.core.restapi.rest_manager.required_components_middleware(request: aiohttp.web_request.Request, handler: collections.abc.Callable[[aiohttp.web_request.Request], collections.abc.Awaitable[tribler.core.restapi.rest_endpoint.RESTResponse]]) tribler.core.restapi.rest_endpoint.RESTResponse

Read a handler’s required components and return HTTP_NOT_FOUND if they have not been set (yet).

class tribler.core.restapi.rest_manager.RESTManager(config: tribler.tribler_config.TriblerConfigManager, shutdown_timeout: int = 1)

This class is responsible for managing the startup and closing of the Tribler HTTP API.

_logger
root_endpoint
runner: aiohttp.web.AppRunner | None = None
site: aiohttp.web.TCPSite | None = None
site_https: aiohttp.web.TCPSite | None = None
config
state_dir
http_host
https_host
shutdown_timeout = 1
add_endpoint(endpoint: RESTEndpointType) RESTEndpointType

Add a REST endpoint to the root endpoint.

get_endpoint(name: str) tribler.core.restapi.rest_endpoint.RESTEndpoint

Get an endpoint by its name, including the first forward slash.

Requesting a non-Tribler (i.e., IPv8) endpoint will cause a crash.

get_api_port() int | None

Get the API port of the currently running server.

async start() None

Starts the HTTP API with the listen port as specified in the session configuration.

async start_http_site(runner: aiohttp.web.AppRunner) None

Start serving HTTP requests.

async start_https_site(runner: aiohttp.web.AppRunner) None

Start serving HTTPS requests.

async stop() None

Clean up all the REST endpoints and connections.