Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 7 additions & 5 deletions _test_unstructured_client/unit/test_custom_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 --------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: false
python:
version: 0.24.1
version: 0.25.0
additionalDependencies:
dependencies:
deepdiff: '>=6.0'
Expand Down
11 changes: 7 additions & 4 deletions src/unstructured_client/_hooks/custom/suggest_defining_url.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down