Skip to content

Commit 7d5ad36

Browse files
committed
add test and refactor
1 parent 9053929 commit 7d5ad36

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

_test_unstructured_client/test_check_url_protocol.py renamed to _test_unstructured_client/test__decorators.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import os
21
import pytest
32

43
from unstructured_client import UnstructuredClient
4+
from unstructured_client.models import shared
5+
from unstructured_client.models.errors import SDKError
56

67

7-
def get_api_key():
8-
api_key = os.getenv("UNS_API_KEY")
9-
if api_key is None:
10-
raise ValueError("""UNS_API_KEY environment variable not set.
11-
Set it in your current shell session with `export UNS_API_KEY=<api_key>`""")
12-
return api_key
8+
FAKE_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
139

1410

1511
@pytest.mark.parametrize(
@@ -25,7 +21,7 @@ def get_api_key():
2521
def test_clean_server_url_on_paid_api_url(server_url: str):
2622
client = UnstructuredClient(
2723
server_url=server_url,
28-
api_key_auth=get_api_key(),
24+
api_key_auth=FAKE_KEY,
2925
)
3026
assert client.general.sdk_configuration.server_url == "https://unstructured-000mock.api.unstructuredapp.io"
3127

@@ -42,18 +38,19 @@ def test_clean_server_url_on_paid_api_url(server_url: str):
4238
def test_clean_server_url_on_localhost(server_url: str):
4339
client = UnstructuredClient(
4440
server_url=server_url,
45-
api_key_auth=get_api_key(),
41+
api_key_auth=FAKE_KEY,
4642
)
4743
assert client.general.sdk_configuration.server_url == "http://localhost:8000"
4844

4945

5046
def test_clean_server_url_on_empty_string():
5147
client = UnstructuredClient(
5248
server_url="",
53-
api_key_auth=get_api_key(),
49+
api_key_auth=FAKE_KEY,
5450
)
5551
assert client.general.sdk_configuration.server_url == ""
5652

53+
5754
@pytest.mark.parametrize(
5855
("server_url"),
5956
[
@@ -63,8 +60,33 @@ def test_clean_server_url_on_empty_string():
6360
)
6461
def test_clean_server_url_with_positional_arguments(server_url: str):
6562
client = UnstructuredClient(
66-
get_api_key(),
63+
FAKE_KEY,
6764
"",
6865
server_url,
6966
)
7067
assert client.general.sdk_configuration.server_url == "https://unstructured-000mock.api.unstructuredapp.io"
68+
69+
70+
def test_suggest_defining_url_if_401():
71+
with pytest.warns(UserWarning):
72+
73+
client = UnstructuredClient(
74+
api_key_auth=FAKE_KEY,
75+
)
76+
77+
filename = "_sample_docs/layout-parser-paper-fast.pdf"
78+
79+
with open(filename, "rb") as f:
80+
files=shared.Files(
81+
content=f.read(),
82+
file_name=filename,
83+
)
84+
85+
req = shared.PartitionParameters(
86+
files=files,
87+
)
88+
89+
try:
90+
client.general.partition(req)
91+
except SDKError as e:
92+
print(e)

src/unstructured_client/utils/_decorators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from urllib.parse import urlparse, urlunparse, ParseResult
77
import warnings
88

9-
from unstructured_client.models import errors
9+
from unstructured_client.models import errors, operations
1010

1111
if TYPE_CHECKING:
1212
from unstructured_client.general import General
@@ -52,14 +52,14 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
5252
return wrapper
5353

5454

55-
def suggest_defining_url_if_401(func: Callable[_P, None]) -> Callable[_P, None]:
55+
def suggest_defining_url_if_401(func: Callable[_P, operations.PartitionResponse]) -> Callable[_P, operations.PartitionResponse]:
5656

5757
@functools.wraps(func)
58-
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
58+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> operations.PartitionResponse:
5959
try:
6060
return func(*args, **kwargs)
6161
except errors.SDKError:
62-
general_obj: General = args[0]
62+
general_obj: General = args[0] # type: ignore
6363
if not general_obj.sdk_configuration.server_url:
6464
warnings.warn("If intending to use the paid API, please define `server_url` in your request.")
6565
return func(*args, **kwargs)

0 commit comments

Comments
 (0)