weezy-sdk 0.1.0__py3-none-any.whl
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.
- weezy/__init__.py +5 -0
- weezy/_transport.py +43 -0
- weezy/client.py +70 -0
- weezy/py.typed +0 -0
- weezy/resources.py +1173 -0
- weezy/types.py +68 -0
- weezy_sdk-0.1.0.dist-info/METADATA +72 -0
- weezy_sdk-0.1.0.dist-info/RECORD +11 -0
- weezy_sdk-0.1.0.dist-info/WHEEL +5 -0
- weezy_sdk-0.1.0.dist-info/licenses/LICENSE +21 -0
- weezy_sdk-0.1.0.dist-info/top_level.txt +1 -0
weezy/__init__.py
ADDED
weezy/_transport.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from urllib.parse import quote, urlsplit
|
|
3
|
+
import httpx
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class WeezyError(Exception):
|
|
7
|
+
def __init__(self, message: str, *, status: int = 0, details: Any = None, request_id: str | None = None):
|
|
8
|
+
super().__init__(message)
|
|
9
|
+
self.status = status
|
|
10
|
+
self.details = details
|
|
11
|
+
self.request_id = request_id
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def segment(value: Any) -> str:
|
|
15
|
+
if value is None or str(value) in ("", ".", ".."):
|
|
16
|
+
raise ValueError("A valid path parameter is required")
|
|
17
|
+
return quote(str(value), safe="")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def validate(client_id: str, client_secret: str, base_url: str, timeout: float) -> str:
|
|
21
|
+
if not client_id or not client_secret or ":" in client_id:
|
|
22
|
+
raise ValueError("Valid client_id and client_secret are required")
|
|
23
|
+
url = urlsplit(base_url)
|
|
24
|
+
if url.scheme not in ("https", "http") or not url.netloc or url.username or url.password or url.query or url.fragment:
|
|
25
|
+
raise ValueError("Invalid base_url")
|
|
26
|
+
if not timeout > 0 or timeout == float("inf"):
|
|
27
|
+
raise ValueError("timeout must be finite and positive")
|
|
28
|
+
return base_url.rstrip("/")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def decode(response: httpx.Response, *, unwrap: bool) -> Any:
|
|
32
|
+
if response.status_code == 204:
|
|
33
|
+
return None
|
|
34
|
+
try:
|
|
35
|
+
data = response.json()
|
|
36
|
+
except ValueError:
|
|
37
|
+
raise WeezyError("API returned a non-JSON response", status=response.status_code, details=response.text,
|
|
38
|
+
request_id=response.headers.get("x-request-id")) from None
|
|
39
|
+
envelope = isinstance(data, dict) and isinstance(data.get("code"), int) and "msg" in data
|
|
40
|
+
if response.is_error or response.is_redirect or (unwrap and envelope and data['code'] >= 400):
|
|
41
|
+
raise WeezyError(data.get("msg", f"API request failed ({response.status_code})") if isinstance(data, dict) else f"API request failed ({response.status_code})",
|
|
42
|
+
status=response.status_code, details=data, request_id=response.headers.get("x-request-id"))
|
|
43
|
+
return data.get("data") if unwrap and envelope else data
|
weezy/client.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
import httpx
|
|
3
|
+
from ._transport import WeezyError, decode, segment, validate
|
|
4
|
+
from .resources import SmsAPI, AsyncSmsAPI, WhatsAppAPI, AsyncWhatsAppAPI
|
|
5
|
+
|
|
6
|
+
DEFAULT_BASE_URL = "https://api.weezy.app/client/api/v1"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Weezy:
|
|
10
|
+
def __init__(self, *, client_id: str, client_secret: str, base_url: str = DEFAULT_BASE_URL,
|
|
11
|
+
timeout: float = 30, transport: httpx.BaseTransport | None = None):
|
|
12
|
+
self._base_url = validate(client_id, client_secret, base_url, timeout)
|
|
13
|
+
self._http = httpx.Client(auth=(client_id, client_secret), timeout=timeout, transport=transport,
|
|
14
|
+
headers={"Accept": "application/json"}, follow_redirects=False)
|
|
15
|
+
self.sms = SmsAPI(self)
|
|
16
|
+
|
|
17
|
+
def whatsapp(self, instance_id: str) -> WhatsAppAPI:
|
|
18
|
+
segment(instance_id)
|
|
19
|
+
return WhatsAppAPI(self, instance_id)
|
|
20
|
+
|
|
21
|
+
def request(self, method: str, path: str, *, body: Any = None, query: dict | None = None, unwrap: bool = False) -> Any:
|
|
22
|
+
try:
|
|
23
|
+
response = self._http.request(method, self._base_url + path, json=body,
|
|
24
|
+
params={k: v for k, v in (query or {}).items() if v is not None})
|
|
25
|
+
except httpx.TimeoutException:
|
|
26
|
+
raise WeezyError("Request timed out") from None
|
|
27
|
+
except httpx.HTTPError:
|
|
28
|
+
raise WeezyError("Network request failed") from None
|
|
29
|
+
return decode(response, unwrap=unwrap)
|
|
30
|
+
|
|
31
|
+
def close(self) -> None:
|
|
32
|
+
self._http.close()
|
|
33
|
+
|
|
34
|
+
def __enter__(self):
|
|
35
|
+
return self
|
|
36
|
+
|
|
37
|
+
def __exit__(self, *args):
|
|
38
|
+
self.close()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class AsyncWeezy:
|
|
42
|
+
def __init__(self, *, client_id: str, client_secret: str, base_url: str = DEFAULT_BASE_URL,
|
|
43
|
+
timeout: float = 30, transport: httpx.AsyncBaseTransport | None = None):
|
|
44
|
+
self._base_url = validate(client_id, client_secret, base_url, timeout)
|
|
45
|
+
self._http = httpx.AsyncClient(auth=(client_id, client_secret), timeout=timeout, transport=transport,
|
|
46
|
+
headers={"Accept": "application/json"}, follow_redirects=False)
|
|
47
|
+
self.sms = AsyncSmsAPI(self)
|
|
48
|
+
|
|
49
|
+
def whatsapp(self, instance_id: str) -> AsyncWhatsAppAPI:
|
|
50
|
+
segment(instance_id)
|
|
51
|
+
return AsyncWhatsAppAPI(self, instance_id)
|
|
52
|
+
|
|
53
|
+
async def request(self, method: str, path: str, *, body: Any = None, query: dict | None = None, unwrap: bool = False) -> Any:
|
|
54
|
+
try:
|
|
55
|
+
response = await self._http.request(method, self._base_url + path, json=body,
|
|
56
|
+
params={k: v for k, v in (query or {}).items() if v is not None})
|
|
57
|
+
except httpx.TimeoutException:
|
|
58
|
+
raise WeezyError("Request timed out") from None
|
|
59
|
+
except httpx.HTTPError:
|
|
60
|
+
raise WeezyError("Network request failed") from None
|
|
61
|
+
return decode(response, unwrap=unwrap)
|
|
62
|
+
|
|
63
|
+
async def aclose(self) -> None:
|
|
64
|
+
await self._http.aclose()
|
|
65
|
+
|
|
66
|
+
async def __aenter__(self):
|
|
67
|
+
return self
|
|
68
|
+
|
|
69
|
+
async def __aexit__(self, *args):
|
|
70
|
+
await self.aclose()
|
weezy/py.typed
ADDED
|
File without changes
|