Skip to content

Commit eb86cbb

Browse files
committed
address comments
1 parent b4a7c8f commit eb86cbb

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

_test_unstructured_client/test__decorators.py

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99

1010

1111
@pytest.mark.parametrize(
12-
("server_url"),
12+
"server_url",
1313
[
14-
("https://unstructured-000mock.api.unstructuredapp.io"), # correct url
15-
("unstructured-000mock.api.unstructuredapp.io"),
16-
("http://unstructured-000mock.api.unstructuredapp.io/general/v0/general"),
17-
("https://unstructured-000mock.api.unstructuredapp.io/general/v0/general"),
18-
("unstructured-000mock.api.unstructuredapp.io/general/v0/general"),
14+
# -- well-formed url --
15+
"https://unstructured-000mock.api.unstructuredapp.io",
16+
# -- common malformed urls --
17+
"unstructured-000mock.api.unstructuredapp.io",
18+
"http://unstructured-000mock.api.unstructuredapp.io/general/v0/general",
19+
"https://unstructured-000mock.api.unstructuredapp.io/general/v0/general",
20+
"unstructured-000mock.api.unstructuredapp.io/general/v0/general",
1921
],
2022
)
21-
def test_clean_server_url_on_paid_api_url(server_url: str):
23+
def test_clean_server_url_fixes_malformed_paid_api_url(server_url: str):
2224
client = UnstructuredClient(
2325
server_url=server_url,
2426
api_key_auth=FAKE_KEY,
@@ -30,38 +32,51 @@ def test_clean_server_url_on_paid_api_url(server_url: str):
3032

3133

3234
@pytest.mark.parametrize(
33-
("server_url"),
35+
"server_url",
3436
[
35-
("http://localhost:8000"), # correct url
36-
("localhost:8000"),
37-
("localhost:8000/general/v0/general"),
38-
("http://localhost:8000/general/v0/general"),
37+
# -- well-formed url --
38+
"http://localhost:8000",
39+
# -- common malformed urls --
40+
"localhost:8000",
41+
"localhost:8000/general/v0/general",
42+
"http://localhost:8000/general/v0/general",
3943
],
4044
)
41-
def test_clean_server_url_on_localhost(server_url: str):
45+
def test_clean_server_url_fixes_malformed_localhost_url(server_url: str):
4246
client = UnstructuredClient(
4347
server_url=server_url,
4448
api_key_auth=FAKE_KEY,
4549
)
4650
assert client.general.sdk_configuration.server_url == "http://localhost:8000"
4751

4852

49-
def test_clean_server_url_on_empty_string():
53+
def test_clean_server_url_returns_empty_string_given_empty_string():
5054
client = UnstructuredClient(
5155
server_url="",
5256
api_key_auth=FAKE_KEY,
5357
)
5458
assert client.general.sdk_configuration.server_url == ""
5559

5660

61+
def test_clean_server_url_returns_None_given_no_server_url():
62+
client = UnstructuredClient(
63+
api_key_auth=FAKE_KEY,
64+
)
65+
assert client.general.sdk_configuration.server_url == None
66+
67+
5768
@pytest.mark.parametrize(
58-
("server_url"),
69+
"server_url",
5970
[
60-
("https://unstructured-000mock.api.unstructuredapp.io"),
61-
("unstructured-000mock.api.unstructuredapp.io/general/v0/general"),
71+
# -- well-formed url --
72+
"https://unstructured-000mock.api.unstructuredapp.io",
73+
# -- malformed url --
74+
"unstructured-000mock.api.unstructuredapp.io/general/v0/general",
6275
],
6376
)
64-
def test_clean_server_url_with_positional_arguments(server_url: str):
77+
def test_clean_server_url_fixes_malformed_urls_with_positional_arguments(
78+
server_url: str,
79+
):
6580
client = UnstructuredClient(
6681
FAKE_KEY,
6782
"",
@@ -73,26 +88,26 @@ def test_clean_server_url_with_positional_arguments(server_url: str):
7388
)
7489

7590

76-
def test_suggest_defining_url_if_401():
77-
with pytest.warns(UserWarning):
78-
79-
client = UnstructuredClient(
80-
api_key_auth=FAKE_KEY,
81-
)
82-
83-
filename = "_sample_docs/layout-parser-paper-fast.pdf"
91+
def test_suggest_defining_url_issues_a_warning_on_a_401():
92+
client = UnstructuredClient(
93+
api_key_auth=FAKE_KEY,
94+
)
8495

85-
with open(filename, "rb") as f:
86-
files = shared.Files(
87-
content=f.read(),
88-
file_name=filename,
89-
)
96+
filename = "_sample_docs/layout-parser-paper-fast.pdf"
9097

91-
req = shared.PartitionParameters(
92-
files=files,
98+
with open(filename, "rb") as f:
99+
files = shared.Files(
100+
content=f.read(),
101+
file_name=filename,
93102
)
94103

95-
try:
104+
req = shared.PartitionParameters(
105+
files=files,
106+
)
107+
108+
with pytest.raises(SDKError, match="API error occurred: Status 401"):
109+
with pytest.warns(
110+
UserWarning,
111+
match="If intending to use the paid API, please define `server_url` in your request.",
112+
):
96113
client.general.partition(req)
97-
except SDKError as e:
98-
print(e)

src/unstructured_client/utils/_decorators.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717

1818
def clean_server_url(func: Callable[_P, None]) -> Callable[_P, None]:
19+
"""A decorator for fixing common types of malformed 'server_url' arguments.
20+
21+
This decorator addresses the common problem of users omitting or using the wrong url scheme and/or
22+
adding the '/general/v0/general' path to the 'server_url'.
23+
The decorator should be manually applied to the __init__ method of UnstructuredClient after merging
24+
a PR from Speakeasy.
25+
"""
1926

2027
@functools.wraps(func)
2128
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
@@ -55,7 +62,11 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
5562
def suggest_defining_url_if_401(
5663
func: Callable[_P, operations.PartitionResponse]
5764
) -> Callable[_P, operations.PartitionResponse]:
58-
65+
"""A decorator to suggest defining the 'server_url' parameter if a 401 Unauthorized error is encountered.
66+
67+
This decorator addresses the common problem of users not passing in the 'server_url' when using their paid api key.
68+
The decorator should be manually applied to General.partition after merging a PR from Speakeasy.
69+
"""
5970
@functools.wraps(func)
6071
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> operations.PartitionResponse:
6172
try:

0 commit comments

Comments
 (0)