Skip to content

Commit

Permalink
Replace token query with header attribute
Browse files Browse the repository at this point in the history
See : https://developer.hashicorp.com/consul/api-docs/api-structure
The "?token=" query parameter is deprecated and will be removed in Consul 1.17.
  • Loading branch information
Mathias Brulatout committed Feb 2, 2024
1 parent 1e3692a commit fe674b6
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 141 deletions.
33 changes: 23 additions & 10 deletions consul/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,47 @@ def __init__(self, *args, loop=None, connections_limit=None, connections_timeout
if connections_timeout:
timeout = aiohttp.ClientTimeout(total=connections_timeout)
session_kwargs["timeout"] = timeout

header = self.build_header(**kwargs)
if header:
session_kwargs["headers"] = header
self._session = aiohttp.ClientSession(connector=connector, **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, **kwargs):
session_kwargs = {}
if connections_timeout:
timeout = aiohttp.ClientTimeout(total=connections_timeout)
session_kwargs["timeout"] = timeout

header = self.build_header(**kwargs)
if header:
session_kwargs["headers"] = header

resp = await self._session.request(method, uri, data=data, **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, **kwargs):
uri = self.uri(path, params)
return self._request(callback, "GET", uri, connections_timeout=connections_timeout)
header = self.build_header(**kwargs)
if header:
kwargs["headers"] = header
return self._request(callback, "GET", uri, connections_timeout=connections_timeout, **kwargs)

def put(self, callback, path, params=None, data="", connections_timeout=None):
def put(self, callback, path, params=None, data="", connections_timeout=None, **kwargs):
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, **kwargs)

def delete(self, callback, path, params=None, connections_timeout=None):
def delete(self, callback, path, params=None, connections_timeout=None, **kwargs):
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, **kwargs)

def post(self, callback, path, params=None, data="", connections_timeout=None):
def post(self, callback, path, params=None, data="", connections_timeout=None, **kwargs):
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, **kwargs)

def close(self):
return self._session.close()
Expand All @@ -62,7 +74,7 @@ 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, **kwargs):
return HTTPClient(
host,
port,
Expand All @@ -72,6 +84,7 @@ def http_connect(self, host, port, scheme, verify=True, cert=None):
connections_timeout=self.connections_timeout,
verify=verify,
cert=cert,
**kwargs,
)

def close(self):
Expand Down
Loading

0 comments on commit fe674b6

Please sign in to comment.