-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for custom auth (#335)
Signed-off-by: Radek Ježek <[email protected]>
- Loading branch information
Showing
2 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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,45 @@ | ||
import logging | ||
|
||
import pytest | ||
from httpx import Request | ||
from httpx._auth import FunctionAuth | ||
from pytest_httpx import HTTPXMock | ||
|
||
from genai import Client, Credentials | ||
from genai.schema import TextGenerationLimitRetrieveEndpoint | ||
from tests.helpers import match_endpoint | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@pytest.mark.unit | ||
class TestApiClient: | ||
def test_default_auth_header(self, patch_generate_limits, httpx_mock: HTTPXMock): | ||
test_api_key = "test_api_key" | ||
client = Client(credentials=Credentials(api_key=test_api_key)) | ||
|
||
client.text.generation.limit.retrieve() | ||
request = httpx_mock.get_request(url=match_endpoint(TextGenerationLimitRetrieveEndpoint)) | ||
|
||
assert request.headers.get("Authorization") == f"Bearer {test_api_key}" | ||
|
||
def test_custom_auth_header(self, patch_generate_limits, httpx_mock: HTTPXMock): | ||
custom_auth_header = "CUSTOM_AUTH_HEADER" | ||
custom_auth_fn_called = False | ||
|
||
def _custom_auth_fn(request: Request) -> Request: | ||
nonlocal custom_auth_fn_called | ||
custom_auth_fn_called = True | ||
request.headers["Authorization"] = custom_auth_header | ||
return request | ||
|
||
client = Client( | ||
credentials=Credentials(api_key="dummy_api_key"), | ||
config={"api_client_config": {"client_options": {"auth": FunctionAuth(_custom_auth_fn)}}}, | ||
) | ||
|
||
client.text.generation.limit.retrieve() | ||
request = httpx_mock.get_request(url=match_endpoint(TextGenerationLimitRetrieveEndpoint)) | ||
|
||
assert custom_auth_fn_called | ||
assert request.headers.get("Authorization") == custom_auth_header |