Skip to content

Commit 85ec107

Browse files
committed
Allow custom headers while connect
1 parent 7c63d31 commit 85ec107

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

videodb/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def connect(
5858
api_key: str = None,
5959
base_url: Optional[str] = VIDEO_DB_API,
6060
log_level: Optional[int] = logging.INFO,
61+
**kwargs,
6162
) -> Connection:
6263
"""A client for interacting with a videodb via REST API
6364
@@ -76,4 +77,4 @@ def connect(
7677
"No API key provided. Set an API key either as an environment variable (VIDEO_DB_API_KEY) or pass it as an argument."
7778
)
7879

79-
return Connection(api_key, base_url)
80+
return Connection(api_key, base_url, **kwargs)

videodb/_utils/_http_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
base_url: str,
3535
version: str,
3636
max_retries: Optional[int] = HttpClientDefaultValues.max_retries,
37+
**kwargs,
3738
) -> None:
3839
"""Create a new http client instance
3940
@@ -52,11 +53,13 @@ def __init__(
5253
self.session.mount("http://", adapter)
5354
self.session.mount("https://", adapter)
5455
self.version = version
56+
kwargs = self._format_headers(kwargs)
5557
self.session.headers.update(
5658
{
5759
"x-access-token": api_key,
5860
"x-videodb-client": f"videodb-python/{self.version}",
5961
"Content-Type": "application/json",
62+
**kwargs,
6063
}
6164
)
6265
self.base_url = base_url
@@ -198,6 +201,14 @@ def _parse_response(self, response: requests.Response):
198201
f"Invalid request: {response.text}", response
199202
) from None
200203

204+
def _format_headers(self, headers: dict):
205+
"""Format the headers"""
206+
formatted_headers = {}
207+
for key, value in headers.items():
208+
key = key.lower().replace("_", "-")
209+
formatted_headers[f"x-{key}"] = value
210+
return formatted_headers
211+
201212
def get(
202213
self, path: str, show_progress: Optional[bool] = False, **kwargs
203214
) -> requests.Response:

videodb/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class Connection(HttpClient):
3030
"""Connection class to interact with the VideoDB"""
3131

32-
def __init__(self, api_key: str, base_url: str) -> "Connection":
32+
def __init__(self, api_key: str, base_url: str, **kwargs) -> "Connection":
3333
"""Initializes a new instance of the Connection class with specified API credentials.
3434
3535
Note: Users should not initialize this class directly.
@@ -44,7 +44,7 @@ def __init__(self, api_key: str, base_url: str) -> "Connection":
4444
self.api_key = api_key
4545
self.base_url = base_url
4646
self.collection_id = "default"
47-
super().__init__(api_key=api_key, base_url=base_url, version=__version__)
47+
super().__init__(api_key=api_key, base_url=base_url, version=__version__, **kwargs)
4848

4949
def get_collection(self, collection_id: Optional[str] = "default") -> Collection:
5050
"""Get a collection object by its ID.

0 commit comments

Comments
 (0)