diff --git a/_test_unstructured_client/integration/test_integration_freemium.py b/_test_unstructured_client/integration/test_integration_freemium.py index c81493af..659c7b4b 100644 --- a/_test_unstructured_client/integration/test_integration_freemium.py +++ b/_test_unstructured_client/integration/test_integration_freemium.py @@ -12,7 +12,7 @@ @pytest.fixture(scope="module") def client() -> UnstructuredClient: - _client = UnstructuredClient(api_key_auth=os.getenv("UNSTRUCTURED_API_KEY")) + _client = UnstructuredClient(api_key_auth=os.getenv("UNSTRUCTURED_API_KEY"), server='free-api') yield _client diff --git a/_test_unstructured_client/unit/test_custom_hooks.py b/_test_unstructured_client/unit/test_custom_hooks.py index a69b1f8c..d5dcd7df 100644 --- a/_test_unstructured_client/unit/test_custom_hooks.py +++ b/_test_unstructured_client/unit/test_custom_hooks.py @@ -165,7 +165,7 @@ def test_unit_clean_server_url_fixes_malformed_urls_with_positional_arguments(se ) -def test_unit_issues_warning_on_a_401(session_: Mock, response_: requests.Session): +def test_unit_issues_warning_on_a_401(caplog, session_: Mock, response_: requests.Session): client = UnstructuredClient(api_key_auth=FAKE_KEY) session_.return_value = response_ filename = "_sample_docs/layout-parser-paper-fast.pdf" @@ -175,12 +175,14 @@ def test_unit_issues_warning_on_a_401(session_: Mock, response_: requests.Sessio req = shared.PartitionParameters(files=files) with pytest.raises(SDKError, match="API error occurred: Status 401"): - with pytest.warns( - UserWarning, - match="If intending to use the paid API, please define `server_url` in your request.", - ): + with caplog.at_level(logging.WARNING): client.general.partition(req) + assert any( + "This API key is invalid against the paid API. If intending to use the free API, please initialize UnstructuredClient with `server='free-api'`." + in message for message in caplog.messages + ) + # -- fixtures -------------------------------------------------------------------------------- diff --git a/gen.yaml b/gen.yaml index e0c43f34..0e68e11f 100644 --- a/gen.yaml +++ b/gen.yaml @@ -10,7 +10,7 @@ generation: auth: oAuth2ClientCredentialsEnabled: false python: - version: 0.24.1 + version: 0.25.0 additionalDependencies: dependencies: deepdiff: '>=6.0' diff --git a/src/unstructured_client/_hooks/custom/suggest_defining_url.py b/src/unstructured_client/_hooks/custom/suggest_defining_url.py index e0774c83..d9268159 100644 --- a/src/unstructured_client/_hooks/custom/suggest_defining_url.py +++ b/src/unstructured_client/_hooks/custom/suggest_defining_url.py @@ -1,19 +1,22 @@ from typing import Optional, Tuple, Union -import warnings +import logging import requests +from unstructured_client._hooks.custom.common import UNSTRUCTURED_CLIENT_LOGGER_NAME from unstructured_client._hooks.types import AfterErrorContext, AfterErrorHook +logger = logging.getLogger(UNSTRUCTURED_CLIENT_LOGGER_NAME) + class SuggestDefiningUrlIf401AfterErrorHook(AfterErrorHook): """Hook advising users to check that 'server_url' is defined if a 401 error is encountered.""" def warn_if_401(self, response: Optional[requests.Response]): - """Suggest defining 'server_url' if a 401 error is encountered.""" + """If the paid API returns 401, warn the user in case they meant to use the free api.""" if response is not None and response.status_code == 401: - warnings.warn( - "If intending to use the paid API, please define `server_url` in your request." + logger.warning( + "This API key is invalid against the paid API. If intending to use the free API, please initialize UnstructuredClient with `server='free-api'`." ) def after_error(