Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
347 lines (301 loc) · 13.2 KB
/
Copy path__init__.py
File metadata and controls
347 lines (301 loc) · 13.2 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""Thin, hand-written developer experience layer over the generated ReplyNodes client.
This package is never touched by ``scripts/generate.sh``; only files under
``replynodes.api``, ``replynodes.models``, and the top-level generated modules
(``replynodes/__init__.py``, ``api_client.py``, ``configuration.py``,
``exceptions.py``, ``rest.py``, ``api_response.py``) are regenerated from the
canonical OpenAPI contract. Resource/method naming mirrors the merged
TypeScript reference SDK (replynodes/replynodes-typescript sdk/src/index.ts)
translated to idiomatic Python (snake_case attributes/methods instead of
camelCase; a client instance instead of a factory function).
Public surface:
* ``ReplyNodes(api_key, base_url=None, timeout=None)`` -- the client.
* ``ReplyNodesError`` -- raised for HTTP error responses; carries
``status``, ``code``, ``request_id`` and ``details``.
* ``ReplyNodesTimeoutError`` -- raised when ``timeout`` (seconds) elapses.
* ``PUBLIC_OPERATION_REGISTRY`` -- resource -> {method: operationId} for the
canonical GET operations this wrapper exposes; used by
``scripts/check_surface_coverage.py`` to prevent silent surface drift.
"""
from __future__ import annotations
import json
import re
from types import SimpleNamespace
from typing import Any, Callable, Dict, Optional
from urllib.parse import urlparse
import urllib3
from replynodes.api.app_store_api import AppStoreApi
from replynodes.api.brand_api import BrandApi
from replynodes.api.fomo_api import FomoApi
from replynodes.api.google_api import GoogleApi
from replynodes.api.google_maps_api import GoogleMapsApi
from replynodes.api.google_play_api import GooglePlayApi
from replynodes.api.google_shopping_api import GoogleShoppingApi
from replynodes.api.hacker_news_api import HackerNewsApi
from replynodes.api.instagram_api import InstagramApi
from replynodes.api.reddit_api import RedditApi
from replynodes.api.tiktok_api import TiktokApi
from replynodes.api.web_api import WebApi
from replynodes.api.youtube_api import YoutubeApi
from replynodes.api_client import ApiClient
from replynodes.configuration import Configuration
from replynodes.exceptions import ApiException
__all__ = [
"ReplyNodes",
"ReplyNodesError",
"ReplyNodesTimeoutError",
"PUBLIC_OPERATION_REGISTRY",
]
_OFFICIAL_API_ORIGIN = "https://api.replynodes.com"
_SAFE_TEST_ORIGINS = {"https://test.invalid"}
_LOCAL_TEST_HOSTNAMES = {"localhost", "127.0.0.1", "::1"}
# The intentional public name for every canonical operation. Values are the
# generated operation IDs; this wrapper never constructs HTTP requests
# itself, it only binds generated methods under intentional resource/action
# names. ``web.search`` is a backward-compatible alias for ``google.search``
# (both resolve to the ``googleSearch`` operation), matching the naming
# convention documented in replynodes-fetcher/docs/sdk-openapi-naming.md.
PUBLIC_OPERATION_REGISTRY: Dict[str, Dict[str, str]] = {
"app_store": {
"app": "appStoreApp",
"developer": "appStoreDeveloper",
"list": "appStoreList",
"privacy": "appStorePrivacy",
"ratings": "appStoreRatings",
"reviews": "appStoreReviews",
"search": "appStoreSearch",
"similar": "appStoreSimilar",
"suggest": "appStoreSuggest",
},
"brand": {
"fonts": "brandFonts",
"retrieve": "brandRetrieve",
"search": "brandSearch",
"styleguide": "brandStyleguide",
},
"fomo": {
"alerts": "fomoAlerts",
"leaderboard": "fomoLeaderboard",
"notifications": "fomoNotifications",
"search": "fomoSearch",
"thesis": "fomoThesis",
"thesis_by_token": "fomoThesisByToken",
"thesis_by_user": "fomoThesisByUser",
"thesis_by_user_token": "fomoThesisByUserToken",
"token_holders": "fomoTokenHolders",
"tokens_graduated": "fomoTokensGraduated",
"tokens_most_held": "fomoTokensMostHeld",
"tokens_trending": "fomoTokensTrending",
"trade": "fomoTrade",
"user_balances": "fomoUserBalances",
"user_profile": "fomoUserProfile",
"user_trades": "fomoUserTrades",
},
"google": {
"search": "googleSearch",
},
"google_maps": {
"place_details": "googleMapsPlaceDetails",
"place_reviews": "googleMapsPlaceReviews",
"search_places": "googleMapsSearchPlaces",
},
"google_play": {
"app_details": "googlePlayAppDetails",
"availability": "googlePlayAvailability",
"categories": "googlePlayCategories",
"category_apps": "googlePlayCategoryApps",
"data_safety": "googlePlayDataSafety",
"developer": "googlePlayDeveloper",
"permissions": "googlePlayPermissions",
"reviews": "googlePlayReviews",
"search": "googlePlaySearch",
"similar_apps": "googlePlaySimilarApps",
"suggest": "googlePlaySuggest",
},
"google_shopping": {
"product_offers": "googleShoppingProductOffers",
"search": "googleShoppingSearch",
},
"hacker_news": {
"item": "hackerNewsItem",
"search": "hackerNewsSearch",
"stories_ask": "hackerNewsStoriesAsk",
"stories_best": "hackerNewsStoriesBest",
"stories_job": "hackerNewsStoriesJob",
"stories_new": "hackerNewsStoriesNew",
"stories_show": "hackerNewsStoriesShow",
"stories_top": "hackerNewsStoriesTop",
"user": "hackerNewsUser",
},
"instagram": {
"posts": "instagramPosts",
"profile": "instagramProfile",
},
"reddit": {
"post_by_id": "redditPostById",
"post_by_permalink": "redditPostByPermalink",
"search": "redditSearch",
"subreddit_posts": "redditSubredditPosts",
"user_activity": "redditUserActivity",
"user_posts": "redditUserPosts",
},
"tiktok": {
"post": "tiktokPost",
"user": "tiktokUser",
"user_posts": "tiktokUserPosts",
},
"web": {
"brand": "webBrand",
"crawl": "webCrawl",
"map": "webMap",
"scrape": "webScrape",
"search": "googleSearch",
},
"youtube": {
"channel": "youtubeChannel",
"comments": "youtubeComments",
"playlist": "youtubePlaylist",
"related": "youtubeRelated",
"search": "youtubeSearch",
"transcript": "youtubeTranscript",
"video": "youtubeVideo",
},
}
# (resource, method) -> resource whose generated API class actually
# implements the operation. Only ``web.search`` is an alias today; it is
# implemented by GoogleApi.google_search, not WebApi.
_ALIASES = {("web", "search"): "google"}
_API_CLASSES = {
"app_store": AppStoreApi,
"brand": BrandApi,
"fomo": FomoApi,
"google": GoogleApi,
"google_maps": GoogleMapsApi,
"google_play": GooglePlayApi,
"google_shopping": GoogleShoppingApi,
"hacker_news": HackerNewsApi,
"instagram": InstagramApi,
"reddit": RedditApi,
"tiktok": TiktokApi,
"web": WebApi,
"youtube": YoutubeApi,
}
def operation_id_to_method_name(operation_id: str) -> str:
"""Convert a canonical camelCase operationId to the generated snake_case
method name (matches OpenAPI Generator's own python naming exactly, e.g.
``youtubeChannel`` -> ``youtube_channel``, ``fomoThesisByToken`` ->
``fomo_thesis_by_token``)."""
step1 = re.sub(r"(.)([A-Z][a-z]+)", r"\1_\2", operation_id)
step2 = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", step1)
return step2.lower()
class ReplyNodesError(Exception):
"""Raised for ReplyNodes HTTP error responses (4xx/5xx)."""
def __init__(
self,
message: str,
status: Optional[int],
*,
code: Optional[str] = None,
request_id: Optional[str] = None,
details: Any = None,
) -> None:
super().__init__(message)
self.status = status
self.code = code
self.request_id = request_id
self.details = details
class ReplyNodesTimeoutError(Exception):
"""Raised when a request exceeds the configured client ``timeout``."""
def __init__(self, message: str = "ReplyNodes request timed out") -> None:
super().__init__(message)
def _validate_base_url(base_url: Optional[str]) -> Optional[str]:
if base_url is None:
return None
parsed = urlparse(base_url)
if not parsed.scheme or not parsed.netloc:
raise ValueError(
"ReplyNodes base_url must be an absolute URL for an allowed ReplyNodes or test origin"
)
if parsed.query or parsed.fragment:
raise ValueError("ReplyNodes base_url must not include a query string or fragment")
origin = f"{parsed.scheme}://{parsed.netloc}"
is_local_test_origin = parsed.hostname in _LOCAL_TEST_HOSTNAMES and parsed.scheme in ("http", "https")
if origin != _OFFICIAL_API_ORIGIN and not is_local_test_origin and origin not in _SAFE_TEST_ORIGINS:
raise ValueError(
"ReplyNodes base_url must use the official ReplyNodes API origin or an explicitly allowed test origin"
)
# Generated endpoint paths already include the public `/v1` prefix. Accept
# the common server URL form with that suffix, but do not duplicate it.
normalized_path = parsed.path.rstrip("/")
if normalized_path == "/v1":
return origin
return base_url.rstrip("/")
def _error_details(exc: ApiException) -> Any:
if not exc.body:
return None
try:
return json.loads(exc.body)
except (TypeError, ValueError):
return None
def _to_replynodes_error(exc: ApiException) -> ReplyNodesError:
details = _error_details(exc)
error_field = details.get("error") if isinstance(details, dict) else None
code = error_field.get("code") if isinstance(error_field, dict) else None
message = error_field.get("message") if isinstance(error_field, dict) else None
message = message or exc.reason or "ReplyNodes request failed"
request_id = error_field.get("request_id") if isinstance(error_field, dict) else None
if not request_id:
headers = exc.headers or {}
request_id = headers.get("X-Request-Id") or headers.get("x-request-id")
return ReplyNodesError(message, exc.status, code=code, request_id=request_id, details=details)
class ReplyNodes:
"""ReplyNodes API client.
``timeout`` is a positive number of seconds applied to every request
(unlike the merged TypeScript reference, which takes milliseconds; this
follows Python HTTP client convention such as ``requests``/``httpx``).
Omit it to use the platform's default (no client-side deadline).
Retries are always explicitly disabled (``retries=False``); this SDK
never silently retries a request, since a retried GET can still repeat a
metered/billed read against the gateway.
"""
def __init__(self, api_key: str, base_url: Optional[str] = None, timeout: Optional[float] = None) -> None:
if not api_key:
raise ValueError("ReplyNodes requires an api_key")
if timeout is not None and (isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or timeout <= 0):
raise ValueError("ReplyNodes timeout must be a positive number of seconds")
validated_base_url = _validate_base_url(base_url)
self._timeout: Optional[float] = float(timeout) if timeout is not None else None
configuration = Configuration(host=validated_base_url, access_token=api_key, retries=False)
self._api_client = ApiClient(configuration)
self._api_client.user_agent = f"replynodes-python/{_sdk_version()}"
apis = {name: api_class(self._api_client) for name, api_class in _API_CLASSES.items()}
bound_by_resource: Dict[str, Dict[str, Callable[..., Any]]] = {}
for resource_name, methods in PUBLIC_OPERATION_REGISTRY.items():
bound: Dict[str, Callable[..., Any]] = {}
for method_name, operation_id in methods.items():
alias_owner = _ALIASES.get((resource_name, method_name))
if alias_owner is not None:
bound[method_name] = bound_by_resource[alias_owner][method_name]
else:
bound[method_name] = self._bind(apis[resource_name], operation_id)
bound_by_resource[resource_name] = bound
setattr(self, resource_name, SimpleNamespace(**bound))
def _bind(self, api: Any, operation_id: str) -> Callable[..., Any]:
method_name = operation_id_to_method_name(operation_id)
generated_method = getattr(api, method_name)
def call(**kwargs: Any) -> Any:
try:
return generated_method(_request_timeout=self._timeout, **kwargs)
except ApiException as exc:
raise _to_replynodes_error(exc) from exc
except urllib3.exceptions.MaxRetryError as exc:
if isinstance(exc.reason, urllib3.exceptions.TimeoutError):
raise ReplyNodesTimeoutError() from exc
raise
except (urllib3.exceptions.ReadTimeoutError, urllib3.exceptions.ConnectTimeoutError) as exc:
raise ReplyNodesTimeoutError() from exc
call.__name__ = method_name
call.__qualname__ = f"ReplyNodes.{method_name}"
return call
def _sdk_version() -> str:
from replynodes import __version__
return __version__