-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-12360 Async HTTP Client #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import pytest | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wondering. Maybe, to avoid repetition, since you seem copied the same tests for both clients, you could parametrize tests with sync/async clients? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. once are shared, but the important ones we need to use await in the http methods |
||
| import respx | ||
| from httpx import ConnectTimeout, Response, codes | ||
|
|
||
| from mpt_api_client.http.client import HTTPClientAsync | ||
| from tests.conftest import API_TOKEN, API_URL | ||
|
|
||
|
|
||
| def test_mpt_client_initialization(): | ||
| client = HTTPClientAsync(base_url=API_URL, api_token=API_TOKEN) | ||
|
|
||
| assert client.base_url == API_URL | ||
| assert client.headers["Authorization"] == "Bearer test-token" | ||
| assert client.headers["User-Agent"] == "swo-marketplace-client/1.0" | ||
|
|
||
|
|
||
| def test_env_initialization(monkeypatch): | ||
| monkeypatch.setenv("MPT_TOKEN", API_TOKEN) | ||
| monkeypatch.setenv("MPT_URL", API_URL) | ||
|
|
||
| client = HTTPClientAsync() | ||
|
|
||
| assert client.base_url == API_URL | ||
| assert client.headers["Authorization"] == f"Bearer {API_TOKEN}" | ||
|
|
||
|
|
||
| def test_mpt_client_without_token(): | ||
| with pytest.raises(ValueError): | ||
| HTTPClientAsync(base_url=API_URL) | ||
|
|
||
|
|
||
| def test_mpt_client_without_url(): | ||
| with pytest.raises(ValueError): | ||
| HTTPClientAsync(api_token=API_TOKEN) | ||
|
|
||
|
|
||
| @respx.mock | ||
| async def test_mock_call_success(http_client_async): | ||
| success_route = respx.get(f"{API_URL}/").mock( | ||
| return_value=Response(200, json={"message": "Hello, World!"}) | ||
| ) | ||
|
|
||
| success_response = await http_client_async.get("/") | ||
|
|
||
| assert success_response.status_code == codes.OK | ||
| assert success_response.json() == {"message": "Hello, World!"} | ||
| assert success_route.called | ||
|
|
||
|
|
||
| @respx.mock | ||
| async def test_mock_call_failure(http_client_async): | ||
| timeout_route = respx.get(f"{API_URL}/timeout").mock(side_effect=ConnectTimeout("Mock Timeout")) | ||
|
|
||
| with pytest.raises(ConnectTimeout): | ||
| await http_client_async.get("/timeout") | ||
|
|
||
| assert timeout_route.called | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems repeating in both classes so could be refactored to a Base Class. I think this is what you did in PR #18?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR #18 is about MPTClient, this one is about HTTPClient.
I would like to share that code, but that code is calling different parent depending on if it is a sync or an async client.
I do not know how to implement an init that calls a different parent depending on which "child" you are using, if you know how, please let me know.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you did it elsewhere, just call
super().__init__()with any params if needed of course