Skip to content

Commit 50ee715

Browse files
feat(api): manual updates
1 parent 483272e commit 50ee715

3 files changed

Lines changed: 128 additions & 34 deletions

File tree

‎.stats.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
configured_endpoints: 27
22
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/moderation-api%2Fmoderation-api-b71e4bef3bf1d91ff75dc3414d212613e5be0efb29b773ff12363814491fc604.yml
33
openapi_spec_hash: 261c3af6614314fbdd6ea3f7fb61979a
4-
config_hash: 8c2b9aa4fdfe8a809715b93770c6ab0a
4+
config_hash: 3e15855f4c5098c08626c2728c6cbd88

‎README.md‎

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ client = ModerationAPI(
3535
secret_key=os.environ.get("MODAPI_SECRET_KEY"), # This is the default and can be omitted
3636
)
3737

38-
authors = client.authors.list()
39-
print(authors.authors)
38+
response = client.content.submit(
39+
content={
40+
"text": "x",
41+
"type": "text",
42+
},
43+
)
44+
print(response.recommendation)
4045
```
4146

4247
While you can provide a `secret_key` keyword argument,
@@ -59,8 +64,13 @@ client = AsyncModerationAPI(
5964

6065

6166
async def main() -> None:
62-
authors = await client.authors.list()
63-
print(authors.authors)
67+
response = await client.content.submit(
68+
content={
69+
"text": "x",
70+
"type": "text",
71+
},
72+
)
73+
print(response.recommendation)
6474

6575

6676
asyncio.run(main())
@@ -93,8 +103,13 @@ async def main() -> None:
93103
secret_key=os.environ.get("MODAPI_SECRET_KEY"), # This is the default and can be omitted
94104
http_client=DefaultAioHttpClient(),
95105
) as client:
96-
authors = await client.authors.list()
97-
print(authors.authors)
106+
response = await client.content.submit(
107+
content={
108+
"text": "x",
109+
"type": "text",
110+
},
111+
)
112+
print(response.recommendation)
98113

99114

100115
asyncio.run(main())
@@ -141,7 +156,12 @@ from moderation_api import ModerationAPI
141156
client = ModerationAPI()
142157

143158
try:
144-
client.authors.list()
159+
client.content.submit(
160+
content={
161+
"text": "x",
162+
"type": "text",
163+
},
164+
)
145165
except moderation_api.APIConnectionError as e:
146166
print("The server could not be reached")
147167
print(e.__cause__) # an underlying Exception, likely raised within httpx.
@@ -184,7 +204,12 @@ client = ModerationAPI(
184204
)
185205

186206
# Or, configure per-request:
187-
client.with_options(max_retries=5).authors.list()
207+
client.with_options(max_retries=5).content.submit(
208+
content={
209+
"text": "x",
210+
"type": "text",
211+
},
212+
)
188213
```
189214

190215
### Timeouts
@@ -207,7 +232,12 @@ client = ModerationAPI(
207232
)
208233

209234
# Override per-request:
210-
client.with_options(timeout=5.0).authors.list()
235+
client.with_options(timeout=5.0).content.submit(
236+
content={
237+
"text": "x",
238+
"type": "text",
239+
},
240+
)
211241
```
212242

213243
On timeout, an `APITimeoutError` is thrown.
@@ -248,11 +278,16 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
248278
from moderation_api import ModerationAPI
249279

250280
client = ModerationAPI()
251-
response = client.authors.with_raw_response.list()
281+
response = client.content.with_raw_response.submit(
282+
content={
283+
"text": "x",
284+
"type": "text",
285+
},
286+
)
252287
print(response.headers.get('X-My-Header'))
253288

254-
author = response.parse() # get the object that `authors.list()` would have returned
255-
print(author.authors)
289+
content = response.parse() # get the object that `content.submit()` would have returned
290+
print(content.recommendation)
256291
```
257292

258293
These methods return an [`APIResponse`](https://github.com/stainless-sdks/moderation-api-python/tree/main/src/moderation_api/_response.py) object.
@@ -266,7 +301,12 @@ The above interface eagerly reads the full response body when you make the reque
266301
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.
267302

268303
```python
269-
with client.authors.with_streaming_response.list() as response:
304+
with client.content.with_streaming_response.submit(
305+
content={
306+
"text": "x",
307+
"type": "text",
308+
},
309+
) as response:
270310
print(response.headers.get("X-My-Header"))
271311

272312
for line in response.iter_lines():

‎tests/test_client.py‎

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -749,20 +749,30 @@ def test_parse_retry_after_header(
749749
@mock.patch("moderation_api._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
750750
@pytest.mark.respx(base_url=base_url)
751751
def test_retrying_timeout_errors_doesnt_leak(self, respx_mock: MockRouter, client: ModerationAPI) -> None:
752-
respx_mock.get("/authors").mock(side_effect=httpx.TimeoutException("Test timeout error"))
752+
respx_mock.post("/moderate").mock(side_effect=httpx.TimeoutException("Test timeout error"))
753753

754754
with pytest.raises(APITimeoutError):
755-
client.authors.with_streaming_response.list().__enter__()
755+
client.content.with_streaming_response.submit(
756+
content={
757+
"text": "x",
758+
"type": "text",
759+
}
760+
).__enter__()
756761

757762
assert _get_open_connections(client) == 0
758763

759764
@mock.patch("moderation_api._base_client.BaseClient._calculate_retry_timeout", _low_retry_timeout)
760765
@pytest.mark.respx(base_url=base_url)
761766
def test_retrying_status_errors_doesnt_leak(self, respx_mock: MockRouter, client: ModerationAPI) -> None:
762-
respx_mock.get("/authors").mock(return_value=httpx.Response(500))
767+
respx_mock.post("/moderate").mock(return_value=httpx.Response(500))
763768

764769
with pytest.raises(APIStatusError):
765-
client.authors.with_streaming_response.list().__enter__()
770+
client.content.with_streaming_response.submit(
771+
content={
772+
"text": "x",
773+
"type": "text",
774+
}
775+
).__enter__()
766776
assert _get_open_connections(client) == 0
767777

768778
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@@ -789,9 +799,14 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
789799
return httpx.Response(500)
790800
return httpx.Response(200)
791801

792-
respx_mock.get("/authors").mock(side_effect=retry_handler)
802+
respx_mock.post("/moderate").mock(side_effect=retry_handler)
793803

794-
response = client.authors.with_raw_response.list()
804+
response = client.content.with_raw_response.submit(
805+
content={
806+
"text": "x",
807+
"type": "text",
808+
}
809+
)
795810

796811
assert response.retries_taken == failures_before_success
797812
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
@@ -813,9 +828,15 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
813828
return httpx.Response(500)
814829
return httpx.Response(200)
815830

816-
respx_mock.get("/authors").mock(side_effect=retry_handler)
831+
respx_mock.post("/moderate").mock(side_effect=retry_handler)
817832

818-
response = client.authors.with_raw_response.list(extra_headers={"x-stainless-retry-count": Omit()})
833+
response = client.content.with_raw_response.submit(
834+
content={
835+
"text": "x",
836+
"type": "text",
837+
},
838+
extra_headers={"x-stainless-retry-count": Omit()},
839+
)
819840

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

@@ -836,9 +857,15 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
836857
return httpx.Response(500)
837858
return httpx.Response(200)
838859

839-
respx_mock.get("/authors").mock(side_effect=retry_handler)
860+
respx_mock.post("/moderate").mock(side_effect=retry_handler)
840861

841-
response = client.authors.with_raw_response.list(extra_headers={"x-stainless-retry-count": "42"})
862+
response = client.content.with_raw_response.submit(
863+
content={
864+
"text": "x",
865+
"type": "text",
866+
},
867+
extra_headers={"x-stainless-retry-count": "42"},
868+
)
842869

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

@@ -1592,10 +1619,15 @@ async def test_parse_retry_after_header(
15921619
async def test_retrying_timeout_errors_doesnt_leak(
15931620
self, respx_mock: MockRouter, async_client: AsyncModerationAPI
15941621
) -> None:
1595-
respx_mock.get("/authors").mock(side_effect=httpx.TimeoutException("Test timeout error"))
1622+
respx_mock.post("/moderate").mock(side_effect=httpx.TimeoutException("Test timeout error"))
15961623

15971624
with pytest.raises(APITimeoutError):
1598-
await async_client.authors.with_streaming_response.list().__aenter__()
1625+
await async_client.content.with_streaming_response.submit(
1626+
content={
1627+
"text": "x",
1628+
"type": "text",
1629+
}
1630+
).__aenter__()
15991631

16001632
assert _get_open_connections(async_client) == 0
16011633

@@ -1604,10 +1636,15 @@ async def test_retrying_timeout_errors_doesnt_leak(
16041636
async def test_retrying_status_errors_doesnt_leak(
16051637
self, respx_mock: MockRouter, async_client: AsyncModerationAPI
16061638
) -> None:
1607-
respx_mock.get("/authors").mock(return_value=httpx.Response(500))
1639+
respx_mock.post("/moderate").mock(return_value=httpx.Response(500))
16081640

16091641
with pytest.raises(APIStatusError):
1610-
await async_client.authors.with_streaming_response.list().__aenter__()
1642+
await async_client.content.with_streaming_response.submit(
1643+
content={
1644+
"text": "x",
1645+
"type": "text",
1646+
}
1647+
).__aenter__()
16111648
assert _get_open_connections(async_client) == 0
16121649

16131650
@pytest.mark.parametrize("failures_before_success", [0, 2, 4])
@@ -1634,9 +1671,14 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
16341671
return httpx.Response(500)
16351672
return httpx.Response(200)
16361673

1637-
respx_mock.get("/authors").mock(side_effect=retry_handler)
1674+
respx_mock.post("/moderate").mock(side_effect=retry_handler)
16381675

1639-
response = await client.authors.with_raw_response.list()
1676+
response = await client.content.with_raw_response.submit(
1677+
content={
1678+
"text": "x",
1679+
"type": "text",
1680+
}
1681+
)
16401682

16411683
assert response.retries_taken == failures_before_success
16421684
assert int(response.http_request.headers.get("x-stainless-retry-count")) == failures_before_success
@@ -1658,9 +1700,15 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
16581700
return httpx.Response(500)
16591701
return httpx.Response(200)
16601702

1661-
respx_mock.get("/authors").mock(side_effect=retry_handler)
1703+
respx_mock.post("/moderate").mock(side_effect=retry_handler)
16621704

1663-
response = await client.authors.with_raw_response.list(extra_headers={"x-stainless-retry-count": Omit()})
1705+
response = await client.content.with_raw_response.submit(
1706+
content={
1707+
"text": "x",
1708+
"type": "text",
1709+
},
1710+
extra_headers={"x-stainless-retry-count": Omit()},
1711+
)
16641712

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

@@ -1681,9 +1729,15 @@ def retry_handler(_request: httpx.Request) -> httpx.Response:
16811729
return httpx.Response(500)
16821730
return httpx.Response(200)
16831731

1684-
respx_mock.get("/authors").mock(side_effect=retry_handler)
1732+
respx_mock.post("/moderate").mock(side_effect=retry_handler)
16851733

1686-
response = await client.authors.with_raw_response.list(extra_headers={"x-stainless-retry-count": "42"})
1734+
response = await client.content.with_raw_response.submit(
1735+
content={
1736+
"text": "x",
1737+
"type": "text",
1738+
},
1739+
extra_headers={"x-stainless-retry-count": "42"},
1740+
)
16871741

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

0 commit comments

Comments
 (0)