Handlers#

Some users might want to go beyond the boundaries and might want to handle everything by themself. Fortunately, library provides a set of classes that provide low level control over the API. Conventionally, we tend to call these low level classes, “Handlers”

Following are the main handlers provided by the library:

HTTPHandler#

class luster.HTTPHandler(*, token: str, session: Optional[aiohttp.client.ClientSession] = None, bot: bool = True)#

A class that handles HTTP requests to Revolt API.

In most cases, you might not need to interact with this class. Client provides a high level abstraction for this class.

You should not initialize this class manually. Instead, use the create_http_handler() helper function. In a Client, you can use Client.http_handler to get the instance of this class.

Parameters
  • token (str) – The bot or session token used for authenticating requests. This cannot be modified once initialized.

  • bot (bool) – Whether the passed token is a bot token. Set this to False when a session token is passed. Defaults to True.

  • session (Optional[aiohttp.ClientSession]) –

    The client session used for making HTTP requests.

    If not provided, A session is created internally and would be closed automatically after usage. Note that when a session is provided by the user, It must be closed by the user. Library will not take it’s ownership.

BASE_URL: ClassVar[str] = 'https://api.revolt.chat'#

The base URL used for routes.

USER_AGENT: ClassVar[str] = 'Luster (0.0.1, https://github.com/nerdguyahmad/luster)'#

The user agent for attaching to requests.

This by default includes library name but could be modified to be different.

Danger

Sending user agent that are invalid or are in invalid format can result in failing HTTP requests. It is recommended to not change this attribute!

property bot: bool#

Indicates whether token is a bot token.

Returns

Return type

bool

async close() bool#

Closes the underlying client session, if possible.

Returns

Indicates whether the session was closed.

Return type

bool

property closed: bool#

Indicates whether the underlying session is closed.

Returns

Return type

bool

async fetch_node_info() types.NodeInfo#

An alias of query_node() method.

async query_node() types.NodeInfo#

Fetches the information about current Revolt instance.

This route does not require authorization.

Returns

Return type

types.NodeInfo

async request(method: str, route: str, **kwargs: Any) Any#

Requests a certain route and returns the response data.

Parameters
  • method (str) – The HTTP method to use.

  • route (str) – The route that will be appended to BASE_URL.

  • **kwargs

    The keyword arguments that are passed to aiohttp.ClientSession.request(). It is worth noting that provided headers will be updated to include the proper authentication headers and you don’t have to add any authentication headers manually.

    Following headers will be overwritten:

    • X-Session-Token

    • X-Bot-Token

property session: Optional[aiohttp.client.ClientSession]#

The client session used for making HTTP requests.

Returns

Return type

Optional[aiohttp.ClientSession]

property session_owner: bool#

Indicates whether library owns the HTTP session.

Returns

Return type

bool

property token: Optional[str]#

The token used for authenticating requests.

Returns

Return type

Optional[str]

WebsocketHandler#

class luster.WebsocketHandler(http_handler: HTTPHandler, version: types.WebsocketVersion = 1)#

A class that handles websocket connection with Revolt Events API.

This is a low level interface of Client that provides interface for realtime communication with Revolt Events API using websocket.

Parameters
  • http_handler (HTTPHandler) – The HTTP handler used for opening websocket connections.

  • version (types.WebsocketVersion) – The version of websocket protocol to use. Defaults to 1 and currently there are no other options than 1

async close() None#

Closes the websocket connection.

property closed: bool#

Indicates whether the websocket connection is closed.

Returns

Return type

bool

async connect() None#

Connects the websocket.

This is a blocking coroutine that does not return until websocket connection is closed.

async get_websocket_url() str#

Gets the websocket URL including relevant parameters.

This under the hood uses the HTTPHandler.query_node() route to retrieve the websocket URL.

Returns

The websocket URL used for connecting to Revolt websocket.

Return type

str

property http_handler: luster.http.HTTPHandler#

The HTTP handler associated to this websocket handler.

Returns

Return type

HTTPHandler

async on_websocket_event(type: types.EventTypeRecv, data: Dict[str, Any]) Any#

A hook that gets called whenever a websocket event is received.

By default, this does nothing. The subclasses can override this method to implement custom behaviour.

Parameters
async send(type: types.EventTypeSend, data: Dict[str, Any]) None#

Sends an event via websocket.

Parameters