Skip to content

Commit 33039aa

Browse files
authored
chore: update 401 warning hook for new default url (#139)
The default server url is changing to serverless. Therefore, if you get a 401, we should suggest that you meant to use the free api. Also, bump the minor version in anticipation of the url change.
1 parent 977063e commit 33039aa

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

_test_unstructured_client/integration/test_integration_freemium.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@pytest.fixture(scope="module")
1414
def client() -> UnstructuredClient:
15-
_client = UnstructuredClient(api_key_auth=os.getenv("UNSTRUCTURED_API_KEY"))
15+
_client = UnstructuredClient(api_key_auth=os.getenv("UNSTRUCTURED_API_KEY"), server='free-api')
1616
yield _client
1717

1818

_test_unstructured_client/unit/test_custom_hooks.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_unit_clean_server_url_fixes_malformed_urls_with_positional_arguments(se
165165
)
166166

167167

168-
def test_unit_issues_warning_on_a_401(session_: Mock, response_: requests.Session):
168+
def test_unit_issues_warning_on_a_401(caplog, session_: Mock, response_: requests.Session):
169169
client = UnstructuredClient(api_key_auth=FAKE_KEY)
170170
session_.return_value = response_
171171
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
175175
req = shared.PartitionParameters(files=files)
176176

177177
with pytest.raises(SDKError, match="API error occurred: Status 401"):
178-
with pytest.warns(
179-
UserWarning,
180-
match="If intending to use the paid API, please define `server_url` in your request.",
181-
):
178+
with caplog.at_level(logging.WARNING):
182179
client.general.partition(req)
183180

181+
assert any(
182+
"This API key is invalid against the paid API. If intending to use the free API, please initialize UnstructuredClient with `server='free-api'`."
183+
in message for message in caplog.messages
184+
)
185+
184186

185187
# -- fixtures --------------------------------------------------------------------------------
186188

gen.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ generation:
1010
auth:
1111
oAuth2ClientCredentialsEnabled: false
1212
python:
13-
version: 0.24.1
13+
version: 0.25.0
1414
additionalDependencies:
1515
dependencies:
1616
deepdiff: '>=6.0'

src/unstructured_client/_hooks/custom/suggest_defining_url.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
from typing import Optional, Tuple, Union
2-
import warnings
32

3+
import logging
44
import requests
55

6+
from unstructured_client._hooks.custom.common import UNSTRUCTURED_CLIENT_LOGGER_NAME
67
from unstructured_client._hooks.types import AfterErrorContext, AfterErrorHook
78

9+
logger = logging.getLogger(UNSTRUCTURED_CLIENT_LOGGER_NAME)
10+
811

912
class SuggestDefiningUrlIf401AfterErrorHook(AfterErrorHook):
1013
"""Hook advising users to check that 'server_url' is defined if a 401 error is encountered."""
1114

1215
def warn_if_401(self, response: Optional[requests.Response]):
13-
"""Suggest defining 'server_url' if a 401 error is encountered."""
16+
"""If the paid API returns 401, warn the user in case they meant to use the free api."""
1417
if response is not None and response.status_code == 401:
15-
warnings.warn(
16-
"If intending to use the paid API, please define `server_url` in your request."
18+
logger.warning(
19+
"This API key is invalid against the paid API. If intending to use the free API, please initialize UnstructuredClient with `server='free-api'`."
1720
)
1821

1922
def after_error(

0 commit comments

Comments
 (0)