-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
206 lines (173 loc) · 7.06 KB
/
client.py
File metadata and controls
206 lines (173 loc) · 7.06 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""Async ManyChat API client implementation.
This module provides an async client for interacting with the ManyChat API,
including rate limiting, retries, and error handling.
"""
from __future__ import annotations
import asyncio
import logging
from typing import Any, Dict, Optional, TypeVar, cast
from urllib.parse import urljoin
import aiohttp
import backoff
from pydantic import ValidationError
from . import __version__
from .config import ManyChatConfig, config
from .api.facebook._exceptions import (
ManyChatAPIError,
ManyChatRateLimitError,
ManyChatValidationError,
ManyChatAuthError,
)
from .api.facebook._requests import BaseRequest
from .api.facebook._responses import BaseResponse
# Type variables for generic method returns
T = TypeVar("T", bound=BaseResponse)
# Configure logger
logger = logging.getLogger(__name__)
class ManyChatClient:
"""Async client for interacting with the ManyChat API.
Handles authentication, request/response serialization, rate limiting,
retries, and error handling.
"""
def __init__(self, config: Optional[ManyChatConfig] = None):
"""Initialize the ManyChat client.
Args:
config: Configuration instance. If not provided, loads from environment.
"""
self.config = config or ManyChatConfig()
self._session: Optional[aiohttp.ClientSession] = None
self._rate_limit_semaphore = asyncio.Semaphore(
self.config.rate_limit / 60 # Convert per-minute to per-second
)
self._last_request_time: float = 0
async def __aenter__(self) -> "ManyChatClient":
"""Async context manager entry."""
await self.start()
return self
async def __aexit__(self, *args: Any) -> None:
"""Async context manager exit."""
await self.close()
async def start(self) -> None:
"""Initialize the client session."""
if self._session is None or self._session.closed:
self._session = aiohttp.ClientSession(
headers={
"User-Agent": f"ManyChat-Python-SDK/{__version__}",
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {self.config.api_key}",
},
timeout=aiohttp.ClientTimeout(total=self.config.timeout),
raise_for_status=True,
)
async def close(self) -> None:
"""Close the client session."""
if self._session and not self._session.closed:
await self._session.close()
async def _enforce_rate_limit(self) -> None:
"""Enforce rate limiting between requests."""
async with self._rate_limit_semaphore:
now = asyncio.get_event_loop().time()
elapsed = now - self._last_request_time
if elapsed < 1.0: # Ensure at least 1s between requests
await asyncio.sleep(1.0 - elapsed)
self._last_request_time = asyncio.get_event_loop().time()
@backoff.on_exception(
backoff.expo,
(
aiohttp.ClientError,
asyncio.TimeoutError,
ManyChatRateLimitError,
),
max_tries=3,
jitter=backoff.full_jitter,
)
async def _request(
self,
method: str,
endpoint: str,
request_data: Optional[BaseRequest] = None,
response_model: type[T] = BaseResponse,
) -> T:
"""Make an authenticated request to the ManyChat API.
Args:
method: HTTP method (GET, POST, etc.)
endpoint: API endpoint path
request_data: Request data model
response_model: Pydantic model for response validation
Returns:
Parsed response data
Raises:
ManyChatAPIError: For API-level errors
ManyChatAuthError: For authentication failures
ManyChatRateLimitError: For rate limiting issues
ManyChatValidationError: For request/response validation errors
"""
if self._session is None or self._session.closed:
await self.start()
url = urljoin(self.config.api_url, endpoint)
# Prepare request data
json_data = request_data.dict(exclude_none=True) if request_data else None
# Enforce rate limiting
await self._enforce_rate_limit()
try:
logger.debug(
"Making %s request to %s with data: %s",
method,
url,
json_data,
)
async with self._session.request(
method=method,
url=url,
json=json_data,
) as response:
response_data = await response.json()
logger.debug(
"Received response from %s %s: %s",
method,
url,
response_data,
)
# Handle API errors
if response.status >= 400:
if response.status == 401:
raise ManyChatAuthError("Invalid API key")
if response.status == 429:
retry_after = int(response.headers.get("Retry-After", 60))
raise ManyChatRateLimitError(
f"Rate limit exceeded. Retry after {retry_after} seconds",
retry_after=retry_after,
)
raise ManyChatAPIError(
f"API request failed with status {response.status}: {response_data}",
status_code=response.status,
response=response_data,
)
# Validate and parse response
try:
return response_model.parse_obj(response_data)
except ValidationError as e:
raise ManyChatValidationError(
f"Failed to validate response: {e}"
) from e
except aiohttp.ClientError as e:
logger.error("Request failed: %s", str(e))
raise ManyChatAPIError(f"Request failed: {str(e)}") from e
except asyncio.TimeoutError as e:
logger.error("Request timed out")
raise ManyChatAPIError("Request timed out") from e
# Public API methods will be added here
# Example:
# async def get_subscriber_info(self, subscriber_id: str) -> SubscriberInfoResponse:
# """Get subscriber information."""
# request = GetSubscriberInfoRequest(subscriber_id=subscriber_id)
# return await self._request(
# "POST",
# "/subscriber/getInfo",
# request_data=request,
# response_model=SubscriberInfoResponse,
# )
# Singleton instance for easy import
client = ManyChatClient()
__all__ = ["ManyChatClient", "client"]