Skip to content

Commit 4a9db34

Browse files
committed
feat: update use_trace context manager to specify return type and add tests for trace id handling
1 parent 89e91a7 commit 4a9db34

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

audiostack/helpers/request_interface.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import contextvars
33
import json
44
import shutil
5-
from typing import Any, Callable, Dict, Optional, Union
5+
from typing import Any, Callable, Dict, Generator, Optional, Union
66

77
import requests
88

@@ -160,9 +160,8 @@ def download_url(cls, url: str, name: str, destination: str) -> None:
160160

161161

162162
@contextlib.contextmanager
163-
def use_trace(trace_id):
163+
def use_trace(trace_id: str) -> Generator[None, None, None]:
164164
token = _current_trace_id.set(trace_id)
165-
166165
try:
167166
yield
168167
finally:

audiostack/tests/helpers/test_request_interface.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import pytest
44

55
import audiostack
6-
from audiostack.helpers.request_interface import RequestInterface
6+
from audiostack.helpers.request_interface import RequestInterface, use_trace
7+
from audiostack.helpers.request_types import RequestTypes
78

89

910
@patch("audiostack.helpers.request_interface.open")
@@ -59,3 +60,49 @@ def test_RequestInterface_download_url_4XX(
5960
mock_requests.get.return_value.status_code = 400
6061
with pytest.raises(Exception):
6162
RequestInterface.download_url(url="foo", name="bar", destination="baz")
63+
64+
65+
@patch("audiostack.helpers.request_interface.requests")
66+
def test_RequestInterface_with_trace_id(mock_requests: Mock) -> None:
67+
body = {
68+
"scriptText": "scriptText",
69+
"projectName": "projectName",
70+
"moduleName": "moduleName",
71+
"scriptName": "scriptName",
72+
"metadata": "metadata",
73+
}
74+
with use_trace("trace_id"):
75+
mock_requests.post.return_value.status_code = 200
76+
interface = RequestInterface(family="content")
77+
interface.send_request(rtype=RequestTypes.POST, route="script", json=body)
78+
mock_requests.post.assert_called_once_with(
79+
url=f"{audiostack.api_base}/content/script",
80+
headers={
81+
"x-api-key": audiostack.api_key,
82+
"x-python-sdk-version": audiostack.sdk_version,
83+
"x-customer-trace-id": "trace_id",
84+
},
85+
json=body,
86+
)
87+
88+
89+
@patch("audiostack.helpers.request_interface.requests")
90+
def test_RequestInterface_with_no_trace_id(mock_requests: Mock) -> None:
91+
body = {
92+
"scriptText": "scriptText",
93+
"projectName": "projectName",
94+
"moduleName": "moduleName",
95+
"scriptName": "scriptName",
96+
"metadata": "metadata",
97+
}
98+
mock_requests.post.return_value.status_code = 200
99+
interface = RequestInterface(family="content")
100+
interface.send_request(rtype=RequestTypes.POST, route="script", json=body)
101+
mock_requests.post.assert_called_once_with(
102+
url=f"{audiostack.api_base}/content/script",
103+
headers={
104+
"x-api-key": audiostack.api_key,
105+
"x-python-sdk-version": audiostack.sdk_version,
106+
},
107+
json=body,
108+
)

0 commit comments

Comments
 (0)