waapi 0.1.0__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,38 +1,17 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: waapi
3
- Version: 0.1.0
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
7
7
  Project-URL: Source, https://github.com/WaAPIapp/waapi-python-sdk
8
8
  Project-URL: Issues, https://github.com/WaAPIapp/waapi-python-sdk/issues
9
9
  Author-email: WaAPI <info@waapi.app>
10
- License: The MIT License (MIT)
11
-
12
- Copyright (c) WaAPI <info@waapi.app>
13
-
14
- Permission is hereby granted, free of charge, to any person obtaining a copy
15
- of this software and associated documentation files (the "Software"), to deal
16
- in the Software without restriction, including without limitation the rights
17
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
- copies of the Software, and to permit persons to whom the Software is
19
- furnished to do so, subject to the following conditions:
20
-
21
- The above copyright notice and this permission notice shall be included in all
22
- copies or substantial portions of the Software.
23
-
24
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
- SOFTWARE.
10
+ License-Expression: MIT
31
11
  License-File: LICENSE
32
12
  Keywords: api,automation,chatbot,messaging,sdk,waapi
33
13
  Classifier: Development Status :: 4 - Beta
34
14
  Classifier: Intended Audience :: Developers
35
- Classifier: License :: OSI Approved :: MIT License
36
15
  Classifier: Programming Language :: Python :: 3
37
16
  Classifier: Programming Language :: Python :: 3.9
38
17
  Classifier: Programming Language :: Python :: 3.10
@@ -105,10 +84,18 @@ async with AsyncWaAPI(token="YOUR_API_TOKEN", instance_id=123) as client:
105
84
 
106
85
  ## Errors
107
86
 
108
- A successful HTTP exchange is not proof the message was sent. The API answers
109
- `200` with `{"status": "error"}` when, for example, the instance is not
110
- connected — so the SDK raises on that too, rather than handing back a body that
111
- 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.
112
99
 
113
100
  ```python
114
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
@@ -1,20 +1,22 @@
1
1
  [build-system]
2
- requires = ["hatchling"]
2
+ requires = ["hatchling>=1.27"]
3
3
  build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "waapi"
7
- version = "0.1.0"
7
+ version = "0.1.2"
8
8
  description = "Official Python SDK for the WaAPI REST API"
9
9
  readme = "README.md"
10
- license = { file = "LICENSE" }
10
+ # PEP 639: an SPDX expression, not the file. The table form made PyPI
11
+ # render the whole licence text where the licence name belongs.
12
+ license = "MIT"
13
+ license-files = ["LICENSE"]
11
14
  requires-python = ">=3.9"
12
15
  authors = [{ name = "WaAPI", email = "info@waapi.app" }]
13
16
  keywords = ["waapi", "messaging", "api", "sdk", "automation", "chatbot"]
14
17
  classifiers = [
15
18
  "Development Status :: 4 - Beta",
16
19
  "Intended Audience :: Developers",
17
- "License :: OSI Approved :: MIT License",
18
20
  "Programming Language :: Python :: 3",
19
21
  "Programming Language :: Python :: 3.9",
20
22
  "Programming Language :: Python :: 3.10",
@@ -6,7 +6,7 @@
6
6
  client.send_message(chat_id="4915112345678@c.us", message="Hello")
7
7
  """
8
8
 
9
- __version__ = "0.1.0"
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