-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_requests.py
More file actions
263 lines (222 loc) · 8.51 KB
/
_requests.py
File metadata and controls
263 lines (222 loc) · 8.51 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
import importlib
import re
from collections import OrderedDict
from dataclasses import dataclass
import time
from ._internal_logging import (
InternalLogger,
)
import requests
from requests import Response, RequestException
from requests.adapters import HTTPAdapter
from tenacity import (
retry,
stop_after_delay,
wait_exponential,
retry_if_exception_type,
)
logger = InternalLogger(__name__)
try:
from importlib.metadata import version
Version = version("prefab-cloud-python")
except importlib.metadata.PackageNotFoundError:
Version = "development"
VersionHeader = "X-PrefabCloud-Client-Version"
DEFAULT_TIMEOUT = 5 # seconds
# from https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
class TimeoutHTTPAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs) -> None:
self.timeout = kwargs.pop("timeout", DEFAULT_TIMEOUT)
super().__init__(*args, **kwargs)
def send(self, request, **kwargs) -> Response:
if "timeout" not in kwargs:
kwargs["timeout"] = self.timeout
return super().send(request, **kwargs)
class NoRetryAdapter(HTTPAdapter):
def send(
self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None
):
return super().send(request, stream, timeout, verify, cert, proxies)
class UnauthorizedException(Exception):
def __init__(self, api_key):
api_key_prefix = api_key[:10] if api_key else ""
super().__init__(
f"Prefab attempts to fetch data are unauthorized using api key starting with {api_key_prefix}. Please check your api key."
)
class HostIterator:
def __init__(self, hosts):
self.hosts = hosts
self.index = 0
def __iter__(self):
return self
def __next__(self):
if not self.hosts:
raise StopIteration
host = self.hosts[self.index]
self.index = (self.index + 1) % len(self.hosts)
return host
# --- Simple LRU Cache Implementation ---
@dataclass
class CacheEntry:
data: bytes
etag: str
expires_at: float
url: str # The full URL from the successful response
class LRUCache:
def __init__(self, max_size: int):
self.max_size = max_size
self.cache = OrderedDict()
def get(self, key):
try:
value = self.cache.pop(key)
self.cache[key] = value # Mark as recently used.
return value
except KeyError:
return None
def set(self, key, value):
if key in self.cache:
self.cache.pop(key)
elif len(self.cache) >= self.max_size:
self.cache.popitem(last=False)
self.cache[key] = value
def clear(self):
self.cache.clear()
def __len__(self):
return len(self.cache)
class ApiClient:
def __init__(self, options):
"""
:param options: An object with attributes such as:
- prefab_api_urls: list of API host URLs (e.g. ["https://a.example.com", "https://b.example.com"])
- version: version string
"""
self.hosts = options.prefab_api_urls
self.session = requests.Session()
self.session.mount("https://", requests.adapters.HTTPAdapter())
self.session.mount("http://", requests.adapters.HTTPAdapter())
self.session.headers.update(
{
"X-PrefabCloud-Client-Version": f"prefab-cloud-python-{getattr(options, 'version', 'development')}"
}
)
# Initialize a cache (here with a maximum of 2 entries).
self.cache = LRUCache(max_size=2)
def get_host(self, attempt_number, host_list):
return host_list[attempt_number % len(host_list)]
def _get_attempt_number(self) -> int:
"""
Retrieve the current attempt number from tenacity's statistics if available,
otherwise default to 1.
"""
stats = getattr(self.resilient_request, "statistics", None)
if stats is None:
return 1
return stats.get("attempt_number", 1)
def _build_url(self, path, hosts: list[str] = None) -> str:
"""
Build the full URL using host-selection logic.
"""
attempt_number = self._get_attempt_number()
host = self.get_host(attempt_number - 1, hosts or self.hosts)
return f"{host.rstrip('/')}/{path.lstrip('/')}"
def _get_cached_response(self, url: str) -> Response:
"""
If a valid cache entry exists for the given URL, return a synthetic Response.
"""
now = time.time()
entry = self.cache.get(url)
if entry is not None and entry.expires_at > now:
resp = Response()
resp._content = entry.data
resp.status_code = 200
resp.headers = {"ETag": entry.etag, "X-Cache": "HIT"}
resp.url = entry.url
return resp
return None
def _apply_cache_headers(self, url: str, kwargs: dict) -> dict:
"""
If a stale cache entry exists, add its ETag as an 'If-None-Match' header.
"""
entry = self.cache.get(url)
headers = kwargs.get("headers", {}).copy()
if entry is not None and entry.etag:
headers["If-None-Match"] = entry.etag
kwargs["headers"] = headers
return kwargs
def _update_cache(self, url: str, response: Response) -> None:
"""
If the response is cacheable (status 200, and Cache-Control does not include 'no-store'),
update the cache. If Cache-Control includes 'no-cache', mark the cache entry as immediately expired,
so that subsequent requests always trigger revalidation.
"""
cache_control = response.headers.get("Cache-Control", "")
if "no-store" in cache_control.lower():
return
etag = response.headers.get("ETag")
max_age = 0
m = re.search(r"max-age=(\d+)", cache_control)
if m:
max_age = int(m.group(1))
# If 'no-cache' is present, then even though we may store the response,
# we treat it as expired immediately so that every subsequent request is revalidated.
if "no-cache" in cache_control.lower():
expires_at = time.time() # Immediately expired.
else:
expires_at = time.time() + max_age if max_age > 0 else 0
if (etag is not None or max_age > 0) and expires_at > time.time():
self.cache.set(
url,
CacheEntry(
data=response.content,
etag=etag,
expires_at=expires_at,
url=response.url,
),
)
response.headers["X-Cache"] = "MISS"
def _send_request(self, method: str, url: str, **kwargs) -> Response:
"""
Hook method to perform the actual HTTP request.
"""
return self.session.request(method, url, **kwargs)
@retry(
stop=stop_after_delay(8),
wait=wait_exponential(multiplier=1, min=0.05, max=2),
retry=retry_if_exception_type((RequestException, ConnectionError, OSError)),
)
def resilient_request(
self,
path,
method="GET",
allow_cache: bool = False,
hosts: list[str] = None,
**kwargs,
) -> Response:
"""
Makes a resilient (retrying) request.
If allow_cache is True and the request method is GET, caching logic is applied.
This includes:
- Checking the cache and returning a synthetic response if valid.
- Adding an 'If-None-Match' header when a stale entry exists.
- Handling a 304 (Not Modified) response by returning the cached entry.
- Caching a 200 response if Cache-Control permits.
"""
url = self._build_url(path, hosts)
if method.upper() == "GET" and allow_cache:
cached = self._get_cached_response(url)
if cached:
return cached
kwargs = self._apply_cache_headers(url, kwargs)
response = self._send_request(method, url, **kwargs)
if method.upper() == "GET" and allow_cache:
if response.status_code == 304:
cached = self.cache.get(url)
if cached:
resp = Response()
resp._content = cached.data
resp.status_code = 200
resp.headers = {"ETag": cached.etag, "X-Cache": "HIT"}
resp.url = cached.url
return resp
self._update_cache(url, response)
return response