Add Support for API Key Authentication (#230)

This commit is contained in:
Alessio Dalla Piazza
2024-03-11 13:16:10 +01:00
committed by GitHub
parent 1b35547927
commit d5ae2ebaa2
4 changed files with 63 additions and 18 deletions

View File

@@ -88,23 +88,22 @@ class HttpResponse:
return self.resp.status
def http_request(url, json=None, stream=False, auth_token=None, verify=None):
def http_request(url, json=None, stream=False, auth_token=None, api_key=None, verify=None):
"""A faster version of requests.post with low-level urllib API."""
headers = {"Content-Type": "application/json; charset=utf-8"}
# add the Authorization header if an auth token is provided
if auth_token is not None:
headers["Authorization"] = f"Bearer {auth_token}"
# add the API Key header if an API key is provided
if api_key is not None:
headers["X-API-Key"] = api_key
if stream:
if auth_token is None:
return requests.post(url, json=json, stream=True, verify=verify)
headers = {
"Content-Type": "application/json",
"Authentication": f"Bearer {auth_token}",
}
return requests.post(
url, json=json, stream=True, headers=headers, verify=verify
)
return requests.post(url, json=json, stream=True, headers=headers)
else:
req = urllib.request.Request(url)
req.add_header("Content-Type", "application/json; charset=utf-8")
if auth_token is not None:
req.add_header("Authentication", f"Bearer {auth_token}")
req = urllib.request.Request(url, headers=headers)
if json is None:
data = None
else: