| 1 |
from typing import Optional |
| 2 |
|
| 3 |
import aiohttp |
| 4 |
|
| 5 |
from consul import Timeout, base |
| 6 |
|
| 7 |
__all__ = ["Consul"] |
| 8 |
|
| 9 |
|
| 10 |
class HTTPClient(base.HTTPClient): |
| 11 |
"""Asyncio adapter for python consul using aiohttp library""" |
| 12 |
|
| 13 |
def __init__(self, *args, loop=None, connections_limit=None, connections_timeout=None, **kwargs) -> None: |
| 14 |
super().__init__(*args, **kwargs) |
| 15 |
self.loop = loop |
| 16 |
connector_kwargs = {} |
| 17 |
if connections_limit: |
| 18 |
connector_kwargs["limit"] = connections_limit |
| 19 |
connector = aiohttp.TCPConnector(loop=self.loop, verify_ssl=self.verify, **connector_kwargs) |
| 20 |
session_kwargs = {} |
| 21 |
if connections_timeout: |
| 22 |
timeout = aiohttp.ClientTimeout(total=connections_timeout) |
| 23 |
session_kwargs["timeout"] = timeout |
| 24 |
self._session = aiohttp.ClientSession(connector=connector, **session_kwargs) |
| 25 |
|
| 26 |
async def _request( |
| 27 |
self, callback, method, uri, headers: Optional[dict[str, str]], data=None, connections_timeout=None |
| 28 |
): |
| 29 |
session_kwargs = {} |
| 30 |
if connections_timeout: |
| 31 |
timeout = aiohttp.ClientTimeout(total=connections_timeout) |
| 32 |
session_kwargs["timeout"] = timeout |
| 33 |
resp = await self._session.request(method, uri, headers=headers, data=data, **session_kwargs) |
| 34 |
body = await resp.text(encoding="utf-8") |
| 35 |
if resp.status == 599: |
| 36 |
raise Timeout |
| 37 |
r = base.Response(resp.status, resp.headers, body) |
| 38 |
return callback(r) |
| 39 |
|
| 40 |
def get(self, callback, path, params=None, headers: Optional[dict[str, str]] = None, connections_timeout=None): |
| 41 |
uri = self.uri(path, params) |
| 42 |
return self._request(callback, "GET", uri, headers=headers, connections_timeout=connections_timeout) |
| 43 |
|
| 44 |
def put( |
| 45 |
self, |
| 46 |
callback, |
| 47 |
path, |
| 48 |
params=None, |
| 49 |
data: str = "", |
| 50 |
headers: Optional[dict[str, str]] = None, |
| 51 |
connections_timeout=None, |
| 52 |
): |
| 53 |
uri = self.uri(path, params) |
| 54 |
return self._request(callback, "PUT", uri, headers=headers, data=data, connections_timeout=connections_timeout) |
| 55 |
|
| 56 |
def delete(self, callback, path, params=None, headers: Optional[dict[str, str]] = None, connections_timeout=None): |
| 57 |
uri = self.uri(path, params) |
| 58 |
return self._request(callback, "DELETE", uri, headers=headers, connections_timeout=connections_timeout) |
| 59 |
|
| 60 |
def post( |
| 61 |
self, |
| 62 |
callback, |
| 63 |
path, |
| 64 |
params=None, |
| 65 |
data: str = "", |
| 66 |
headers: Optional[dict[str, str]] = None, |
| 67 |
connections_timeout=None, |
| 68 |
): |
| 69 |
uri = self.uri(path, params) |
| 70 |
return self._request(callback, "POST", uri, headers=headers, data=data, connections_timeout=connections_timeout) |
| 71 |
|
| 72 |
def close(self): |
| 73 |
return self._session.close() |
| 74 |
|
| 75 |
|
| 76 |
class Consul(base.Consul): |
| 77 |
def __init__(self, *args, loop=None, connections_limit=None, connections_timeout=None, **kwargs) -> None: |
| 78 |
self.loop = loop |
| 79 |
self.connections_limit = connections_limit |
| 80 |
self.connections_timeout = connections_timeout |
| 81 |
super().__init__(*args, **kwargs) |
| 82 |
|
| 83 |
def http_connect(self, host: str, port: int, scheme, verify: bool = True, cert=None): |
| 84 |
return HTTPClient( |
| 85 |
host, |
| 86 |
port, |
| 87 |
scheme, |
| 88 |
loop=self.loop, |
| 89 |
connections_limit=self.connections_limit, |
| 90 |
connections_timeout=self.connections_timeout, |
| 91 |
verify=verify, |
| 92 |
cert=cert, |
| 93 |
) |
| 94 |
|
| 95 |
def close(self): |
| 96 |
"""Close all opened http connections""" |
| 97 |
return self.http.close() |