feat: implement context manager for UptimeKumaApi class
This commit is contained in:
parent
391e5a3077
commit
e42f6461c0
1 changed files with 23 additions and 0 deletions
|
@ -337,6 +337,9 @@ class UptimeKumaApi(object):
|
||||||
>>> from uptime_kuma_api import UptimeKumaApi
|
>>> from uptime_kuma_api import UptimeKumaApi
|
||||||
>>> api = UptimeKumaApi('INSERT_URL')
|
>>> api = UptimeKumaApi('INSERT_URL')
|
||||||
>>> api.login('INSERT_USERNAME', 'INSERT_PASSWORD')
|
>>> api.login('INSERT_USERNAME', 'INSERT_PASSWORD')
|
||||||
|
{
|
||||||
|
'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNjgyOTU4OTU4fQ.Xb81nuKXeNyE1D_XoQowYgsgZHka-edONdwHmIznJdk'
|
||||||
|
}
|
||||||
|
|
||||||
Now you can call one of the existing methods of the instance. For example create a new monitor:
|
Now you can call one of the existing methods of the instance. For example create a new monitor:
|
||||||
|
|
||||||
|
@ -354,6 +357,20 @@ class UptimeKumaApi(object):
|
||||||
|
|
||||||
>>> api.disconnect()
|
>>> api.disconnect()
|
||||||
|
|
||||||
|
With a context manager, the disconnect method is called automatically:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from uptime_kuma_api import UptimeKumaApi
|
||||||
|
|
||||||
|
with UptimeKumaApi('INSERT_URL') as api:
|
||||||
|
api.login('INSERT_USERNAME', 'INSERT_PASSWORD')
|
||||||
|
api.add_monitor(
|
||||||
|
type=MonitorType.HTTP,
|
||||||
|
name="Google",
|
||||||
|
url="https://google.com"
|
||||||
|
)
|
||||||
|
|
||||||
:param str url: The url to the Uptime Kuma instance. For example ``http://127.0.0.1:3001``
|
:param str url: The url to the Uptime Kuma instance. For example ``http://127.0.0.1:3001``
|
||||||
:param float wait_timeout: How many seconds the client should wait for the connection., defaults to 1
|
:param float wait_timeout: How many seconds the client should wait for the connection., defaults to 1
|
||||||
:param dict headers: Headers that are passed to the socketio connection, defaults to None
|
:param dict headers: Headers that are passed to the socketio connection, defaults to None
|
||||||
|
@ -419,6 +436,12 @@ class UptimeKumaApi(object):
|
||||||
|
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.disconnect()
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def wait_for_event(self, event: Event) -> None:
|
def wait_for_event(self, event: Event) -> None:
|
||||||
# 200 * 0.05 seconds = 10 seconds
|
# 200 * 0.05 seconds = 10 seconds
|
||||||
|
|
Loading…
Reference in a new issue