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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: waapi
3
- Version: 0.1.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 answers
88
- `200` with `{"status": "error"}` when, for example, the instance is not
89
- connected — so the SDK raises on that too, rather than handing back a body that
90
- looks like success.
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 answers
55
- `200` with `{"status": "error"}` when, for example, the instance is not
56
- connected — so the SDK raises on that too, rather than handing back a body that
57
- looks like success.
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
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "waapi"
7
- version = "0.1.1"
7
+ version = "0.1.2"
8
8
  description = "Official Python SDK for the WaAPI REST API"
9
9
  readme = "README.md"
10
10
  # PEP 639: an SPDX expression, not the file. The table form made PyPI
@@ -6,7 +6,7 @@
6
6
  client.send_message(chat_id="4915112345678@c.us", message="Hello")
7
7
  """
8
8
 
9
- __version__ = "0.1.1"
9
+ __version__ = "0.1.2"
10
10
 
11
11
  from .client import AsyncWaAPI, WaAPI
12
12
  from .exceptions import (
@@ -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
- # A 2xx is not proof the action happened. The API answers 200 with
77
- # {"status": "error"} when, for example, the instance is not connected.
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 = body.get("status")
80
- if envelope is not None and envelope != "success":
81
- raise FailedActionError(
82
- "WaAPI accepted the request but did not carry the action out: "
83
- + _brief(body),
84
- body,
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