add context manager and tests

This commit is contained in:
Andrew Herrington
2021-12-30 18:24:57 -06:00
parent 7bc06275d3
commit 885651c666
4 changed files with 107 additions and 0 deletions

View File

@@ -1,8 +1,10 @@
"""An async healthchecks.io client."""
from types import TracebackType
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
from httpx import Client as HTTPXClient
@@ -50,6 +52,23 @@ class Client(AbstractClient):
self._client.headers["user-agent"] = f"py-healthchecks.io/{client_version}"
self._client.headers["Content-type"] = "application/json"
def __enter__(self) -> "Client":
"""Context manager entrance.
Returns:
Client: returns this client as a context manager
"""
return self
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc: Optional[BaseException],
traceback: Optional[TracebackType],
) -> None:
"""Context manager exit."""
self._finalizer_method()
def _finalizer_method(self) -> None:
"""Closes the httpx client."""
self._client.close()