-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_base_client.py
More file actions
682 lines (556 loc) · 26.1 KB
/
Copy path_base_client.py
File metadata and controls
682 lines (556 loc) · 26.1 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
"""Transport layer: URL/header assembly, retry policy, cache, quota hooks, request loops."""
from __future__ import annotations
import asyncio
import inspect
import random
import threading
import time
import warnings
from collections.abc import Awaitable, Callable, Mapping
from dataclasses import dataclass, field
from types import TracebackType
from typing import Any, TypeVar
import httpx
from ._config import ClientConfig, mask_api_key
from ._constants import (
INITIAL_RETRY_DELAY,
MAX_RETRY_DELAY,
RETRYABLE_STATUS_CODES,
UNSAFE_METHODS,
USER_AGENT,
)
from ._exceptions import APIConnectionError, APITimeoutError
from ._qs import build_query
from ._response import (
RateLimitInfo,
build_status_error,
coerce_payload,
decode_payload,
parse_rate_limit,
parse_retry_after,
)
from ._types import NotGiven, RequestOptions, RequestSpec
from .helpers.cache import CacheProtocol, cache_key, operation_name
__all__ = ["AsyncAPIClient", "BaseClient", "SyncAPIClient"]
_SyncSelfT = TypeVar("_SyncSelfT", bound="SyncAPIClient")
_AsyncSelfT = TypeVar("_AsyncSelfT", bound="AsyncAPIClient")
_ACCEPT_BY_KIND = {
"json": "application/json",
"none": "application/json",
"text": "text/plain, application/json;q=0.9, */*;q=0.8",
"bytes": "*/*",
}
#: Distinguishes "not cached" from "cached ``None``" (``DELETE`` bodies, empty 200s).
_MISS: Any = object()
#: Callback signatures for the quota hooks.
RateLimitCallback = Callable[[RateLimitInfo], None]
Unsubscribe = Callable[[], None]
async def _default_async_sleep(seconds: float) -> None:
await asyncio.sleep(seconds)
@dataclass(frozen=True)
class _CachePlan:
"""Where a response would be cached and for how long."""
key: str
ttl: float
@dataclass
class _QuotaWatch:
"""One ``on_quota_low`` registration plus its edge-trigger state."""
callback: RateLimitCallback
threshold: float
triggered: bool = False
@dataclass
class _Hooks:
"""Hook registry, shared shape for both clients."""
rate_limit: list[RateLimitCallback] = field(default_factory=list)
quota_low: list[_QuotaWatch] = field(default_factory=list)
class BaseClient:
"""Everything the sync and async clients share except the I/O itself."""
def __init__(self, config: ClientConfig, *, cache: CacheProtocol | None = None) -> None:
if cache is not None and not isinstance(cache, CacheProtocol):
raise TypeError(
"cache must implement get(key) and set(key, value, ttl) — see "
"skylink_api.helpers.cache.CacheProtocol"
)
self._config = config
self._cache = cache
self._hooks = _Hooks()
self._hook_lock = threading.Lock()
#: Whether ``config.timeout`` is sent with every request. True for
#: SDK-built transports (the pool was constructed from the same value, so
#: it is a no-op there) and for ``with_options(timeout=...)`` clones —
#: the clone shares its parent's pool, whose baked-in timeout would
#: otherwise silently win. False only for a caller-supplied
#: ``http_client`` with no explicit ``timeout``, where the caller's own
#: client configuration stays authoritative.
self._apply_config_timeout = True
#: Quota snapshot from the most recent response that carried quota
#: headers — ``X-RateLimit-Requests-*`` on RapidAPI, ``X-RateLimit-*``
#: on the direct channel.
#:
#: Last writer wins, so with concurrent requests in flight this is "one of
#: the recent responses", not "the newest". Use :meth:`on_rate_limit` when
#: you need the value that belongs to a specific response.
self.last_rate_limit: RateLimitInfo | None = None
# ── introspection ────────────────────────────────────────────────────────
@property
def config(self) -> ClientConfig:
return self._config
@property
def cache(self) -> CacheProtocol | None:
"""The response cache, or ``None`` when caching is off (the default)."""
return self._cache
def __repr__(self) -> str:
"""Short, informative and key-safe — the API key is always masked."""
return (
f"{type(self).__name__}(provider={self._config.provider!r}, "
f"base_url={self._config.base_url!r}, "
f"api_key={mask_api_key(self._config.api_key)!r}, "
f"max_retries={self._config.max_retries}, "
f"history_plan={self._config.history_plan!r}, "
f"cache={'on' if self._cache is not None else 'off'})"
)
@property
def base_url(self) -> str:
return self._config.base_url
@property
def provider(self) -> str:
return self._config.provider
@property
def max_retries(self) -> int:
return self._config.max_retries
@property
def history_plan(self) -> str:
return self._config.history_plan
# ── request assembly ─────────────────────────────────────────────────────
def _build_url(self, path: str) -> str:
"""Join ``base_url`` and a resource path.
``base_url`` is stored without a trailing slash and paths are version
independent, so a custom base URL is never given an extra ``/v3.1``.
"""
if not path.startswith("/"):
path = f"/{path}"
return f"{self._config.base_url}{path}"
def _build_headers(self, spec: RequestSpec, options: RequestOptions | None) -> dict[str, str]:
headers: dict[str, str] = {
"Accept": _ACCEPT_BY_KIND.get(spec.response_kind, "application/json"),
"User-Agent": USER_AGENT,
}
headers.update(self._config.auth_headers())
headers.update(self._config.default_headers)
if spec.headers:
headers.update(spec.headers)
if options is not None and options.get("headers"):
headers.update(options["headers"])
return headers
def _build_params(self, spec: RequestSpec, options: RequestOptions | None) -> dict[str, str]:
extra = options.get("query") if options is not None else None
return build_query(spec.query, extra)
def _resolve_max_retries(self, options: RequestOptions | None) -> int:
if options is not None and "max_retries" in options:
value = options["max_retries"]
if value < 0:
raise ValueError(f"max_retries must be >= 0, got {value}")
return value
return self._config.max_retries
def _request_kwargs(self, spec: RequestSpec, options: RequestOptions | None) -> dict[str, Any]:
kwargs: dict[str, Any] = {
"method": spec.method,
"url": self._build_url(spec.path),
"params": self._build_params(spec, options),
"headers": self._build_headers(spec, options),
}
if spec.json_body is not None:
kwargs["json"] = spec.json_body
# Precedence: the caller's per-call timeout, then the endpoint's own
# default (``/briefing/*`` only), then the client-wide config value. An
# explicit ``request_options={"timeout": ...}`` always wins, including
# when it is ``None`` (= no timeout). The config fallback must be sent
# explicitly: ``with_options`` clones share their parent's httpx client,
# so relying on the pool's baked-in default would silently ignore the
# clone's timeout.
if options is not None and "timeout" in options:
kwargs["timeout"] = options["timeout"]
elif not isinstance(spec.timeout, NotGiven):
kwargs["timeout"] = spec.timeout
elif self._apply_config_timeout:
kwargs["timeout"] = self._config.timeout
return kwargs
# ── retry policy ─────────────────────────────────────────────────────────
def _should_retry_status(self, method: str, status_code: int) -> bool:
"""429/500/502/503/504 are retried; unsafe methods only on 429.
A 429 provably never reached the handler, so replaying ``POST /webhooks``
after one cannot duplicate a subscription — a 5xx can.
"""
if status_code not in RETRYABLE_STATUS_CODES:
return False
if method.upper() in UNSAFE_METHODS:
return status_code == 429
return True
def _should_retry_transport(self, method: str) -> bool:
"""Connection/timeout failures are ambiguous — never replay unsafe methods."""
return method.upper() not in UNSAFE_METHODS
def _retry_delay(self, attempt: int, headers: Mapping[str, str] | None = None) -> float:
"""``Retry-After`` if the server sent one, else full-jitter exponential backoff.
``attempt`` is 0-based, so the delay before the first retry is drawn from
``[0, 0.5)`` seconds and the cap is reached at attempt 4.
"""
if headers is not None:
retry_after = parse_retry_after(headers)
if retry_after is not None:
return retry_after
ceiling = min(MAX_RETRY_DELAY, INITIAL_RETRY_DELAY * float(2**attempt))
return float(random.random() * ceiling)
# ── quota hooks ──────────────────────────────────────────────────────────
@staticmethod
def _reject_async_callback(callback: RateLimitCallback) -> None:
"""Hooks are invoked synchronously — a coroutine callback would be
created, dropped unawaited, and its body would never run. Fail loudly at
registration instead of silently at dispatch."""
if not callable(callback):
raise TypeError(f"callback must be callable, got {type(callback).__name__}")
if inspect.iscoroutinefunction(callback) or inspect.iscoroutinefunction(
type(callback).__call__ # callable instances: an async __call__ method
):
raise TypeError(
"Quota hook callbacks must be synchronous. An `async def` callback "
"is never awaited, so its body would never run. From a synchronous "
"callback you can schedule async work with "
"asyncio.get_running_loop().create_task(...)."
)
def on_rate_limit(self, callback: RateLimitCallback) -> Unsubscribe:
"""Call ``callback(info)`` after every response that carried quota headers.
``info`` is the snapshot parsed from *that* response, not
:attr:`last_rate_limit` — with concurrent requests the attribute is
last-writer-wins, the callback argument never is::
stop = sky.on_rate_limit(lambda info: print(info.remaining, "left"))
...
stop() # unsubscribe
Both channels' spellings are understood (``X-RateLimit-Requests-*`` on
RapidAPI, ``X-RateLimit-*`` on direct); a response carrying neither
(staging instances, some proxies) fires nothing. Error responses fire too
— a 429 carries the most interesting quota numbers of all.
An exception raised by ``callback`` never fails the request: it is caught
and re-emitted as a :class:`RuntimeWarning`, so it shows up in test runs
and in ``-W error`` setups without breaking production traffic.
The callback must be a plain synchronous callable — also on
:class:`AsyncSkyLink`. Passing an ``async def`` raises :class:`TypeError`
at registration, because the hook dispatcher never awaits.
Returns:
A function that removes the callback; calling it twice is harmless.
"""
self._reject_async_callback(callback)
with self._hook_lock:
self._hooks.rate_limit.append(callback)
def unsubscribe() -> None:
with self._hook_lock:
if callback in self._hooks.rate_limit:
self._hooks.rate_limit.remove(callback)
return unsubscribe
def on_quota_low(
self,
callback: RateLimitCallback,
*,
threshold: float = 0.1,
) -> Unsubscribe:
"""Call ``callback(info)`` when ``remaining / limit`` drops to ``threshold``.
Edge triggered, not level triggered: it fires **once** on the crossing and
stays quiet until the ratio climbs back above ``threshold`` (a new quota
window), which then re-arms it. Otherwise every remaining call of the day
would fire the callback.
Responses that carry no ``limit``/``remaining`` pair, or a ``limit`` of
zero, are ignored — a ratio cannot be computed from them.
Args:
callback: Receives the :class:`RateLimitInfo` of the crossing response.
Must be synchronous — an ``async def`` raises :class:`TypeError`
at registration, because the hook dispatcher never awaits.
threshold: Fraction of the quota left, ``0 < threshold <= 1``
(default ``0.1`` — the last 10%).
Returns:
A function that removes the callback.
"""
self._reject_async_callback(callback)
if not 0 < threshold <= 1:
raise ValueError(f"threshold must be in (0, 1], got {threshold}")
watch = _QuotaWatch(callback=callback, threshold=threshold)
with self._hook_lock:
self._hooks.quota_low.append(watch)
def unsubscribe() -> None:
with self._hook_lock:
if watch in self._hooks.quota_low:
self._hooks.quota_low.remove(watch)
return unsubscribe
def _record_rate_limit(self, response: httpx.Response) -> None:
"""Publish the quota headers of one response: attribute first, then hooks."""
rate_limit = parse_rate_limit(response.headers)
if rate_limit is None:
return
self.last_rate_limit = rate_limit
self._dispatch_rate_limit(rate_limit)
def _dispatch_rate_limit(self, info: RateLimitInfo) -> None:
"""Run the hooks for ``info``; user code never runs while the lock is held."""
ratio: float | None = None
if info.limit is not None and info.remaining is not None and info.limit > 0:
ratio = info.remaining / info.limit
with self._hook_lock:
callbacks = list(self._hooks.rate_limit)
due: list[RateLimitCallback] = []
for watch in self._hooks.quota_low:
if ratio is None:
continue
if ratio <= watch.threshold:
if not watch.triggered:
watch.triggered = True
due.append(watch.callback)
else:
watch.triggered = False
for callback in [*callbacks, *due]:
try:
callback(info)
except Exception as err: # a bad hook must not fail the call
warnings.warn(
f"SkyLink quota hook {getattr(callback, '__name__', callback)!r} "
f"raised {type(err).__name__}: {err}",
RuntimeWarning,
stacklevel=2,
)
def _copy_hooks_from(self, other: BaseClient) -> None:
"""Snapshot ``other``'s hooks into this client (used by ``with_options``)."""
with other._hook_lock:
rate_limit = list(other._hooks.rate_limit)
quota_low = [
_QuotaWatch(watch.callback, watch.threshold, watch.triggered)
for watch in other._hooks.quota_low
]
with self._hook_lock:
self._hooks.rate_limit = rate_limit
self._hooks.quota_low = quota_low
# ── response cache ───────────────────────────────────────────────────────
def _cache_plan(self, spec: RequestSpec, options: RequestOptions | None) -> _CachePlan | None:
"""Where this request would be cached, or ``None`` when it must not be.
Only ``GET`` is cacheable — replaying anything else would be a lie — and a
resolved TTL of ``0`` (the default for every operation) means "no caching",
which is why an unconfigured cache changes nothing.
"""
cache = self._cache
if cache is None or spec.method.upper() != "GET":
return None
ttl = 0.0
ttl_for = getattr(cache, "ttl_for", None)
if callable(ttl_for):
operation = spec.operation or operation_name(spec.path)
try:
ttl = float(ttl_for(operation))
except Exception as err: # a broken cache degrades to no cache
self._warn_cache("ttl_for", err)
return None
if ttl <= 0:
return None
key = cache_key(
spec.method,
spec.path,
self._build_params(spec, options),
provider=self._config.provider,
base_url=self._config.base_url,
)
return _CachePlan(key=key, ttl=ttl)
def _cache_load(self, plan: _CachePlan, spec: RequestSpec) -> Any:
"""Cached payload for ``spec``, or :data:`_MISS`."""
cache = self._cache
if cache is None:
return _MISS
try:
entry = cache.get(plan.key)
except Exception as err: # a broken cache degrades to no cache
self._warn_cache("get", err)
return _MISS
if not isinstance(entry, Mapping) or entry.get("kind") != spec.response_kind:
# Absent, or stored for a different decoding of the same URL.
return _MISS
return entry.get("payload")
def _cache_store(self, plan: _CachePlan, spec: RequestSpec, payload: Any) -> None:
cache = self._cache
if cache is None:
return
try:
cache.set(plan.key, {"kind": spec.response_kind, "payload": payload}, plan.ttl)
except Exception as err: # a broken cache degrades to no cache
self._warn_cache("set", err)
@staticmethod
def _warn_cache(operation: str, err: Exception) -> None:
warnings.warn(
f"SkyLink response cache {operation}() raised {type(err).__name__}: {err} — "
"continuing without the cache.",
RuntimeWarning,
stacklevel=3,
)
class SyncAPIClient(BaseClient):
"""Blocking transport built on a single :class:`httpx.Client`."""
def __init__(
self,
config: ClientConfig,
*,
http_client: httpx.Client | None = None,
sleep: Callable[[float], None] = time.sleep,
cache: CacheProtocol | None = None,
owns_transport: bool = True,
) -> None:
super().__init__(config, cache=cache)
self._sleep = sleep
self._owns_transport = owns_transport
# A caller-supplied client keeps its own timeout configuration unless an
# explicit ``timeout`` was also given (the public constructors and
# ``with_options`` flip this back on in that case).
self._apply_config_timeout = http_client is None
self._http_client = http_client or httpx.Client(
timeout=config.timeout,
follow_redirects=True,
)
@property
def http_client(self) -> httpx.Client:
return self._http_client
def execute(self, spec: RequestSpec, options: RequestOptions | None = None) -> Any:
"""Send ``spec``, retrying per policy, and decode the response.
The response cache is consulted here and nowhere else: a hit skips the
network entirely and re-validates the stored payload, a successful GET
stores its payload before it is validated.
"""
max_retries = self._resolve_max_retries(options)
attempt = 0
plan = self._cache_plan(spec, options)
if plan is not None:
payload = self._cache_load(plan, spec)
if payload is not _MISS:
return coerce_payload(spec, payload)
while True:
request = self._http_client.build_request(**self._request_kwargs(spec, options))
try:
response = self._http_client.send(request)
except httpx.TimeoutException as err:
if attempt < max_retries and self._should_retry_transport(spec.method):
self._sleep(self._retry_delay(attempt))
attempt += 1
continue
raise APITimeoutError(f"Request timed out: {spec.method} {request.url}") from err
except httpx.HTTPError as err:
if attempt < max_retries and self._should_retry_transport(spec.method):
self._sleep(self._retry_delay(attempt))
attempt += 1
continue
raise APIConnectionError(
f"Connection error for {spec.method} {request.url}: {err}"
) from err
self._record_rate_limit(response)
if response.is_success:
payload = decode_payload(spec, response)
if plan is not None:
self._cache_store(plan, spec, payload)
return coerce_payload(spec, payload, status_code=response.status_code)
retryable = self._should_retry_status(spec.method, response.status_code)
if attempt < max_retries and retryable:
delay = self._retry_delay(attempt, response.headers)
response.close()
self._sleep(delay)
attempt += 1
continue
raise build_status_error(response)
def close(self) -> None:
"""Close the underlying HTTP connection pool.
A clone made by ``with_options`` shares its parent's transport and does
**not** own it, so closing the clone is a no-op — only the client that
created the pool (or was handed one at construction) closes it.
"""
if self._owns_transport:
self._http_client.close()
def __enter__(self: _SyncSelfT) -> _SyncSelfT:
return self
def __exit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> None:
self.close()
class AsyncAPIClient(BaseClient):
"""Asyncio transport built on a single :class:`httpx.AsyncClient`."""
def __init__(
self,
config: ClientConfig,
*,
http_client: httpx.AsyncClient | None = None,
sleep: Callable[[float], Awaitable[None]] = _default_async_sleep,
cache: CacheProtocol | None = None,
owns_transport: bool = True,
) -> None:
super().__init__(config, cache=cache)
self._sleep = sleep
self._owns_transport = owns_transport
# Same rule as the sync transport: a caller-supplied client keeps its
# own timeout configuration unless ``timeout`` was given explicitly.
self._apply_config_timeout = http_client is None
self._http_client = http_client or httpx.AsyncClient(
timeout=config.timeout,
follow_redirects=True,
)
@property
def http_client(self) -> httpx.AsyncClient:
return self._http_client
async def execute(self, spec: RequestSpec, options: RequestOptions | None = None) -> Any:
"""Send ``spec``, retrying per policy, and decode the response.
The response cache is consulted here and nowhere else — see
:meth:`SyncAPIClient.execute`.
"""
max_retries = self._resolve_max_retries(options)
attempt = 0
plan = self._cache_plan(spec, options)
if plan is not None:
payload = self._cache_load(plan, spec)
if payload is not _MISS:
return coerce_payload(spec, payload)
while True:
request = self._http_client.build_request(**self._request_kwargs(spec, options))
try:
response = await self._http_client.send(request)
except httpx.TimeoutException as err:
if attempt < max_retries and self._should_retry_transport(spec.method):
await self._sleep(self._retry_delay(attempt))
attempt += 1
continue
raise APITimeoutError(f"Request timed out: {spec.method} {request.url}") from err
except httpx.HTTPError as err:
if attempt < max_retries and self._should_retry_transport(spec.method):
await self._sleep(self._retry_delay(attempt))
attempt += 1
continue
raise APIConnectionError(
f"Connection error for {spec.method} {request.url}: {err}"
) from err
self._record_rate_limit(response)
if response.is_success:
payload = decode_payload(spec, response)
if plan is not None:
self._cache_store(plan, spec, payload)
return coerce_payload(spec, payload, status_code=response.status_code)
retryable = self._should_retry_status(spec.method, response.status_code)
if attempt < max_retries and retryable:
delay = self._retry_delay(attempt, response.headers)
await response.aclose()
await self._sleep(delay)
attempt += 1
continue
raise build_status_error(response)
async def aclose(self) -> None:
"""Close the underlying HTTP connection pool.
A clone made by ``with_options`` does not own the transport it shares, so
closing it leaves the pool alone — see :meth:`SyncAPIClient.close`.
"""
if self._owns_transport:
await self._http_client.aclose()
async def __aenter__(self: _AsyncSelfT) -> _AsyncSelfT:
return self
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> None:
await self.aclose()