Skip to content

Commit fb05c3c

Browse files
authored
sent sdk version in headers (#65)
* sent sdk version in headers * comment suggestions, tests * add client headers to template since _clinet.py is autogenerated * add autogenerated client * update jinja and client, move tests to test_room_api.py * format * fix tests * Revert "fix tests" This reverts commit 645f6ca. * Revert "format" This reverts commit 72c5e82. * Revert "update jinja and client, move tests to test_room_api.py" This reverts commit 7731e56. * Revert "add autogenerated client" This reverts commit 29eee7d. * Revert "add client headers to template since _clinet.py is autogenerated" This reverts commit a28ab40. * Revert "comment suggestions, tests" This reverts commit 80f2b28. * Revert "sent sdk version in headers" This reverts commit cf9c640. * add headers to _client.py since client.py is autogenerated * add test * after changing fishjam api validation, max_peers can also be string like: "10" because cast value will automatically check if string is a valid positive number * update header_value to new schema
1 parent 2d1e746 commit fb05c3c

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

fishjam/api/_client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
from fishjam._openapi_client.types import Response
66
from fishjam.errors import HTTPError
77
from fishjam.utils import get_fishjam_url
8+
from fishjam.version import get_version
89

910

1011
class Client:
1112
def __init__(self, fishjam_id: str, management_token: str):
1213
self._fishjam_url = get_fishjam_url(fishjam_id)
13-
self.client = AuthenticatedClient(self._fishjam_url, token=management_token)
14+
self.client = AuthenticatedClient(
15+
self._fishjam_url,
16+
token=management_token,
17+
headers={"x-fishjam-api-client": f"python-server/{get_version()}"},
18+
)
1419

1520
def _request(self, method, **kwargs):
1621
response = method.sync_detailed(client=self.client, **kwargs)

tests/test_room_api.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2+
from unittest.mock import Mock, patch
23

4+
import httpx
35
import pytest
46

57
from fishjam import (
@@ -26,6 +28,7 @@
2628
RoomType,
2729
VideoCodec,
2830
)
31+
from fishjam.version import get_version
2932

3033
HOST = "proxy" if os.getenv("DOCKER_TEST") == "TRUE" else "localhost"
3134
FISHJAM_ID = f"http://{HOST}:5555"
@@ -54,6 +57,36 @@ def test_valid_token(self):
5457
assert room in all_rooms
5558

5659

60+
class TestAPIClientHeader:
61+
def test_x_fishjam_api_client_header_is_sent(self):
62+
mock_response = Mock(spec=httpx.Response)
63+
mock_response.status_code = 200
64+
mock_response.headers = httpx.Headers({})
65+
mock_response.json.return_value = {"data": []}
66+
67+
captured_headers = None
68+
69+
def mock_send(request, **kwargs):
70+
nonlocal captured_headers
71+
captured_headers = dict(request.headers)
72+
return mock_response
73+
74+
room_api = FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN)
75+
76+
with patch.object(httpx.HTTPTransport, "handle_request", side_effect=mock_send):
77+
try:
78+
room_api.get_all_rooms()
79+
except Exception:
80+
# We don't care if the request fails, we just want to check the headers
81+
pass
82+
83+
assert captured_headers is not None
84+
assert "x-fishjam-api-client" in captured_headers
85+
86+
expected_header_value = f"python-server/{get_version()}"
87+
assert captured_headers["x-fishjam-api-client"] == expected_header_value
88+
89+
5790
@pytest.fixture
5891
def room_api():
5992
return FishjamClient(FISHJAM_ID, MANAGEMENT_TOKEN)
@@ -102,7 +135,7 @@ def test_valid_params(self, room_api: FishjamClient):
102135
assert room in room_api.get_all_rooms()
103136

104137
def test_invalid_max_peers(self, room_api: FishjamClient):
105-
options = RoomOptions(max_peers="10")
138+
options = RoomOptions(max_peers="nan")
106139

107140
with pytest.raises(BadRequestError):
108141
room_api.create_room(options)

0 commit comments

Comments
 (0)