Skip to content

Commit

Permalink
[WIP] Replace token query with header attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Brulatout committed Aug 24, 2023
1 parent 0792402 commit 4742696
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 127 deletions.
33 changes: 20 additions & 13 deletions consul/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class HTTPClient(base.HTTPClient):
"""Asyncio adapter for python consul using aiohttp library"""

def __init__(self, *args, loop=None, connections_limit=None, connections_timeout=None, **kwargs):
def __init__(self, *args, loop=None, connections_limit=None, connections_timeout=None, token=None, **kwargs):
super().__init__(*args, **kwargs)
self._loop = loop or asyncio.get_event_loop()
connector_kwargs = {}
Expand All @@ -21,35 +21,41 @@ def __init__(self, *args, loop=None, connections_limit=None, connections_timeout
if connections_timeout:
timeout = aiohttp.ClientTimeout(total=connections_timeout)
session_kwargs["timeout"] = timeout
self._session = aiohttp.ClientSession(connector=connector, **session_kwargs)
header = {}
if token:
header["X-Consul-Token"] = token
self._session = aiohttp.ClientSession(connector=connector, headers=header, **session_kwargs)

async def _request(self, callback, method, uri, data=None, connections_timeout=None):
async def _request(self, callback, method, uri, data=None, connections_timeout=None, token=None):
session_kwargs = {}
if connections_timeout:
timeout = aiohttp.ClientTimeout(total=connections_timeout)
session_kwargs["timeout"] = timeout
resp = await self._session.request(method, uri, data=data, **session_kwargs)
header = {}
if token:
header["X-Consul-Token"] = token
resp = await self._session.request(method, uri, data=data, headers=header, **session_kwargs)
body = await resp.text(encoding="utf-8")
if resp.status == 599:
raise base.Timeout
r = base.Response(resp.status, resp.headers, body)
return callback(r)

def get(self, callback, path, params=None, connections_timeout=None):
def get(self, callback, path, params=None, connections_timeout=None, token=None):
uri = self.uri(path, params)
return self._request(callback, "GET", uri, connections_timeout=connections_timeout)
return self._request(callback, "GET", uri, connections_timeout=connections_timeout, token=token)

def put(self, callback, path, params=None, data="", connections_timeout=None):
def put(self, callback, path, params=None, data="", connections_timeout=None, token=None):
uri = self.uri(path, params)
return self._request(callback, "PUT", uri, data=data, connections_timeout=connections_timeout)
return self._request(callback, "PUT", uri, data=data, connections_timeout=connections_timeout, token=token)

def delete(self, callback, path, params=None, connections_timeout=None):
def delete(self, callback, path, params=None, connections_timeout=None, token=None):
uri = self.uri(path, params)
return self._request(callback, "DELETE", uri, connections_timeout=connections_timeout)
return self._request(callback, "DELETE", uri, connections_timeout=connections_timeout, token=token)

def post(self, callback, path, params=None, data="", connections_timeout=None):
def post(self, callback, path, params=None, data="", connections_timeout=None, token=None):
uri = self.uri(path, params)
return self._request(callback, "POST", uri, data=data, connections_timeout=connections_timeout)
return self._request(callback, "POST", uri, data=data, connections_timeout=connections_timeout, token=token)

def close(self):
return self._session.close()
Expand All @@ -62,14 +68,15 @@ def __init__(self, *args, loop=None, connections_limit=None, connections_timeout
self.connections_timeout = connections_timeout
super().__init__(*args, **kwargs)

def http_connect(self, host, port, scheme, verify=True, cert=None):
def http_connect(self, host, port, scheme, verify=True, cert=None, token=None):
return HTTPClient(
host,
port,
scheme,
loop=self._loop,
connections_limit=self.connections_limit,
connections_timeout=self.connections_timeout,
token=token,
verify=verify,
cert=cert,
)
Expand Down
Loading

0 comments on commit 4742696

Please sign in to comment.