-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix/Address server_url breaking change #226
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3cd2ce2
Edit autogenerated endpoints to fix server_url issue
awalker4 8c5b8f8
Add unit tests for all server_url cases
awalker4 94b3b9f
Fix typo
awalker4 a38e446
Use the correct url in contract tests
awalker4 700a0e3
Make server_url tests reusable
awalker4 7e92477
Make sure Speakeasy generates the partition spec second
awalker4 bb503ba
Add endpoint files to .genignore for now
awalker4 f21ac18
Undo server_url changes for platform
awalker4 8625982
Refactor server url helper functions
awalker4 77f982c
Fix typing error
awalker4 8f0b655
Fix unit tests
awalker4 5926550
Don't run this one test for now
awalker4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
import httpx | ||
import pytest | ||
|
||
from unstructured_client.models import operations | ||
from unstructured_client import UnstructuredClient, basesdk, utils | ||
|
||
|
||
# Raise one of these from our mock to return to the test code | ||
class BaseUrlCorrect(Exception): | ||
pass | ||
|
||
|
||
class BaseUrlIncorrect(Exception): | ||
pass | ||
|
||
|
||
def get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
client_instance, | ||
mocked_server_url, | ||
monkeypatch | ||
): | ||
""" | ||
Given an endpoint name, e.g. "general.partition", return a reference | ||
to that method off of the given client instance. | ||
|
||
The client's _build_request will have the following mock: | ||
Assert that the provided server_url is passed into _build_request. | ||
Raise a custom exception to get back to the test. | ||
""" | ||
# Mock this to get past param validation | ||
def mock_unmarshal(*args, **kwargs): | ||
return {} | ||
|
||
monkeypatch.setattr(utils, "unmarshal", mock_unmarshal) | ||
|
||
# Assert that the correct base_url makes it to here | ||
def mock_build_request(*args, base_url, **kwargs): | ||
if base_url == mocked_server_url: | ||
raise BaseUrlCorrect | ||
else: | ||
raise BaseUrlIncorrect(base_url) | ||
|
||
# Find the method from the given string | ||
class_name, method_name = sdk_endpoint_name.split(".") | ||
endpoint_class = getattr(client_instance, class_name) | ||
endpoint_method = getattr(endpoint_class, method_name) | ||
|
||
if "async" in method_name: | ||
monkeypatch.setattr(endpoint_class, "_build_request_async", mock_build_request) | ||
else: | ||
monkeypatch.setattr(endpoint_class, "_build_request", mock_build_request) | ||
|
||
return endpoint_method | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"sdk_endpoint_name", | ||
[ | ||
("general.partition"), | ||
], | ||
) | ||
def test_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): | ||
# Test 1 | ||
# Pass server_url to the client, no path | ||
s = UnstructuredClient(server_url="http://localhost:8000") | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
client_method(request={}) | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to client and ignored, got {e}") | ||
|
||
# Test 2 | ||
# Pass server_url to the client, with path | ||
s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
client_method(request={}) | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to client and was not stripped, got {e}") | ||
|
||
# Test 3 | ||
# Pass server_url to the endpoint, no path | ||
s = UnstructuredClient() | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
client_method(request={}, server_url="http://localhost:8000") | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") | ||
|
||
# Test 4 | ||
# Pass server_url to the endpoint, with path | ||
s = UnstructuredClient() | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
client_method(request={}, server_url="http://localhost:8000/my/endpoint") | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") | ||
|
||
|
||
# Test 5 | ||
# No server_url, should take the default | ||
server_url = "https://api.unstructuredapp.io" if "partition" in sdk_endpoint_name else "https://platform.unstructuredapp.io" | ||
|
||
s = UnstructuredClient() | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
server_url, | ||
monkeypatch | ||
) | ||
|
||
try: | ||
client_method(request={}) | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"Default url not used, got {e}") | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize( | ||
"sdk_endpoint_name", | ||
[ | ||
("general.partition_async"), | ||
], | ||
) | ||
async def test_async_endpoint_uses_correct_url(monkeypatch, sdk_endpoint_name): | ||
# Test 1 | ||
# Pass server_url to the client, no path | ||
s = UnstructuredClient(server_url="http://localhost:8000") | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
await client_method(request={}) | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to client and ignored, got {e}") | ||
|
||
# Test 2 | ||
# Pass server_url to the client, with path | ||
s = UnstructuredClient(server_url="http://localhost:8000/my/endpoint") | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
await client_method(request={}) | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to client and was not stripped, got {e}") | ||
|
||
# Test 3 | ||
# Pass server_url to the endpoint, no path | ||
s = UnstructuredClient() | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
await client_method(request={}, server_url="http://localhost:8000") | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") | ||
|
||
# Test 4 | ||
# Pass server_url to the endpoint, with path | ||
s = UnstructuredClient() | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
"http://localhost:8000", | ||
monkeypatch | ||
) | ||
|
||
try: | ||
await client_method(request={}, server_url="http://localhost:8000/my/endpoint") | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"server_url was passed to endpoint and ignored, got {e}") | ||
|
||
|
||
# Test 5 | ||
# No server_url, should take the default | ||
server_url = "https://api.unstructuredapp.io" if "partition" in sdk_endpoint_name else "https://platform.unstructuredapp.io" | ||
|
||
s = UnstructuredClient() | ||
client_method = get_client_method_with_mock( | ||
sdk_endpoint_name, | ||
s, | ||
server_url, | ||
monkeypatch | ||
) | ||
|
||
try: | ||
await client_method(request={}) | ||
except BaseUrlCorrect: | ||
pass | ||
except BaseUrlIncorrect as e: | ||
pytest.fail(f"Default url not used, got {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would parse the URL first and then check if a url has a scheme by using
parsed_url.scheme
. Then we can prependhttp://
orhttps://
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That comment has been around for a while - I tried parsing without the scheme and it does look like we need to make sure
http://
is there first or it gets confused.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, good to know!