i3pyblocks.blocks.http
Blocks related to HTTP requests, based on aiohttp.
This module contains PollingRequestBlock, that uses aiohttp to request HTTP
endpoints and shows the result in i3bar.
Since requesting HTTP can takes quite sometime, it is very important that we
use an async library for this task or we could block updates in i3pyblocks
until the end of the HTTP request. aiohttp fits this task very well.
PollingRequestBlock is based on PollingBlock since the idea of this Block
is to be used for things like weather updates, for example:
PollingRequestBlock("https://wttr.in/?format=%c+%t")
But more advanced Blocks could update in response for a Webhook or a system event (for example, a network change could trigger a request to get external IP).
Module Contents
Classes
Block that shows result of a periodic HTTP request. |
Functions
|
Returns the response result as text. |
Attributes
- i3pyblocks.blocks.http.DEFAULT_TIMEOUT
- async i3pyblocks.blocks.http.text_callback(resp: aiohttp.ClientResponse) str
Returns the response result as text.
Mostly useful for endpoints that returns just text on its output. Some examples:
$ curl ifconfig.me/ip 123.123.123.123 $ curl 'wttr.in/?format=%t' +31°C
- class i3pyblocks.blocks.http.PollingRequestBlock(url: str, method: str = 'get', format: str = '{response}', format_error: str = 'ERROR', request_opts: Mapping[str, Any] = {'timeout': DEFAULT_TIMEOUT}, response_callback: Callable[[aiohttp.ClientResponse], Awaitable[str]] = text_callback, sleep: int = 60, **kwargs)
Bases:
i3pyblocks.blocks.PollingBlockBlock that shows result of a periodic HTTP request.
This Block continuously request a specified endpoint and shows the result and status code of the request in i3bar.
- Parameters
format – Format string showed after a successful request. Supports both
{response}and{status}placeholders.format_error – Format string showed in case of an error in request.
request_opts – A mapping of options passed directly to the
request()method inaiohttp. The list of available parameters is aiohttp docs.response_callback –
A function that will be called after the response is made to format the result. For example, consider an endpoiint that returns the JSON
{"hello": "world"}. We could format its output using:def json_callback(resp): j = await resp.json() return f"Hello {j['hello']}"
This function would result in
Hello worldon{response}placeholder.sleep – Sleep in seconds between each call to
run().**kwargs – Extra arguments to be passed to
PollingBlockclass.
- async run(self) None
Main loop in PollingBlock.
This is the method that will be run at each X
sleepseconds.Since this is an abstract method, it should be overriden before usage.