Skip to content
7 changes: 7 additions & 0 deletions .genignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ _test_unstructured_client

# ignore Makefile
Makefile

# Ignore the endpoint files until we can fix the base_url issue
src/unstructured_client/destinations.py
src/unstructured_client/general.py
src/unstructured_client/jobs.py
src/unstructured_client/sources.py
src/unstructured_client/workflows.py
2 changes: 1 addition & 1 deletion .speakeasy/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ speakeasyVersion: latest
sources:
my-source:
inputs:
- location: https://api.unstructured.io/general/openapi.json
- location: https://platform.unstructuredapp.io/openapi.json
- location: https://api.unstructured.io/general/openapi.json
overlays:
- location: ./overlay_client.yaml
registry:
Expand Down
2 changes: 1 addition & 1 deletion _test_contract/platform_api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def platform_api_url():
def client(platform_api_url) -> UnstructuredClient:
_client = UnstructuredClient(
api_key_auth=FAKE_API_KEY,
server_url="platform-api",
server_url=platform_api_url,
retry_config=RetryConfig(
strategy="backoff",
retry_connection_errors=False,
Expand Down
285 changes: 285 additions & 0 deletions _test_unstructured_client/unit/test_server_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
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"),
("destinations.create_destination"),
("destinations.delete_destination"),
("destinations.get_destination"),
("destinations.list_destinations"),
("destinations.update_destination"),
("jobs.cancel_job"),
("jobs.get_job"),
("jobs.list_jobs"),
("sources.create_source"),
("sources.delete_source"),
("sources.get_source"),
("sources.list_sources"),
("sources.update_source"),
("workflows.create_workflow"),
("workflows.delete_workflow"),
("workflows.get_workflow"),
("workflows.list_workflows"),
("workflows.run_workflow"),
("workflows.update_workflow"),
],
)
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"),
("destinations.create_destination_async"),
("destinations.delete_destination_async"),
("destinations.get_destination_async"),
("destinations.list_destinations_async"),
("destinations.update_destination_async"),
("jobs.cancel_job_async"),
("jobs.get_job_async"),
("jobs.list_jobs_async"),
("sources.create_source_async"),
("sources.delete_source_async"),
("sources.get_source_async"),
("sources.list_sources_async"),
("sources.update_source_async"),
("workflows.create_workflow_async"),
("workflows.delete_workflow_async"),
("workflows.get_workflow_async"),
("workflows.list_workflows_async"),
("workflows.run_workflow_async"),
("workflows.update_workflow_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}")
6 changes: 3 additions & 3 deletions docs/models/errors/httpvalidationerror.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

## Fields

| Field | Type | Required | Description |
| ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `detail` | List[[shared.ValidationError](../../models/shared/validationerror.md)] | :heavy_minus_sign: | N/A |
| Field | Type | Required | Description |
| -------------------------------------------------------- | -------------------------------------------------------- | -------------------------------------------------------- | -------------------------------------------------------- |
| `detail` | [Optional[errors.Detail]](../../models/errors/detail.md) | :heavy_minus_sign: | N/A |
Loading