Capture Listener
For example, imagine that every time a request to the ‘/logs’ route is made, my code performs a task that checks if the new logs differ from the old ones. Well, let’s see how this can be done:APP_STATUS
, LOGS
, or BACKUP
. This function should take two parameters:
before
(represents the state after the request)after
(represents the state before the request)
before
and after
depends on which endpoint the listener is “listening” to. If it is the APP_STATUS route, it will receive a StatusData
, LOGS will receive a LogsData
, and BACKUP will receive a BackupData
.
As you may have noticed in the example above, the first time the comparison between the logs occurs, after != before
returns True. This happens precisely because after is equal to LogsData(logs='')
, as there is still nothing stored in the cache internally.
Additional information about this decorator
Additional information about this decorator
- If you use discord.py or some fork (you probably use), you should
know that what differentiates events is the name
of the functions that the decorator wraps, but here it differs. To know
which
API route the decorator needs to “listen” to, we use the
endpopint
parameter, it receives anEndpoint
class, so the name of the function that the decorator wraps is up to you. - The function that the decorator wraps can actually be anything that is
a callable. This includes regular functions, coroutines, and even
classes (
__init__
will be called). - If the endpoint is not an [Endpoint.app_status()], [Endpoint.logs()], or [Endpoint.backup()],
only a
response
parameter (of typesquarecloud.http.Response
) will be returned.
You can use the
avoid_listener=True
parameter so that the application listener is not called.Request Listener
The “request listeners” do pretty much the same thing. But here you use the [Client], and the return of all endpoints issquarecloud.http.Response
objects.
Passing extra arguments
You can pass to some methods keyword argument calledextra
, and this will be passed to your listener
You can get some cool features in listeners using pydantic in your project, take a look at using pydantic