unprofane 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unprofane-0.1.0/LICENSE +21 -0
- unprofane-0.1.0/PKG-INFO +49 -0
- unprofane-0.1.0/README.md +27 -0
- unprofane-0.1.0/pyproject.toml +37 -0
- unprofane-0.1.0/tests/test_client.py +97 -0
- unprofane-0.1.0/unprofane/__init__.py +5 -0
- unprofane-0.1.0/unprofane/client.py +228 -0
unprofane-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tomer N. Marketing and Sales Ltd
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
unprofane-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: unprofane
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Unprofane: a profanity and toxicity API that is hard to fool. Sync and async client with retries and a batch helper.
|
|
5
|
+
Project-URL: Homepage, https://unprofane.com
|
|
6
|
+
Project-URL: Documentation, https://unprofane.com/docs/
|
|
7
|
+
Project-URL: Source, https://github.com/unprofane/unprofane-python
|
|
8
|
+
Project-URL: Issues, https://github.com/unprofane/unprofane-python/issues
|
|
9
|
+
Author-email: Unprofane <hello@unprofane.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: filter,moderation,perspective-api,profanity,toxicity
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Requires-Dist: httpx>=0.25
|
|
18
|
+
Provides-Extra: test
|
|
19
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'test'
|
|
20
|
+
Requires-Dist: pytest>=8; extra == 'test'
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# unprofane
|
|
24
|
+
|
|
25
|
+
The Python client for [Unprofane](https://unprofane.com), a profanity and toxicity API that is hard to fool.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install unprofane
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from unprofane import Unprofane
|
|
33
|
+
|
|
34
|
+
client = Unprofane("up_live_YOUR_KEY")
|
|
35
|
+
r = client.check("xXsh1tXx", context="username", policy="teen")
|
|
36
|
+
if not r["allowed"]:
|
|
37
|
+
print(r["action"], r["evasion"], r["censored"])
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Async:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from unprofane import AsyncUnprofane
|
|
44
|
+
|
|
45
|
+
async with AsyncUnprofane("up_live_YOUR_KEY") as client:
|
|
46
|
+
r = await client.check("hello", context="comment")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`check`, `check_batch` (up to 100), `check_many` (any number, chunked), `feedback`, `usage`, `get_list`, `put_list`, `delete_list`. Retries 429 and 5xx with jitter and honours `Retry-After`; other failures raise `UnprofaneError` with the problem details and the request id. Docs: https://unprofane.com/docs/.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# unprofane
|
|
2
|
+
|
|
3
|
+
The Python client for [Unprofane](https://unprofane.com), a profanity and toxicity API that is hard to fool.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install unprofane
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from unprofane import Unprofane
|
|
11
|
+
|
|
12
|
+
client = Unprofane("up_live_YOUR_KEY")
|
|
13
|
+
r = client.check("xXsh1tXx", context="username", policy="teen")
|
|
14
|
+
if not r["allowed"]:
|
|
15
|
+
print(r["action"], r["evasion"], r["censored"])
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Async:
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from unprofane import AsyncUnprofane
|
|
22
|
+
|
|
23
|
+
async with AsyncUnprofane("up_live_YOUR_KEY") as client:
|
|
24
|
+
r = await client.check("hello", context="comment")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`check`, `check_batch` (up to 100), `check_many` (any number, chunked), `feedback`, `usage`, `get_list`, `put_list`, `delete_list`. Retries 429 and 5xx with jitter and honours `Retry-After`; other failures raise `UnprofaneError` with the problem details and the request id. Docs: https://unprofane.com/docs/.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "unprofane"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Unprofane: a profanity and toxicity API that is hard to fool. Sync and async client with retries and a batch helper."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [{ name = "Unprofane", email = "hello@unprofane.com" }]
|
|
13
|
+
keywords = ["profanity", "toxicity", "moderation", "filter", "perspective-api"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Topic :: Software Development :: Libraries",
|
|
18
|
+
]
|
|
19
|
+
dependencies = ["httpx>=0.25"]
|
|
20
|
+
|
|
21
|
+
[project.optional-dependencies]
|
|
22
|
+
test = ["pytest>=8", "pytest-asyncio>=0.23"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://unprofane.com"
|
|
26
|
+
Documentation = "https://unprofane.com/docs/"
|
|
27
|
+
Source = "https://github.com/unprofane/unprofane-python"
|
|
28
|
+
Issues = "https://github.com/unprofane/unprofane-python/issues"
|
|
29
|
+
|
|
30
|
+
[tool.hatch.build.targets.wheel]
|
|
31
|
+
packages = ["unprofane"]
|
|
32
|
+
|
|
33
|
+
[tool.hatch.build.targets.sdist]
|
|
34
|
+
include = ["unprofane", "tests", "README.md", "LICENSE", "pyproject.toml"]
|
|
35
|
+
|
|
36
|
+
[tool.pytest.ini_options]
|
|
37
|
+
asyncio_mode = "auto"
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import httpx
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from unprofane import AsyncUnprofane, Unprofane, UnprofaneError
|
|
7
|
+
from unprofane.client import backoff
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def transport(handler):
|
|
11
|
+
return httpx.MockTransport(handler)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_check_sends_the_key_and_body():
|
|
15
|
+
seen = {}
|
|
16
|
+
|
|
17
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
18
|
+
seen["url"] = str(request.url)
|
|
19
|
+
seen["auth"] = request.headers["authorization"]
|
|
20
|
+
seen["body"] = json.loads(request.content)
|
|
21
|
+
return httpx.Response(200, json={"allowed": False, "action": "block"})
|
|
22
|
+
|
|
23
|
+
with Unprofane("up_test_abc", transport=transport(handler)) as client:
|
|
24
|
+
r = client.check("xXsh1tXx", context="username", policy="teen")
|
|
25
|
+
assert r["action"] == "block"
|
|
26
|
+
assert seen["url"] == "https://api.unprofane.com/v1/check"
|
|
27
|
+
assert seen["auth"] == "Bearer up_test_abc"
|
|
28
|
+
assert seen["body"] == {"text": "xXsh1tXx", "context": "username", "policy": "teen"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_refuses_a_foreign_key():
|
|
32
|
+
with pytest.raises(TypeError):
|
|
33
|
+
Unprofane("sk-nope")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_retries_then_raises_a_problem(monkeypatch):
|
|
37
|
+
monkeypatch.setattr("unprofane.client.time.sleep", lambda s: None)
|
|
38
|
+
calls = {"n": 0}
|
|
39
|
+
|
|
40
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
41
|
+
calls["n"] += 1
|
|
42
|
+
if calls["n"] == 1:
|
|
43
|
+
return httpx.Response(429, json={"title": "Too many requests.", "status": 429}, headers={"retry-after": "0"})
|
|
44
|
+
if calls["n"] == 2:
|
|
45
|
+
return httpx.Response(503, text="down")
|
|
46
|
+
return httpx.Response(200, json={"id": "chk_1", "allowed": True})
|
|
47
|
+
|
|
48
|
+
with Unprofane("up_live_k", transport=transport(handler)) as client:
|
|
49
|
+
assert client.check("hi")["allowed"] is True
|
|
50
|
+
assert calls["n"] == 3
|
|
51
|
+
|
|
52
|
+
def bad(request: httpx.Request) -> httpx.Response:
|
|
53
|
+
return httpx.Response(400, json={"type": "https://unprofane.com/docs/errors/invalid-request", "title": "The request body is not valid.", "status": 400, "detail": "text: too long"}, headers={"request-id": "req_9"})
|
|
54
|
+
|
|
55
|
+
with Unprofane("up_live_k", transport=transport(bad)) as client:
|
|
56
|
+
with pytest.raises(UnprofaneError) as e:
|
|
57
|
+
client.check("")
|
|
58
|
+
assert e.value.status == 400
|
|
59
|
+
assert "invalid-request" in e.value.problem["type"]
|
|
60
|
+
assert e.value.request_id == "req_9"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_check_many_chunks_and_keeps_order():
|
|
64
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
65
|
+
items = json.loads(request.content)["items"]
|
|
66
|
+
return httpx.Response(200, json={"results": [{"id": i["id"], "result": {"allowed": i["text"] != "bad"}} for i in items], "counted": len(items)})
|
|
67
|
+
|
|
68
|
+
texts = ["bad" if i == 137 else f"t{i}" for i in range(250)]
|
|
69
|
+
with Unprofane("up_live_k", transport=transport(handler)) as client:
|
|
70
|
+
results = client.check_many(texts, context="comment")
|
|
71
|
+
with pytest.raises(ValueError):
|
|
72
|
+
client.check_batch([{"id": str(i), "text": "x"} for i in range(101)])
|
|
73
|
+
assert len(results) == 250
|
|
74
|
+
assert results[137] == {"id": "137", "result": {"allowed": False}}
|
|
75
|
+
assert results[249]["id"] == "249"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_backoff():
|
|
79
|
+
assert backoff(1, "2") == 2.0
|
|
80
|
+
assert all(backoff(5, None) <= 8.0 for _ in range(20))
|
|
81
|
+
assert backoff(1, "soon") <= 0.5
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
async def test_async_client_and_lists():
|
|
85
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
86
|
+
if request.method == "DELETE":
|
|
87
|
+
return httpx.Response(204)
|
|
88
|
+
if request.url.path.endswith("/lists/allow"):
|
|
89
|
+
return httpx.Response(200, json={"kind": "allow", "terms": ["Scunthorpe"], "version": 2})
|
|
90
|
+
items = json.loads(request.content)["items"]
|
|
91
|
+
return httpx.Response(200, json={"results": [{"id": i["id"], "result": {"allowed": True}} for i in items], "counted": len(items)})
|
|
92
|
+
|
|
93
|
+
async with AsyncUnprofane("up_live_k", transport=httpx.MockTransport(handler)) as client:
|
|
94
|
+
assert (await client.get_list("prj_1", "allow"))["terms"] == ["Scunthorpe"]
|
|
95
|
+
assert await client.delete_list("prj_1", "allow") is None
|
|
96
|
+
results = await client.check_many([f"t{i}" for i in range(150)], concurrency=3)
|
|
97
|
+
assert [r["id"] for r in results] == [str(i) for i in range(150)]
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"""The sync and async clients: one method per endpoint, retries with jitter
|
|
2
|
+
on 429 and 5xx honouring Retry-After, a batch helper that chunks by 100.
|
|
3
|
+
No dependency beyond httpx. Responses are plain dicts in the API's own
|
|
4
|
+
field names (https://unprofane.com/docs/api/), so nothing here can drift
|
|
5
|
+
from the contract without the contract changing."""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import random
|
|
10
|
+
import time
|
|
11
|
+
from typing import Any, Dict, Iterable, List, Optional, Union
|
|
12
|
+
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
__version__ = "0.1.0"
|
|
16
|
+
_DEFAULT_BASE = "https://api.unprofane.com"
|
|
17
|
+
_RETRYABLE = {429, 502, 503, 504}
|
|
18
|
+
_MAX_BATCH = 100
|
|
19
|
+
|
|
20
|
+
Policy = Union[str, Dict[str, str]]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class UnprofaneError(Exception):
|
|
24
|
+
"""A failure the API answered, with its problem details."""
|
|
25
|
+
|
|
26
|
+
def __init__(self, message: str, status: int, problem: Optional[dict], request_id: Optional[str]):
|
|
27
|
+
super().__init__(message)
|
|
28
|
+
self.status = status
|
|
29
|
+
self.problem = problem
|
|
30
|
+
self.request_id = request_id
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def backoff(attempt: int, retry_after: Optional[str]) -> float:
|
|
34
|
+
"""Exponential with full jitter, capped at eight seconds; Retry-After wins."""
|
|
35
|
+
if retry_after is not None:
|
|
36
|
+
try:
|
|
37
|
+
value = float(retry_after)
|
|
38
|
+
if value >= 0:
|
|
39
|
+
return min(value, 30.0)
|
|
40
|
+
except ValueError:
|
|
41
|
+
pass
|
|
42
|
+
return random.random() * min(8.0, 0.25 * (2**attempt))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _check_key(api_key: str) -> None:
|
|
46
|
+
if not api_key or not api_key.startswith(("up_live_", "up_test_", "up_pk_")):
|
|
47
|
+
raise TypeError("pass an API key (up_live_…, up_test_… or up_pk_…)")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _problem(res: httpx.Response) -> Optional[dict]:
|
|
51
|
+
try:
|
|
52
|
+
body = res.json()
|
|
53
|
+
except ValueError:
|
|
54
|
+
return None
|
|
55
|
+
if isinstance(body, dict) and isinstance(body.get("title"), str):
|
|
56
|
+
body.setdefault("status", res.status_code)
|
|
57
|
+
return body
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _error(res: httpx.Response, method: str, path: str) -> UnprofaneError:
|
|
62
|
+
problem = _problem(res)
|
|
63
|
+
message = (problem or {}).get("detail") or (problem or {}).get("title") or f"Unprofane {method} {path} failed ({res.status_code})"
|
|
64
|
+
return UnprofaneError(message, res.status_code, problem, res.headers.get("request-id"))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _chunks(texts: List[str]) -> List[List[dict]]:
|
|
68
|
+
return [
|
|
69
|
+
[{"id": str(i + j), "text": text} for j, text in enumerate(texts[i : i + _MAX_BATCH])]
|
|
70
|
+
for i in range(0, len(texts), _MAX_BATCH)
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _drop_none(body: Dict[str, Any]) -> Dict[str, Any]:
|
|
75
|
+
return {k: v for k, v in body.items() if v is not None}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class _Base:
|
|
79
|
+
def __init__(self, api_key: str, base_url: str = _DEFAULT_BASE, retries: int = 3, timeout: float = 10.0):
|
|
80
|
+
_check_key(api_key)
|
|
81
|
+
self._headers = {"authorization": f"Bearer {api_key}", "user-agent": f"unprofane-python/{__version__}"}
|
|
82
|
+
self._base = base_url.rstrip("/")
|
|
83
|
+
self._retries = retries
|
|
84
|
+
self._timeout = timeout
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class Unprofane(_Base):
|
|
88
|
+
"""The synchronous client. Use it as a context manager or call close()."""
|
|
89
|
+
|
|
90
|
+
def __init__(self, api_key: str, base_url: str = _DEFAULT_BASE, retries: int = 3, timeout: float = 10.0, transport: Optional[httpx.BaseTransport] = None):
|
|
91
|
+
super().__init__(api_key, base_url, retries, timeout)
|
|
92
|
+
self._http = httpx.Client(base_url=self._base, headers=self._headers, timeout=timeout, transport=transport)
|
|
93
|
+
|
|
94
|
+
def __enter__(self) -> "Unprofane":
|
|
95
|
+
return self
|
|
96
|
+
|
|
97
|
+
def __exit__(self, *_: object) -> None:
|
|
98
|
+
self.close()
|
|
99
|
+
|
|
100
|
+
def close(self) -> None:
|
|
101
|
+
self._http.close()
|
|
102
|
+
|
|
103
|
+
def _call(self, method: str, path: str, body: Optional[dict] = None) -> Any:
|
|
104
|
+
attempt = 0
|
|
105
|
+
while True:
|
|
106
|
+
try:
|
|
107
|
+
res = self._http.request(method, path, json=body)
|
|
108
|
+
except httpx.TransportError:
|
|
109
|
+
if attempt >= self._retries:
|
|
110
|
+
raise
|
|
111
|
+
attempt += 1
|
|
112
|
+
time.sleep(backoff(attempt, None))
|
|
113
|
+
continue
|
|
114
|
+
if res.is_success:
|
|
115
|
+
return None if res.status_code == 204 else res.json()
|
|
116
|
+
if res.status_code in _RETRYABLE and attempt < self._retries:
|
|
117
|
+
attempt += 1
|
|
118
|
+
time.sleep(backoff(attempt, res.headers.get("retry-after")))
|
|
119
|
+
continue
|
|
120
|
+
raise _error(res, method, path)
|
|
121
|
+
|
|
122
|
+
def check(self, text: str, *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, language: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
|
|
123
|
+
"""One text: should it be allowed, with the evidence."""
|
|
124
|
+
return self._call("POST", "/v1/check", _drop_none({"text": text, "context": context, "policy": policy, "mode": mode, "language": language, "mask": mask, "engine": engine, "lists": lists}))
|
|
125
|
+
|
|
126
|
+
def check_batch(self, items: List[dict], *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
|
|
127
|
+
"""Up to 100 items of {id, text}, answered in order."""
|
|
128
|
+
if len(items) > _MAX_BATCH:
|
|
129
|
+
raise ValueError(f"check_batch takes at most {_MAX_BATCH} items; use check_many")
|
|
130
|
+
return self._call("POST", "/v1/check/batch", _drop_none({"items": items, "context": context, "policy": policy, "mode": mode, "mask": mask, "engine": engine, "lists": lists}))
|
|
131
|
+
|
|
132
|
+
def check_many(self, texts: Iterable[str], **options: Any) -> List[dict]:
|
|
133
|
+
"""Any number of texts, chunked by 100, results in input order."""
|
|
134
|
+
out: List[dict] = []
|
|
135
|
+
for chunk in _chunks(list(texts)):
|
|
136
|
+
out.extend(self.check_batch(chunk, **options)["results"])
|
|
137
|
+
return out
|
|
138
|
+
|
|
139
|
+
def feedback(self, check_id: str, verdict: str, *, expected_action: Optional[str] = None, note: Optional[str] = None, text: Optional[str] = None) -> dict:
|
|
140
|
+
"""Report a wrong result. `text` is stored only if you include it."""
|
|
141
|
+
return self._call("POST", "/v1/feedback", _drop_none({"check_id": check_id, "verdict": verdict, "expected_action": expected_action, "note": note, "text": text}))
|
|
142
|
+
|
|
143
|
+
def usage(self) -> dict:
|
|
144
|
+
return self._call("GET", "/v1/usage")
|
|
145
|
+
|
|
146
|
+
def get_list(self, project_id: str, kind: str) -> dict:
|
|
147
|
+
return self._call("GET", f"/v1/projects/{project_id}/lists/{kind}")
|
|
148
|
+
|
|
149
|
+
def put_list(self, project_id: str, kind: str, terms: List[str]) -> dict:
|
|
150
|
+
return self._call("PUT", f"/v1/projects/{project_id}/lists/{kind}", {"terms": terms})
|
|
151
|
+
|
|
152
|
+
def delete_list(self, project_id: str, kind: str) -> None:
|
|
153
|
+
self._call("DELETE", f"/v1/projects/{project_id}/lists/{kind}")
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class AsyncUnprofane(_Base):
|
|
157
|
+
"""The asynchronous client, for asyncio. Use it as an async context manager."""
|
|
158
|
+
|
|
159
|
+
def __init__(self, api_key: str, base_url: str = _DEFAULT_BASE, retries: int = 3, timeout: float = 10.0, transport: Optional[httpx.AsyncBaseTransport] = None):
|
|
160
|
+
super().__init__(api_key, base_url, retries, timeout)
|
|
161
|
+
self._http = httpx.AsyncClient(base_url=self._base, headers=self._headers, timeout=timeout, transport=transport)
|
|
162
|
+
|
|
163
|
+
async def __aenter__(self) -> "AsyncUnprofane":
|
|
164
|
+
return self
|
|
165
|
+
|
|
166
|
+
async def __aexit__(self, *_: object) -> None:
|
|
167
|
+
await self.aclose()
|
|
168
|
+
|
|
169
|
+
async def aclose(self) -> None:
|
|
170
|
+
await self._http.aclose()
|
|
171
|
+
|
|
172
|
+
async def _call(self, method: str, path: str, body: Optional[dict] = None) -> Any:
|
|
173
|
+
import asyncio
|
|
174
|
+
|
|
175
|
+
attempt = 0
|
|
176
|
+
while True:
|
|
177
|
+
try:
|
|
178
|
+
res = await self._http.request(method, path, json=body)
|
|
179
|
+
except httpx.TransportError:
|
|
180
|
+
if attempt >= self._retries:
|
|
181
|
+
raise
|
|
182
|
+
attempt += 1
|
|
183
|
+
await asyncio.sleep(backoff(attempt, None))
|
|
184
|
+
continue
|
|
185
|
+
if res.is_success:
|
|
186
|
+
return None if res.status_code == 204 else res.json()
|
|
187
|
+
if res.status_code in _RETRYABLE and attempt < self._retries:
|
|
188
|
+
attempt += 1
|
|
189
|
+
await asyncio.sleep(backoff(attempt, res.headers.get("retry-after")))
|
|
190
|
+
continue
|
|
191
|
+
raise _error(res, method, path)
|
|
192
|
+
|
|
193
|
+
async def check(self, text: str, *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, language: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
|
|
194
|
+
return await self._call("POST", "/v1/check", _drop_none({"text": text, "context": context, "policy": policy, "mode": mode, "language": language, "mask": mask, "engine": engine, "lists": lists}))
|
|
195
|
+
|
|
196
|
+
async def check_batch(self, items: List[dict], *, context: Optional[str] = None, policy: Optional[Policy] = None, mode: Optional[str] = None, mask: Optional[str] = None, engine: Optional[str] = None, lists: Optional[dict] = None) -> dict:
|
|
197
|
+
if len(items) > _MAX_BATCH:
|
|
198
|
+
raise ValueError(f"check_batch takes at most {_MAX_BATCH} items; use check_many")
|
|
199
|
+
return await self._call("POST", "/v1/check/batch", _drop_none({"items": items, "context": context, "policy": policy, "mode": mode, "mask": mask, "engine": engine, "lists": lists}))
|
|
200
|
+
|
|
201
|
+
async def check_many(self, texts: Iterable[str], *, concurrency: int = 2, **options: Any) -> List[dict]:
|
|
202
|
+
import asyncio
|
|
203
|
+
|
|
204
|
+
chunks = _chunks(list(texts))
|
|
205
|
+
out: List[Optional[dict]] = [None] * len(chunks)
|
|
206
|
+
sem = asyncio.Semaphore(max(1, concurrency))
|
|
207
|
+
|
|
208
|
+
async def one(i: int) -> None:
|
|
209
|
+
async with sem:
|
|
210
|
+
out[i] = await self.check_batch(chunks[i], **options)
|
|
211
|
+
|
|
212
|
+
await asyncio.gather(*(one(i) for i in range(len(chunks))))
|
|
213
|
+
return [r for batch in out for r in (batch or {}).get("results", [])]
|
|
214
|
+
|
|
215
|
+
async def feedback(self, check_id: str, verdict: str, *, expected_action: Optional[str] = None, note: Optional[str] = None, text: Optional[str] = None) -> dict:
|
|
216
|
+
return await self._call("POST", "/v1/feedback", _drop_none({"check_id": check_id, "verdict": verdict, "expected_action": expected_action, "note": note, "text": text}))
|
|
217
|
+
|
|
218
|
+
async def usage(self) -> dict:
|
|
219
|
+
return await self._call("GET", "/v1/usage")
|
|
220
|
+
|
|
221
|
+
async def get_list(self, project_id: str, kind: str) -> dict:
|
|
222
|
+
return await self._call("GET", f"/v1/projects/{project_id}/lists/{kind}")
|
|
223
|
+
|
|
224
|
+
async def put_list(self, project_id: str, kind: str, terms: List[str]) -> dict:
|
|
225
|
+
return await self._call("PUT", f"/v1/projects/{project_id}/lists/{kind}", {"terms": terms})
|
|
226
|
+
|
|
227
|
+
async def delete_list(self, project_id: str, kind: str) -> None:
|
|
228
|
+
await self._call("DELETE", f"/v1/projects/{project_id}/lists/{kind}")
|