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 aClient
, you can useClient.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 passedtoken
is a bot token. Set this toFalse
when a session token is passed. Defaults toTrue
.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.
- 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!
- async close() bool #
Closes the underlying client session, if possible.
- Returns
Indicates whether the session was closed.
- Return type
- 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
- 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.**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
]
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 to1
and currently there are no other options than1
- 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
- property http_handler: luster.http.HTTPHandler#
The HTTP handler associated to this websocket handler.
- Returns
- Return type
- 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
type (
types.EventTypeRecv
) – The type of event that was received.data (
dict
) – The received data.
- async send(type: types.EventTypeSend, data: Dict[str, Any]) None #
Sends an event via websocket.
- Parameters
type (
types.EventTypeSend
) – The type of event to send.data (
dict
) – The event data, excludingtype
.