Skip to content

Commit b0dbeed

Browse files
fix: Adding Authorization token to headers (#74)
1 parent b1bc7b9 commit b0dbeed

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

audiostack/helpers/request_interface.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ def __init__(self, family: str) -> None:
4141
@staticmethod
4242
def make_header(headers: Optional[dict] = None) -> dict:
4343
new_headers = {
44-
"x-api-key": audiostack.api_key,
4544
"x-python-sdk-version": audiostack.sdk_version,
4645
}
46+
if audiostack.api_key:
47+
new_headers["x-api-key"] = audiostack.api_key
48+
elif audiostack.Authorization:
49+
new_headers["Authorization"] = audiostack.Authorization
4750
current_trace_id = _current_trace_id.get()
4851
if current_trace_id is not None:
4952
new_headers["x-customer-trace-id"] = current_trace_id

audiostack/tests/helpers/test_request_interface.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def test_RequestInterface_download_url(
1414
mock_requests: Mock, mock_shutil: Mock, mock_open: Mock, tmp_path: str
1515
) -> None:
1616
mock_requests.get.return_value.status_code = 200
17-
1817
RequestInterface.download_url(url="foo", name="bar", destination=tmp_path)
1918
mock_requests.get.assert_called_once_with(
2019
url="foo",
@@ -106,3 +105,28 @@ def test_RequestInterface_with_no_trace_id(mock_requests: Mock) -> None:
106105
},
107106
json=body,
108107
)
108+
109+
110+
def test_RequestInterface_make_header_api_key() -> None:
111+
audiostack.Authorization = "bearer_token" # type: ignore
112+
# Gets ignored if api_key is present
113+
headers = RequestInterface.make_header()
114+
assert headers == {
115+
"x-python-sdk-version": audiostack.sdk_version,
116+
"x-api-key": audiostack.api_key,
117+
}
118+
119+
120+
def test_RequestInterface_make_header_Authorization() -> None:
121+
key = audiostack.api_key # keep the key as we will reset it after the test
122+
audiostack.api_key = None
123+
audiostack.Authorization = "bearer_token" # type: ignore
124+
headers = RequestInterface.make_header()
125+
assert headers == {
126+
"x-python-sdk-version": audiostack.sdk_version,
127+
"Authorization": "bearer_token",
128+
}
129+
130+
audiostack.api_key = key
131+
audiostack.Authorization = None
132+
# resetting after last test - otherwise downstream tests will fail with wrong API even if they import the API key

0 commit comments

Comments
 (0)