Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 24 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It is generated with [Stainless](https://www.stainlessapi.com/).

## Documentation

The REST API documentation can be found on [docs.zeroentropy.dev](https://docs.zeroentropy.dev). The full API of this library can be found in [api.md](api.md).
The REST API documentation can be found on [docs.zeroentropy.dev](https://docs.zeroentropy.dev/api-reference). The full API of this library can be found in [api.md](api.md).

## Installation

Expand All @@ -31,11 +31,15 @@ client = Zeroentropy(
api_key=os.environ.get("ZEROENTROPY_API_KEY"), # This is the default and can be omitted
)

response = client.documents.get_info(
collection_name="collection_name",
path="path",
response = client.documents.add(
collection_name="example_collection",
content={
"type": "text",
"text": "Example Content",
},
path="my_document.txt",
)
print(response.document)
print(response.message)
```

While you can provide an `api_key` keyword argument,
Expand All @@ -58,11 +62,15 @@ client = AsyncZeroentropy(


async def main() -> None:
response = await client.documents.get_info(
collection_name="collection_name",
path="path",
response = await client.documents.add(
collection_name="example_collection",
content={
"type": "text",
"text": "Example Content",
},
path="my_document.txt",
)
print(response.document)
print(response.message)


asyncio.run(main())
Expand Down Expand Up @@ -166,10 +174,7 @@ from zeroentropy import Zeroentropy
client = Zeroentropy()

try:
client.documents.get_info(
collection_name="collection_name",
path="path",
)
client.status.get_status()
except zeroentropy.APIConnectionError as e:
print("The server could not be reached")
print(e.__cause__) # an underlying Exception, likely raised within httpx.
Expand Down Expand Up @@ -212,10 +217,7 @@ client = Zeroentropy(
)

# Or, configure per-request:
client.with_options(max_retries=5).documents.get_info(
collection_name="collection_name",
path="path",
)
client.with_options(max_retries=5).status.get_status()
```

### Timeouts
Expand All @@ -238,10 +240,7 @@ client = Zeroentropy(
)

# Override per-request:
client.with_options(timeout=5.0).documents.get_info(
collection_name="collection_name",
path="path",
)
client.with_options(timeout=5.0).status.get_status()
```

On timeout, an `APITimeoutError` is thrown.
Expand Down Expand Up @@ -282,14 +281,11 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
from zeroentropy import Zeroentropy

client = Zeroentropy()
response = client.documents.with_raw_response.get_info(
collection_name="collection_name",
path="path",
)
response = client.status.with_raw_response.get_status()
print(response.headers.get('X-My-Header'))

document = response.parse() # get the object that `documents.get_info()` would have returned
print(document.document)
status = response.parse() # get the object that `status.get_status()` would have returned
print(status.num_documents)
```

These methods return an [`APIResponse`](https://github.com/ZeroEntropy-AI/zeroentropy-python/tree/main/src/zeroentropy/_response.py) object.
Expand All @@ -303,10 +299,7 @@ The above interface eagerly reads the full response body when you make the reque
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.

```python
with client.documents.with_streaming_response.get_info(
collection_name="collection_name",
path="path",
) as response:
with client.status.with_streaming_response.get_status() as response:
print(response.headers.get("X-My-Header"))

for line in response.iter_lines():
Expand Down
56 changes: 24 additions & 32 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ def test_parse_retry_after_header(self, remaining_retries: int, retry_after: str
@mock.patch("zeroentropy._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
respx_mock.post("/documents/get-document-info").mock(side_effect=httpx.TimeoutException("Test timeout error"))
respx_mock.post("/status/get-status").mock(side_effect=httpx.TimeoutException("Test timeout error"))

with pytest.raises(APITimeoutError):
self.client.post(
"/documents/get-document-info",
body=cast(object, dict(collection_name="collection_name", path="path")),
"/status/get-status",
body=cast(object, dict()),
cast_to=httpx.Response,
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
)
Expand All @@ -736,12 +736,12 @@ def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> No
@mock.patch("zeroentropy._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
respx_mock.post("/documents/get-document-info").mock(return_value=httpx.Response(500))
respx_mock.post("/status/get-status").mock(return_value=httpx.Response(500))

with pytest.raises(APIStatusError):
self.client.post(
"/documents/get-document-info",
body=cast(object, dict(collection_name="collection_name", path="path")),
"/status/get-status",
body=cast(object, dict()),
cast_to=httpx.Response,
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
)
Expand Down Expand Up @@ -772,9 +772,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(500)
return httpx.Response(200)

respx_mock.post("/documents/get-document-info").mock(side_effect=retry_handler)
respx_mock.post("/status/get-status").mock(side_effect=retry_handler)

response = client.documents.with_raw_response.get_info(collection_name="collection_name", path="path")
response = client.status.with_raw_response.get_status()

assert response.retries_taken == failures_before_success
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
Expand All @@ -796,11 +796,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(500)
return httpx.Response(200)

respx_mock.post("/documents/get-document-info").mock(side_effect=retry_handler)
respx_mock.post("/status/get-status").mock(side_effect=retry_handler)

response = client.documents.with_raw_response.get_info(
collection_name="collection_name", path="path", extra_headers={"x-stainless-retry-count": Omit()}
)
response = client.status.with_raw_response.get_status(extra_headers={"x-stainless-retry-count": Omit()})

assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0

Expand All @@ -821,11 +819,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(500)
return httpx.Response(200)

respx_mock.post("/documents/get-document-info").mock(side_effect=retry_handler)
respx_mock.post("/status/get-status").mock(side_effect=retry_handler)

response = client.documents.with_raw_response.get_info(
collection_name="collection_name", path="path", extra_headers={"x-stainless-retry-count": "42"}
)
response = client.status.with_raw_response.get_status(extra_headers={"x-stainless-retry-count": "42"})

assert response.http_request.headers.get("x-stainless-retry-count") == "42"

Expand Down Expand Up @@ -1501,12 +1497,12 @@ async def test_parse_retry_after_header(self, remaining_retries: int, retry_afte
@mock.patch("zeroentropy._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
respx_mock.post("/documents/get-document-info").mock(side_effect=httpx.TimeoutException("Test timeout error"))
respx_mock.post("/status/get-status").mock(side_effect=httpx.TimeoutException("Test timeout error"))

with pytest.raises(APITimeoutError):
await self.client.post(
"/documents/get-document-info",
body=cast(object, dict(collection_name="collection_name", path="path")),
"/status/get-status",
body=cast(object, dict()),
cast_to=httpx.Response,
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
)
Expand All @@ -1516,12 +1512,12 @@ async def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter)
@mock.patch("zeroentropy._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
@pytest.mark.respx(base_url=base_url)
async def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter) -> None:
respx_mock.post("/documents/get-document-info").mock(return_value=httpx.Response(500))
respx_mock.post("/status/get-status").mock(return_value=httpx.Response(500))

with pytest.raises(APIStatusError):
await self.client.post(
"/documents/get-document-info",
body=cast(object, dict(collection_name="collection_name", path="path")),
"/status/get-status",
body=cast(object, dict()),
cast_to=httpx.Response,
options={"headers": {RAW_RESPONSE_HEADER: "stream"}},
)
Expand Down Expand Up @@ -1553,9 +1549,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(500)
return httpx.Response(200)

respx_mock.post("/documents/get-document-info").mock(side_effect=retry_handler)
respx_mock.post("/status/get-status").mock(side_effect=retry_handler)

response = await client.documents.with_raw_response.get_info(collection_name="collection_name", path="path")
response = await client.status.with_raw_response.get_status()

assert response.retries_taken == failures_before_success
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
Expand All @@ -1578,11 +1574,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(500)
return httpx.Response(200)

respx_mock.post("/documents/get-document-info").mock(side_effect=retry_handler)
respx_mock.post("/status/get-status").mock(side_effect=retry_handler)

response = await client.documents.with_raw_response.get_info(
collection_name="collection_name", path="path", extra_headers={"x-stainless-retry-count": Omit()}
)
response = await client.status.with_raw_response.get_status(extra_headers={"x-stainless-retry-count": Omit()})

assert len(response.http_request.headers.get_list("x-stainless-retry-count")) == 0

Expand All @@ -1604,11 +1598,9 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
return httpx.Response(500)
return httpx.Response(200)

respx_mock.post("/documents/get-document-info").mock(side_effect=retry_handler)
respx_mock.post("/status/get-status").mock(side_effect=retry_handler)

response = await client.documents.with_raw_response.get_info(
collection_name="collection_name", path="path", extra_headers={"x-stainless-retry-count": "42"}
)
response = await client.status.with_raw_response.get_status(extra_headers={"x-stainless-retry-count": "42"})

assert response.http_request.headers.get("x-stainless-retry-count") == "42"

Expand Down