-
Notifications
You must be signed in to change notification settings - Fork 2
FCE-2044. sdk clients should fail fast #82
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
faf3f5a
add creds valitdation
czerwiukk a22c39c
bump examples
czerwiukk f3134fe
simpllify validation
czerwiukk 68a843a
lint
czerwiukk 26e653d
validate credentials using validate
czerwiukk 4d185b2
just mock the request
czerwiukk c0a452d
format
czerwiukk 2be64e0
fix tests and simplify docstrings
czerwiukk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Contains endpoint functions for accessing the API""" |
91 changes: 91 additions & 0 deletions
91
fishjam/_openapi_client/api/credentials/validate_credentials.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| from http import HTTPStatus | ||
| from typing import Any | ||
|
|
||
| import httpx | ||
|
|
||
| from ... import errors | ||
| from ...client import AuthenticatedClient, Client | ||
| from ...types import Response | ||
|
|
||
|
|
||
| def _get_kwargs() -> dict[str, Any]: | ||
| _kwargs: dict[str, Any] = { | ||
| "method": "get", | ||
| "url": "/validate", | ||
| } | ||
|
|
||
| return _kwargs | ||
|
|
||
|
|
||
| def _parse_response( | ||
| *, client: AuthenticatedClient | Client, response: httpx.Response | ||
| ) -> Any | None: | ||
| if response.status_code == 200: | ||
| return None | ||
|
|
||
| if response.status_code == 404: | ||
| return None | ||
|
|
||
| if client.raise_on_unexpected_status: | ||
| raise errors.UnexpectedStatus(response.status_code, response.content) | ||
| else: | ||
| return None | ||
|
|
||
|
|
||
| def _build_response( | ||
| *, client: AuthenticatedClient | Client, response: httpx.Response | ||
| ) -> Response[Any]: | ||
| return Response( | ||
| status_code=HTTPStatus(response.status_code), | ||
| content=response.content, | ||
| headers=response.headers, | ||
| parsed=_parse_response(client=client, response=response), | ||
| ) | ||
|
|
||
|
|
||
| def sync_detailed( | ||
| *, | ||
| client: AuthenticatedClient, | ||
| ) -> Response[Any]: | ||
| """Validate Fishjam Management Token | ||
|
|
||
| Returns 200 if the provided Fishjam Management Token is valid, 404 otherwise. | ||
|
|
||
| Raises: | ||
| errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. | ||
| httpx.TimeoutException: If the request takes longer than Client.timeout. | ||
|
|
||
| Returns: | ||
| Response[Any] | ||
| """ | ||
|
|
||
| kwargs = _get_kwargs() | ||
|
|
||
| response = client.get_httpx_client().request( | ||
| **kwargs, | ||
| ) | ||
|
|
||
| return _build_response(client=client, response=response) | ||
|
|
||
|
|
||
| async def asyncio_detailed( | ||
| *, | ||
| client: AuthenticatedClient, | ||
| ) -> Response[Any]: | ||
| """Validate Fishjam Management Token | ||
|
|
||
| Returns 200 if the provided Fishjam Management Token is valid, 404 otherwise. | ||
|
|
||
| Raises: | ||
| errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True. | ||
| httpx.TimeoutException: If the request takes longer than Client.timeout. | ||
|
|
||
| Returns: | ||
| Response[Any] | ||
| """ | ||
|
|
||
| kwargs = _get_kwargs() | ||
|
|
||
| response = await client.get_async_httpx_client().request(**kwargs) | ||
|
|
||
| return _build_response(client=client, response=response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # pylint: disable=missing-class-docstring, missing-function-docstring, missing-module-docstring | ||
|
|
||
| from http import HTTPStatus | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from fishjam import FishjamClient | ||
| from fishjam.errors import ( | ||
| InvalidFishjamCredentialsError, | ||
| MissingFishjamIdError, | ||
| ) | ||
|
|
||
| VALID_FISHJAM_ID = "fjm_test" | ||
| VALID_MANAGEMENT_TOKEN = "tok_test" | ||
|
|
||
| VALIDATE = "fishjam.api._fishjam_client.credentials_validate_credentials.sync_detailed" | ||
|
|
||
|
|
||
| def _response(status: HTTPStatus): | ||
| return Mock(status_code=status, headers={}) | ||
|
|
||
|
|
||
| class TestSyncValidation: | ||
| def test_empty_fishjam_id_raises(self): | ||
| with pytest.raises(MissingFishjamIdError): | ||
| FishjamClient(fishjam_id="", management_token=VALID_MANAGEMENT_TOKEN) | ||
|
|
||
| def test_both_provided_does_not_raise(self): | ||
| FishjamClient( | ||
| fishjam_id=VALID_FISHJAM_ID, management_token=VALID_MANAGEMENT_TOKEN | ||
| ) | ||
|
|
||
|
|
||
| class TestLiveCheck: | ||
| def test_create_and_verify_raises_invalid_credentials_on_404(self): | ||
| with patch(VALIDATE, return_value=_response(HTTPStatus.NOT_FOUND)): | ||
| with pytest.raises(InvalidFishjamCredentialsError): | ||
| FishjamClient.create_and_verify( | ||
| fishjam_id=VALID_FISHJAM_ID, | ||
| management_token=VALID_MANAGEMENT_TOKEN, | ||
| ) | ||
|
|
||
| def test_create_and_verify_returns_client_and_pings_once(self): | ||
| with patch(VALIDATE, return_value=_response(HTTPStatus.OK)) as mock_validate: | ||
| client = FishjamClient.create_and_verify( | ||
| fishjam_id=VALID_FISHJAM_ID, | ||
| management_token=VALID_MANAGEMENT_TOKEN, | ||
| ) | ||
|
|
||
| assert isinstance(client, FishjamClient) | ||
| assert mock_validate.call_count == 1 | ||
|
|
||
| def test_check_credentials_raises_invalid_credentials_on_404(self): | ||
| client = FishjamClient( | ||
| fishjam_id=VALID_FISHJAM_ID, management_token=VALID_MANAGEMENT_TOKEN | ||
| ) | ||
| with patch(VALIDATE, return_value=_response(HTTPStatus.NOT_FOUND)): | ||
| with pytest.raises(InvalidFishjamCredentialsError): | ||
| client.check_credentials() | ||
|
|
||
| def test_check_credentials_returns_none_on_success(self): | ||
| client = FishjamClient( | ||
| fishjam_id=VALID_FISHJAM_ID, management_token=VALID_MANAGEMENT_TOKEN | ||
| ) | ||
| with patch(VALIDATE, return_value=_response(HTTPStatus.OK)): | ||
| assert client.check_credentials() is None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.