reachflow 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.
- reachflow-0.1.0/.gitignore +17 -0
- reachflow-0.1.0/LICENSE +21 -0
- reachflow-0.1.0/PKG-INFO +134 -0
- reachflow-0.1.0/README.md +104 -0
- reachflow-0.1.0/pyproject.toml +54 -0
- reachflow-0.1.0/src/reachflow/__init__.py +22 -0
- reachflow-0.1.0/src/reachflow/client.py +113 -0
- reachflow-0.1.0/src/reachflow/errors.py +114 -0
- reachflow-0.1.0/src/reachflow/http.py +238 -0
- reachflow-0.1.0/src/reachflow/py.typed +0 -0
- reachflow-0.1.0/src/reachflow/resources/__init__.py +1 -0
- reachflow-0.1.0/src/reachflow/resources/messages.py +220 -0
- reachflow-0.1.0/src/reachflow/resources/otp.py +110 -0
- reachflow-0.1.0/src/reachflow/resources/providers.py +47 -0
- reachflow-0.1.0/src/reachflow/types.py +111 -0
- reachflow-0.1.0/tests/test_client.py +170 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
node_modules/
|
|
2
|
+
dist/
|
|
3
|
+
*.tsbuildinfo
|
|
4
|
+
.coverage
|
|
5
|
+
htmlcov/
|
|
6
|
+
.pytest_cache/
|
|
7
|
+
.mypy_cache/
|
|
8
|
+
.ruff_cache/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
.venv/
|
|
11
|
+
venv/
|
|
12
|
+
reachflow-python/.venv/
|
|
13
|
+
.env
|
|
14
|
+
.env.*
|
|
15
|
+
!.env.example
|
|
16
|
+
scripts/.env.sandbox
|
|
17
|
+
reachflow-node/.npmrc.publish
|
reachflow-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ReachFlow
|
|
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.
|
reachflow-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: reachflow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Client officiel Python pour l'API publique ReachFlow
|
|
5
|
+
Project-URL: Homepage, https://docs.reachflow.me/developpeurs
|
|
6
|
+
Project-URL: Documentation, https://docs.reachflow.me/developpeurs
|
|
7
|
+
Project-URL: Repository, https://github.com/reachflow/reachflow-python
|
|
8
|
+
Project-URL: Issues, https://github.com/reachflow/reachflow-python/issues
|
|
9
|
+
Author-email: ReachFlow <contact@reachflow.me>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: api,otp,reachflow,sdk,whatsapp
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Requires-Dist: httpx>=0.27.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: mypy>=1.13.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.24.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: respx>=0.21.0; extra == 'dev'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# reachflow
|
|
32
|
+
|
|
33
|
+
Client officiel **Python** pour l’[API publique ReachFlow](https://docs.reachflow.me/developpeurs) (REST v1).
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install reachflow
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Prérequis :** Python ≥ 3.10.
|
|
42
|
+
|
|
43
|
+
## Configuration
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from reachflow import ReachFlow
|
|
47
|
+
|
|
48
|
+
client = ReachFlow(
|
|
49
|
+
api_key="rfl_live_…", # ou rfl_test_…
|
|
50
|
+
base_url="https://sandbox-api.reachflow.me", # optionnel
|
|
51
|
+
timeout_ms=30_000, # optionnel
|
|
52
|
+
max_retries=2, # optionnel — 429 / 5xx
|
|
53
|
+
)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Utilisez le client comme context manager pour fermer la connexion HTTP :
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
with ReachFlow(api_key="rfl_live_…") as client:
|
|
60
|
+
...
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Exemples
|
|
64
|
+
|
|
65
|
+
### Envoyer un message
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
with ReachFlow(api_key="rfl_live_…") as client:
|
|
69
|
+
result = client.messages.send(
|
|
70
|
+
provider_id="uuid-du-provider",
|
|
71
|
+
to="22996123456",
|
|
72
|
+
message="Votre commande est confirmée.",
|
|
73
|
+
)
|
|
74
|
+
status = client.messages.wait_for_terminal(result["messageId"])
|
|
75
|
+
print(status["status"])
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### OTP
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
with ReachFlow(api_key="rfl_live_…") as client:
|
|
82
|
+
sent = client.otp.send(
|
|
83
|
+
provider_id="uuid-du-provider",
|
|
84
|
+
phone_number="22996123456",
|
|
85
|
+
brand_name="Mon App",
|
|
86
|
+
)
|
|
87
|
+
# Le code arrive sur WhatsApp — jamais dans la réponse JSON.
|
|
88
|
+
verified = client.otp.verify(otp_id=sent["otpId"], code="482910")
|
|
89
|
+
print(verified["valid"])
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Client asynchrone
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from reachflow import AsyncReachFlow
|
|
96
|
+
|
|
97
|
+
async with AsyncReachFlow(api_key="rfl_live_…") as client:
|
|
98
|
+
providers = await client.providers.list()
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Gestion des erreurs
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from reachflow import ReachFlow, ReachFlowError
|
|
105
|
+
|
|
106
|
+
try:
|
|
107
|
+
client.messages.send(...)
|
|
108
|
+
except ReachFlowError as err:
|
|
109
|
+
print(err.status_code, err.code, err.message)
|
|
110
|
+
if err.retryable:
|
|
111
|
+
...
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Idempotence
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
client.messages.send(
|
|
118
|
+
provider_id=provider_id,
|
|
119
|
+
to=to,
|
|
120
|
+
message=message,
|
|
121
|
+
idempotency_key="order-12345",
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Développement
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
pip install -e ".[dev]"
|
|
129
|
+
pytest
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Licence
|
|
133
|
+
|
|
134
|
+
MIT
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# reachflow
|
|
2
|
+
|
|
3
|
+
Client officiel **Python** pour l’[API publique ReachFlow](https://docs.reachflow.me/developpeurs) (REST v1).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install reachflow
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Prérequis :** Python ≥ 3.10.
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from reachflow import ReachFlow
|
|
17
|
+
|
|
18
|
+
client = ReachFlow(
|
|
19
|
+
api_key="rfl_live_…", # ou rfl_test_…
|
|
20
|
+
base_url="https://sandbox-api.reachflow.me", # optionnel
|
|
21
|
+
timeout_ms=30_000, # optionnel
|
|
22
|
+
max_retries=2, # optionnel — 429 / 5xx
|
|
23
|
+
)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Utilisez le client comme context manager pour fermer la connexion HTTP :
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
with ReachFlow(api_key="rfl_live_…") as client:
|
|
30
|
+
...
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Exemples
|
|
34
|
+
|
|
35
|
+
### Envoyer un message
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
with ReachFlow(api_key="rfl_live_…") as client:
|
|
39
|
+
result = client.messages.send(
|
|
40
|
+
provider_id="uuid-du-provider",
|
|
41
|
+
to="22996123456",
|
|
42
|
+
message="Votre commande est confirmée.",
|
|
43
|
+
)
|
|
44
|
+
status = client.messages.wait_for_terminal(result["messageId"])
|
|
45
|
+
print(status["status"])
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### OTP
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
with ReachFlow(api_key="rfl_live_…") as client:
|
|
52
|
+
sent = client.otp.send(
|
|
53
|
+
provider_id="uuid-du-provider",
|
|
54
|
+
phone_number="22996123456",
|
|
55
|
+
brand_name="Mon App",
|
|
56
|
+
)
|
|
57
|
+
# Le code arrive sur WhatsApp — jamais dans la réponse JSON.
|
|
58
|
+
verified = client.otp.verify(otp_id=sent["otpId"], code="482910")
|
|
59
|
+
print(verified["valid"])
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Client asynchrone
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from reachflow import AsyncReachFlow
|
|
66
|
+
|
|
67
|
+
async with AsyncReachFlow(api_key="rfl_live_…") as client:
|
|
68
|
+
providers = await client.providers.list()
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Gestion des erreurs
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from reachflow import ReachFlow, ReachFlowError
|
|
75
|
+
|
|
76
|
+
try:
|
|
77
|
+
client.messages.send(...)
|
|
78
|
+
except ReachFlowError as err:
|
|
79
|
+
print(err.status_code, err.code, err.message)
|
|
80
|
+
if err.retryable:
|
|
81
|
+
...
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Idempotence
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
client.messages.send(
|
|
88
|
+
provider_id=provider_id,
|
|
89
|
+
to=to,
|
|
90
|
+
message=message,
|
|
91
|
+
idempotency_key="order-12345",
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Développement
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pip install -e ".[dev]"
|
|
99
|
+
pytest
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Licence
|
|
103
|
+
|
|
104
|
+
MIT
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24.0"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "reachflow"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Client officiel Python pour l'API publique ReachFlow"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
authors = [{ name = "ReachFlow", email = "contact@reachflow.me" }]
|
|
13
|
+
keywords = ["reachflow", "whatsapp", "api", "sdk", "otp"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
dependencies = [
|
|
26
|
+
"httpx>=0.27.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = [
|
|
31
|
+
"pytest>=8.0.0",
|
|
32
|
+
"pytest-asyncio>=0.24.0",
|
|
33
|
+
"respx>=0.21.0",
|
|
34
|
+
"mypy>=1.13.0",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://docs.reachflow.me/developpeurs"
|
|
39
|
+
Documentation = "https://docs.reachflow.me/developpeurs"
|
|
40
|
+
Repository = "https://github.com/reachflow/reachflow-python"
|
|
41
|
+
Issues = "https://github.com/reachflow/reachflow-python/issues"
|
|
42
|
+
|
|
43
|
+
[tool.hatch.build.targets.wheel]
|
|
44
|
+
packages = ["src/reachflow"]
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
testpaths = ["tests"]
|
|
48
|
+
asyncio_mode = "auto"
|
|
49
|
+
|
|
50
|
+
[tool.mypy]
|
|
51
|
+
python_version = "3.10"
|
|
52
|
+
strict = true
|
|
53
|
+
packages = ["reachflow"]
|
|
54
|
+
mypy_path = ["src"]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Client officiel Python pour l'API publique ReachFlow."""
|
|
2
|
+
|
|
3
|
+
from reachflow.client import ReachFlow, AsyncReachFlow
|
|
4
|
+
from reachflow.errors import ReachFlowError
|
|
5
|
+
from reachflow.types import (
|
|
6
|
+
DEFAULT_BASE_URL,
|
|
7
|
+
TERMINAL_MESSAGE_STATUSES,
|
|
8
|
+
MessageStatus,
|
|
9
|
+
MediaType,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"ReachFlow",
|
|
14
|
+
"AsyncReachFlow",
|
|
15
|
+
"ReachFlowError",
|
|
16
|
+
"DEFAULT_BASE_URL",
|
|
17
|
+
"TERMINAL_MESSAGE_STATUSES",
|
|
18
|
+
"MessageStatus",
|
|
19
|
+
"MediaType",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from types import TracebackType
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from reachflow.http import (
|
|
8
|
+
AsyncHttpClient,
|
|
9
|
+
ClientConfig,
|
|
10
|
+
HttpClient,
|
|
11
|
+
resolve_config,
|
|
12
|
+
)
|
|
13
|
+
from reachflow.resources.messages import AsyncMessagesResource, MessagesResource
|
|
14
|
+
from reachflow.resources.otp import AsyncOtpResource, OtpResource
|
|
15
|
+
from reachflow.resources.providers import AsyncProvidersResource, ProvidersResource
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ReachFlow:
|
|
19
|
+
"""Client synchrone pour l'API publique ReachFlow (REST v1)."""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
*,
|
|
24
|
+
api_key: str,
|
|
25
|
+
base_url: str | None = None,
|
|
26
|
+
timeout_ms: int = 30_000,
|
|
27
|
+
max_retries: int = 2,
|
|
28
|
+
http_client: httpx.Client | None = None,
|
|
29
|
+
) -> None:
|
|
30
|
+
if not api_key or not api_key.strip():
|
|
31
|
+
raise ValueError("ReachFlow: api_key is required")
|
|
32
|
+
|
|
33
|
+
self._config: ClientConfig = resolve_config(
|
|
34
|
+
api_key=api_key,
|
|
35
|
+
base_url=base_url,
|
|
36
|
+
timeout_ms=timeout_ms,
|
|
37
|
+
max_retries=max_retries,
|
|
38
|
+
)
|
|
39
|
+
self._owns_client = http_client is None
|
|
40
|
+
self._httpx = http_client or httpx.Client()
|
|
41
|
+
self._http = HttpClient(self._config, self._httpx)
|
|
42
|
+
|
|
43
|
+
self.messages = MessagesResource(self._http)
|
|
44
|
+
self.providers = ProvidersResource(self._http)
|
|
45
|
+
self.otp = OtpResource(self._http)
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def base_url(self) -> str:
|
|
49
|
+
return self._config.base_url
|
|
50
|
+
|
|
51
|
+
def close(self) -> None:
|
|
52
|
+
if self._owns_client:
|
|
53
|
+
self._httpx.close()
|
|
54
|
+
|
|
55
|
+
def __enter__(self) -> ReachFlow:
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
def __exit__(
|
|
59
|
+
self,
|
|
60
|
+
exc_type: type[BaseException] | None,
|
|
61
|
+
exc: BaseException | None,
|
|
62
|
+
tb: TracebackType | None,
|
|
63
|
+
) -> None:
|
|
64
|
+
self.close()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class AsyncReachFlow:
|
|
68
|
+
"""Client asynchrone pour l'API publique ReachFlow (REST v1)."""
|
|
69
|
+
|
|
70
|
+
def __init__(
|
|
71
|
+
self,
|
|
72
|
+
*,
|
|
73
|
+
api_key: str,
|
|
74
|
+
base_url: str | None = None,
|
|
75
|
+
timeout_ms: int = 30_000,
|
|
76
|
+
max_retries: int = 2,
|
|
77
|
+
http_client: httpx.AsyncClient | None = None,
|
|
78
|
+
) -> None:
|
|
79
|
+
if not api_key or not api_key.strip():
|
|
80
|
+
raise ValueError("ReachFlow: api_key is required")
|
|
81
|
+
|
|
82
|
+
self._config: ClientConfig = resolve_config(
|
|
83
|
+
api_key=api_key,
|
|
84
|
+
base_url=base_url,
|
|
85
|
+
timeout_ms=timeout_ms,
|
|
86
|
+
max_retries=max_retries,
|
|
87
|
+
)
|
|
88
|
+
self._owns_client = http_client is None
|
|
89
|
+
self._httpx = http_client or httpx.AsyncClient()
|
|
90
|
+
self._http = AsyncHttpClient(self._config, self._httpx)
|
|
91
|
+
|
|
92
|
+
self.messages = AsyncMessagesResource(self._http)
|
|
93
|
+
self.providers = AsyncProvidersResource(self._http)
|
|
94
|
+
self.otp = AsyncOtpResource(self._http)
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def base_url(self) -> str:
|
|
98
|
+
return self._config.base_url
|
|
99
|
+
|
|
100
|
+
async def close(self) -> None:
|
|
101
|
+
if self._owns_client:
|
|
102
|
+
await self._httpx.aclose()
|
|
103
|
+
|
|
104
|
+
async def __aenter__(self) -> AsyncReachFlow:
|
|
105
|
+
return self
|
|
106
|
+
|
|
107
|
+
async def __aexit__(
|
|
108
|
+
self,
|
|
109
|
+
exc_type: type[BaseException] | None,
|
|
110
|
+
exc: BaseException | None,
|
|
111
|
+
tb: TracebackType | None,
|
|
112
|
+
) -> None:
|
|
113
|
+
await self.close()
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from email.utils import parsedate_to_datetime
|
|
4
|
+
from typing import Any, Literal
|
|
5
|
+
|
|
6
|
+
ReachFlowErrorCode = Literal[
|
|
7
|
+
"unauthorized",
|
|
8
|
+
"plan_required",
|
|
9
|
+
"insufficient_scope",
|
|
10
|
+
"rate_limit_exceeded",
|
|
11
|
+
"too_many_auth_failures",
|
|
12
|
+
"validation_error",
|
|
13
|
+
"not_found",
|
|
14
|
+
"api_error",
|
|
15
|
+
"network_error",
|
|
16
|
+
"timeout",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ReachFlowError(Exception):
|
|
21
|
+
"""Erreur levée par le client ReachFlow."""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
message: str,
|
|
26
|
+
*,
|
|
27
|
+
status_code: int,
|
|
28
|
+
code: ReachFlowErrorCode,
|
|
29
|
+
retryable: bool = False,
|
|
30
|
+
retry_after_ms: int | None = None,
|
|
31
|
+
body: Any = None,
|
|
32
|
+
) -> None:
|
|
33
|
+
super().__init__(message)
|
|
34
|
+
self.message = message
|
|
35
|
+
self.status_code = status_code
|
|
36
|
+
self.code = code
|
|
37
|
+
self.retryable = retryable
|
|
38
|
+
self.retry_after_ms = retry_after_ms
|
|
39
|
+
self.body = body
|
|
40
|
+
|
|
41
|
+
@classmethod
|
|
42
|
+
def from_response(cls, status: int, body: Any, fallback: str | None = None) -> ReachFlowError:
|
|
43
|
+
parsed = body if isinstance(body, dict) else {}
|
|
44
|
+
api_error = parsed.get("error")
|
|
45
|
+
message = parsed.get("message") or fallback or f"ReachFlow API error (HTTP {status})"
|
|
46
|
+
code = _map_api_error_to_code(status, api_error)
|
|
47
|
+
retryable = status == 429 or status == 408 or 500 <= status < 600
|
|
48
|
+
return cls(
|
|
49
|
+
message,
|
|
50
|
+
status_code=status,
|
|
51
|
+
code=code,
|
|
52
|
+
retryable=retryable,
|
|
53
|
+
body=body,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@classmethod
|
|
57
|
+
def network(cls, cause: BaseException) -> ReachFlowError:
|
|
58
|
+
return cls(
|
|
59
|
+
"Network error while calling ReachFlow API",
|
|
60
|
+
status_code=0,
|
|
61
|
+
code="network_error",
|
|
62
|
+
retryable=True,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
@classmethod
|
|
66
|
+
def timeout(cls, timeout_ms: int) -> ReachFlowError:
|
|
67
|
+
return cls(
|
|
68
|
+
f"Request timed out after {timeout_ms}ms",
|
|
69
|
+
status_code=408,
|
|
70
|
+
code="timeout",
|
|
71
|
+
retryable=True,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def parse_retry_after_ms(header: str | None) -> int | None:
|
|
76
|
+
if not header:
|
|
77
|
+
return None
|
|
78
|
+
try:
|
|
79
|
+
return max(0, int(float(header.strip())) * 1000)
|
|
80
|
+
except ValueError:
|
|
81
|
+
pass
|
|
82
|
+
try:
|
|
83
|
+
dt = parsedate_to_datetime(header)
|
|
84
|
+
from datetime import datetime, timezone
|
|
85
|
+
|
|
86
|
+
now = datetime.now(timezone.utc)
|
|
87
|
+
if dt.tzinfo is None:
|
|
88
|
+
dt = dt.replace(tzinfo=timezone.utc)
|
|
89
|
+
return max(0, int((dt - now).total_seconds() * 1000))
|
|
90
|
+
except (TypeError, ValueError, OverflowError):
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _map_api_error_to_code(status: int, api_error: str | None) -> ReachFlowErrorCode:
|
|
95
|
+
mapping: dict[str, ReachFlowErrorCode] = {
|
|
96
|
+
"unauthorized": "unauthorized",
|
|
97
|
+
"plan_required": "plan_required",
|
|
98
|
+
"insufficient_scope": "insufficient_scope",
|
|
99
|
+
"rate_limit_exceeded": "rate_limit_exceeded",
|
|
100
|
+
"too_many_auth_failures": "too_many_auth_failures",
|
|
101
|
+
}
|
|
102
|
+
if api_error in mapping:
|
|
103
|
+
return mapping[api_error]
|
|
104
|
+
if status == 401:
|
|
105
|
+
return "unauthorized"
|
|
106
|
+
if status == 403:
|
|
107
|
+
return "insufficient_scope"
|
|
108
|
+
if status == 404:
|
|
109
|
+
return "not_found"
|
|
110
|
+
if status in (400, 422):
|
|
111
|
+
return "validation_error"
|
|
112
|
+
if status == 429:
|
|
113
|
+
return "rate_limit_exceeded"
|
|
114
|
+
return "api_error"
|