waapi 0.1.1__tar.gz → 0.1.2__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.
- {waapi-0.1.1 → waapi-0.1.2}/PKG-INFO +13 -5
- {waapi-0.1.1 → waapi-0.1.2}/README.md +12 -4
- {waapi-0.1.1 → waapi-0.1.2}/pyproject.toml +1 -1
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/__init__.py +1 -1
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/_http.py +22 -9
- {waapi-0.1.1 → waapi-0.1.2}/tests/test_client.py +23 -0
- {waapi-0.1.1 → waapi-0.1.2}/.github/workflows/publish.yml +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/.github/workflows/tests.yml +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/.gitignore +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/CONTRIBUTING.md +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/LICENSE +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/scripts/sync_actions.py +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/_actions.py +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/_generated.py +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/client.py +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/exceptions.py +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/src/waapi/py.typed +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/tests/conftest.py +0 -0
- {waapi-0.1.1 → waapi-0.1.2}/tests/test_generated_actions.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: waapi
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Official Python SDK for the WaAPI REST API
|
|
5
5
|
Project-URL: Homepage, https://waapi.app
|
|
6
6
|
Project-URL: Documentation, https://waapi.app/docs
|
|
@@ -84,10 +84,18 @@ async with AsyncWaAPI(token="YOUR_API_TOKEN", instance_id=123) as client:
|
|
|
84
84
|
|
|
85
85
|
## Errors
|
|
86
86
|
|
|
87
|
-
A successful HTTP exchange is not proof the message was sent. The API
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
A successful HTTP exchange is not proof the message was sent. The API carries
|
|
88
|
+
two envelopes:
|
|
89
|
+
|
|
90
|
+
| Field | Answers |
|
|
91
|
+
|---|---|
|
|
92
|
+
| `status` | did the request reach the instance |
|
|
93
|
+
| `data.status` | did the instance carry the action out |
|
|
94
|
+
|
|
95
|
+
**The inner one is authoritative.** A malformed chat ID comes back as
|
|
96
|
+
`{"status": "success", "data": {"status": "error", …}}` — nothing was sent. The
|
|
97
|
+
SDK raises `FailedActionError` on either envelope, so neither case reaches you
|
|
98
|
+
looking like success.
|
|
91
99
|
|
|
92
100
|
```python
|
|
93
101
|
from waapi import WaAPI, FailedActionError, AuthenticationError, RateLimitError
|
|
@@ -51,10 +51,18 @@ async with AsyncWaAPI(token="YOUR_API_TOKEN", instance_id=123) as client:
|
|
|
51
51
|
|
|
52
52
|
## Errors
|
|
53
53
|
|
|
54
|
-
A successful HTTP exchange is not proof the message was sent. The API
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
A successful HTTP exchange is not proof the message was sent. The API carries
|
|
55
|
+
two envelopes:
|
|
56
|
+
|
|
57
|
+
| Field | Answers |
|
|
58
|
+
|---|---|
|
|
59
|
+
| `status` | did the request reach the instance |
|
|
60
|
+
| `data.status` | did the instance carry the action out |
|
|
61
|
+
|
|
62
|
+
**The inner one is authoritative.** A malformed chat ID comes back as
|
|
63
|
+
`{"status": "success", "data": {"status": "error", …}}` — nothing was sent. The
|
|
64
|
+
SDK raises `FailedActionError` on either envelope, so neither case reaches you
|
|
65
|
+
looking like success.
|
|
58
66
|
|
|
59
67
|
```python
|
|
60
68
|
from waapi import WaAPI, FailedActionError, AuthenticationError, RateLimitError
|
|
@@ -73,20 +73,33 @@ def interpret(response: httpx.Response, *, check_body_status: bool = True) -> An
|
|
|
73
73
|
if not 200 <= status < 300:
|
|
74
74
|
raise WaAPIError(f"WaAPI returned HTTP {status}: {_brief(body)}")
|
|
75
75
|
|
|
76
|
-
#
|
|
77
|
-
#
|
|
76
|
+
# Two envelopes, and the inner one is the authoritative answer:
|
|
77
|
+
#
|
|
78
|
+
# body.status did the request reach the instance
|
|
79
|
+
# body.data.status did the instance carry the action out
|
|
80
|
+
#
|
|
81
|
+
# A malformed chatId comes back as {"status": "success", "data":
|
|
82
|
+
# {"status": "error", "message": "incorrect chatId format."}} -- nothing
|
|
83
|
+
# was sent. Checking only the outer envelope reports that as delivered,
|
|
84
|
+
# which is the exact failure this class exists to prevent, one level down.
|
|
78
85
|
if check_body_status and isinstance(body, Mapping):
|
|
79
|
-
envelope
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
+
for envelope in (_inner_status(body), body.get("status")):
|
|
87
|
+
if envelope is not None and envelope != "success":
|
|
88
|
+
raise FailedActionError(
|
|
89
|
+
"WaAPI accepted the request but did not carry the action out: "
|
|
90
|
+
+ _brief(body),
|
|
91
|
+
body,
|
|
92
|
+
)
|
|
86
93
|
|
|
87
94
|
return body
|
|
88
95
|
|
|
89
96
|
|
|
97
|
+
def _inner_status(body: Mapping[str, Any]) -> Any:
|
|
98
|
+
"""``body.data.status`` when the response wraps a client action."""
|
|
99
|
+
data = body.get("data")
|
|
100
|
+
return data.get("status") if isinstance(data, Mapping) else None
|
|
101
|
+
|
|
102
|
+
|
|
90
103
|
def _brief(body: Any, limit: int = 400) -> str:
|
|
91
104
|
text = body if isinstance(body, str) else _json.dumps(body, ensure_ascii=False)
|
|
92
105
|
return text if len(text) <= limit else text[:limit] + "..."
|
|
@@ -212,3 +212,26 @@ def test_the_generated_surface_is_not_empty():
|
|
|
212
212
|
|
|
213
213
|
methods = {n for n in vars(GeneratedActions) if not n.startswith("_")}
|
|
214
214
|
assert len(methods) > 100, f"only {len(methods)} generated methods"
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_the_inner_status_is_the_authoritative_one():
|
|
218
|
+
"""A request can reach the instance and still not be carried out.
|
|
219
|
+
|
|
220
|
+
The outer envelope reports delivery to the instance; the inner one reports
|
|
221
|
+
whether the instance did the thing. A malformed chatId produces
|
|
222
|
+
success-outside, error-inside — and was reported as sent until the check
|
|
223
|
+
looked one level down.
|
|
224
|
+
"""
|
|
225
|
+
client = client_returning(200, {
|
|
226
|
+
"status": "success",
|
|
227
|
+
"data": {"status": "error", "message": "incorrect chatId format."},
|
|
228
|
+
})
|
|
229
|
+
with pytest.raises(FailedActionError) as excinfo:
|
|
230
|
+
client.send_message(chat_id="bogus", message="must not count as sent")
|
|
231
|
+
assert "did not carry the action out" in str(excinfo.value)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def test_a_data_array_does_not_trip_the_inner_check():
|
|
235
|
+
"""Plenty of actions return a list in `data`, which has no status at all."""
|
|
236
|
+
client = client_returning(200, {"status": "success", "data": [{"id": 1}, {"id": 2}]})
|
|
237
|
+
assert client.get_chats()["data"] == [{"id": 1}, {"id": 2}]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|