-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
118 lines (94 loc) · 3.54 KB
/
Copy pathclient.py
File metadata and controls
118 lines (94 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Synchronous and asynchronous WaAPI clients."""
from __future__ import annotations
from typing import Any
import httpx
from . import __version__
from ._actions import ActionsMixin, AsyncActionsMixin
from ._http import DEFAULT_BASE_URL, DEFAULT_TIMEOUT, build_headers, interpret
_USER_AGENT = f"waapi-python-sdk/{__version__}"
class _Base:
def __init__(
self,
token: str,
*,
instance_id: int | str | None = None,
base_url: str = DEFAULT_BASE_URL,
timeout: float = DEFAULT_TIMEOUT,
) -> None:
if not token:
raise ValueError("an API token is required — create one at https://waapi.app/user/api-tokens")
self.token = token
self.instance_id = instance_id
self.base_url = base_url.rstrip("/")
self.timeout = timeout
def _resolve_instance(self, instance_id: int | str | None) -> int | str:
"""Fall back to the client-wide instance so single-instance code stays short."""
resolved = instance_id if instance_id is not None else self.instance_id
if resolved is None:
raise ValueError(
"no instance id given and none configured on the client — "
"pass instance_id=... to the call or to WaAPI(...)"
)
return resolved
class WaAPI(_Base, ActionsMixin):
"""Blocking client.
>>> client = WaAPI(token="...", instance_id=123)
>>> client.send_message(chat_id="4915112345678@c.us", message="Hello")
"""
def __init__(self, token: str, **kwargs: Any) -> None:
transport = kwargs.pop("transport", None)
super().__init__(token, **kwargs)
self._client = httpx.Client(
base_url=self.base_url,
headers=build_headers(self.token, _USER_AGENT),
timeout=self.timeout,
transport=transport,
)
def request(
self,
method: str,
path: str,
*,
json: Any = None,
params: Any = None,
check_body_status: bool = True,
) -> Any:
response = self._client.request(method, path, json=json, params=params)
return interpret(response, check_body_status=check_body_status)
def close(self) -> None:
self._client.close()
def __enter__(self) -> WaAPI:
return self
def __exit__(self, *exc: object) -> None:
self.close()
class AsyncWaAPI(_Base, AsyncActionsMixin):
"""Non-blocking client with the same method names as :class:`WaAPI`.
>>> async with AsyncWaAPI(token="...", instance_id=123) as client:
... await client.send_message(chat_id="4915112345678@c.us", message="Hi")
"""
def __init__(self, token: str, **kwargs: Any) -> None:
transport = kwargs.pop("transport", None)
super().__init__(token, **kwargs)
self._client = httpx.AsyncClient(
base_url=self.base_url,
headers=build_headers(self.token, _USER_AGENT),
timeout=self.timeout,
transport=transport,
)
async def request(
self,
method: str,
path: str,
*,
json: Any = None,
params: Any = None,
check_body_status: bool = True,
) -> Any:
response = await self._client.request(method, path, json=json, params=params)
return interpret(response, check_body_status=check_body_status)
async def aclose(self) -> None:
await self._client.aclose()
async def __aenter__(self) -> AsyncWaAPI:
return self
async def __aexit__(self, *exc: object) -> None:
await self.aclose()