skapi 2.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- skapi/__init__.py +15 -0
- skapi/_cognito.py +386 -0
- skapi/_database.py +1454 -0
- skapi/_encryption.py +1359 -0
- skapi/_forward.py +484 -0
- skapi/_http.py +373 -0
- skapi/_md5.py +94 -0
- skapi/_project.py +83 -0
- skapi/_realtime.py +549 -0
- skapi/_social.py +323 -0
- skapi/_srp.py +126 -0
- skapi/_user.py +880 -0
- skapi/_ws.py +176 -0
- skapi/client.py +270 -0
- skapi/errors.py +26 -0
- skapi-2.4.0.dist-info/METADATA +46 -0
- skapi-2.4.0.dist-info/RECORD +20 -0
- skapi-2.4.0.dist-info/WHEEL +5 -0
- skapi-2.4.0.dist-info/licenses/LICENSE +201 -0
- skapi-2.4.0.dist-info/top_level.txt +1 -0
skapi/_http.py
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The request layer: endpoint discovery, headers, body encoding, errors, rate-limit retries and
|
|
3
|
+
paging, matching skapi-js (src/utils/network.ts) so the server sees the same requests.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import hashlib
|
|
9
|
+
import json
|
|
10
|
+
import math
|
|
11
|
+
import random
|
|
12
|
+
import re
|
|
13
|
+
import threading
|
|
14
|
+
import time
|
|
15
|
+
import urllib.error
|
|
16
|
+
import urllib.request
|
|
17
|
+
from email.utils import parsedate_to_datetime
|
|
18
|
+
from typing import Any, Dict, Optional
|
|
19
|
+
from urllib.parse import quote
|
|
20
|
+
|
|
21
|
+
from .errors import SkapiError
|
|
22
|
+
|
|
23
|
+
DEFAULT_TARGET_CDN = "d3e9syvbtso631"
|
|
24
|
+
ENDPOINT_VERSION = "v2"
|
|
25
|
+
KNOWN_ERROR_CODES = ["INVALID_CORS", "INVALID_REQUEST", "SERVICE_DISABLED", "INVALID_PARAMETER", "ERROR", "EXISTS", "NOT_EXISTS"]
|
|
26
|
+
RATE_LIMIT_MAX_RETRIES = 6
|
|
27
|
+
|
|
28
|
+
ADMIN_ROUND_ROBIN = {
|
|
29
|
+
"get-newsletters", "get-public-newsletters", "subscribe-newsletter", "subscribe-public-newsletter",
|
|
30
|
+
"signupkey", "admin-newsletter-request", "admin-signup", "confirm-signup", "openid-logger",
|
|
31
|
+
"block-account", "admin-edit-profile", "remove-account", "recover-account", "mock",
|
|
32
|
+
"grant-access", "last-verified-email", "ticket", "register-ticket", "get-newsletter-subscription",
|
|
33
|
+
"send-inquiry", "register-newsletter-group", "delete-newsletter-group", "newsletter-group-endpoint",
|
|
34
|
+
"invitation-list", "csr", "csr-poll", "csr-cancel", "csr-finalize",
|
|
35
|
+
}
|
|
36
|
+
RECORD_ROUND_ROBIN = {
|
|
37
|
+
"store-subscription", "get-vapid-public-key", "push-notification", "delete-subscription",
|
|
38
|
+
"subscription", "get-subscription", "get-table", "get-tag", "get-uniqueid", "get-index",
|
|
39
|
+
"get-signed-url", "get-signed-urls", "extract-file-text", "grid-query", "anonymous-login",
|
|
40
|
+
"anonymous-session", "grant-private-access", "request-private-access-key", "get-ws-group",
|
|
41
|
+
"check-schema", "get-feed", "castspell", "dopamine", "getspell",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_counter_lock = threading.Lock()
|
|
45
|
+
_request_counter = 0 # one counter for admin and record picks, as in skapi-js
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _next_counter() -> int:
|
|
49
|
+
global _request_counter
|
|
50
|
+
with _counter_lock:
|
|
51
|
+
_request_counter += 1
|
|
52
|
+
return _request_counter
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _js_value(v: Any) -> Any:
|
|
56
|
+
"""What JSON.stringify writes: an integral float as an integer, NaN and Infinity as null."""
|
|
57
|
+
if isinstance(v, bool) or v is None:
|
|
58
|
+
return v
|
|
59
|
+
if isinstance(v, float):
|
|
60
|
+
if math.isnan(v) or math.isinf(v):
|
|
61
|
+
return None
|
|
62
|
+
return int(v) if v.is_integer() and abs(v) < 2 ** 53 else v
|
|
63
|
+
if isinstance(v, dict):
|
|
64
|
+
return {str(k): _js_value(x) for k, x in v.items()}
|
|
65
|
+
if isinstance(v, (list, tuple)):
|
|
66
|
+
return [_js_value(x) for x in v]
|
|
67
|
+
return v
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def js_json(v: Any) -> str:
|
|
71
|
+
"""JSON.stringify: compact, insertion order kept, non-ASCII left as is."""
|
|
72
|
+
return json.dumps(_js_value(v), separators=(",", ":"), ensure_ascii=False)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def encode_uri_component(text: str) -> str:
|
|
76
|
+
return quote(text, safe="-_.!~*'()")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _sorted_keys(v: Any) -> Any:
|
|
80
|
+
if isinstance(v, dict):
|
|
81
|
+
return {k: _sorted_keys(v[k]) for k in sorted(v)}
|
|
82
|
+
if isinstance(v, list):
|
|
83
|
+
return [_sorted_keys(x) for x in v]
|
|
84
|
+
return v
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _md5(text: str) -> str:
|
|
88
|
+
return hashlib.md5(text.encode("utf-8")).hexdigest()
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _retry_delay_ms(value: Optional[str]) -> Optional[float]:
|
|
92
|
+
if not value:
|
|
93
|
+
return None
|
|
94
|
+
try:
|
|
95
|
+
return max(0.0, float(value) * 1000)
|
|
96
|
+
except ValueError:
|
|
97
|
+
pass
|
|
98
|
+
try:
|
|
99
|
+
return max(0.0, parsedate_to_datetime(value).timestamp() * 1000 - time.time() * 1000)
|
|
100
|
+
except (TypeError, ValueError):
|
|
101
|
+
return None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _rate_limit_wait_ms(delay: float, attempt: int) -> float:
|
|
105
|
+
base = delay if delay > 0 else min(60000, 500 * 2 ** attempt)
|
|
106
|
+
base = min(60000, base)
|
|
107
|
+
return base + math.floor(random.random() * min(1000, max(50, base * 0.25)))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def parse_error(status: int, text: str) -> SkapiError:
|
|
111
|
+
"""A non-2xx answer as a SkapiError, read the way skapi-js reads it."""
|
|
112
|
+
try:
|
|
113
|
+
body = json.loads(text)
|
|
114
|
+
except ValueError:
|
|
115
|
+
body = text
|
|
116
|
+
if isinstance(body, str):
|
|
117
|
+
parts = body.split(":")
|
|
118
|
+
code = parts[0].strip()
|
|
119
|
+
message = ":".join(parts[1:]).strip()
|
|
120
|
+
return SkapiError(message or body, code if code in KNOWN_ERROR_CODES else "ERROR")
|
|
121
|
+
if isinstance(body, dict) and any(k in body for k in ("message", "stage", "code")):
|
|
122
|
+
code = body.get("code") or str(status) or "ERROR"
|
|
123
|
+
message = (body.get("message") or "").strip() or str(code)
|
|
124
|
+
cause = body.get("cause", body if "stage" in body else None)
|
|
125
|
+
return SkapiError(message, str(code), cause)
|
|
126
|
+
return SkapiError(str(body), "ERROR")
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class Requester:
|
|
130
|
+
def __init__(self, service: str, owner: str, version: str, target_cdn: Optional[str] = None, timeout: float = 120):
|
|
131
|
+
self.service = service
|
|
132
|
+
self.owner = owner
|
|
133
|
+
self.version = version
|
|
134
|
+
self.target_cdn = target_cdn or DEFAULT_TARGET_CDN
|
|
135
|
+
self.timeout = timeout
|
|
136
|
+
self.public_identifier = ""
|
|
137
|
+
self._admin: Optional[Dict[str, Any]] = None
|
|
138
|
+
self._record: Optional[Dict[str, Any]] = None
|
|
139
|
+
self._lock = threading.Lock()
|
|
140
|
+
# paging: the startKeys a query has seen, newest last ("end" when the list is done)
|
|
141
|
+
self._start_key_history: Dict[str, Dict[str, list]] = {}
|
|
142
|
+
|
|
143
|
+
# ---- endpoints ------------------------------------------------------------------------------
|
|
144
|
+
|
|
145
|
+
def _fetch_endpoint_file(self, name: str) -> Dict[str, Any]:
|
|
146
|
+
url = f"https://{self.target_cdn}.cloudfront.net/{self.service[:4]}/{name}-{ENDPOINT_VERSION}.json"
|
|
147
|
+
try:
|
|
148
|
+
with urllib.request.urlopen(urllib.request.Request(url), timeout=self.timeout) as resp:
|
|
149
|
+
return json.loads(resp.read().decode("utf-8"))
|
|
150
|
+
except (urllib.error.HTTPError, ValueError, UnicodeDecodeError):
|
|
151
|
+
raise SkapiError("The project ID does not exist. Create your project from skapi.com", "NOT_EXISTS") from None
|
|
152
|
+
except (urllib.error.URLError, OSError) as err:
|
|
153
|
+
raise SkapiError("Network error", "NETWORK_ERROR", err) from None
|
|
154
|
+
|
|
155
|
+
def endpoints(self):
|
|
156
|
+
with self._lock:
|
|
157
|
+
if self._admin is None:
|
|
158
|
+
self._admin = self._fetch_endpoint_file("admin")
|
|
159
|
+
if self._record is None:
|
|
160
|
+
self._record = self._fetch_endpoint_file("record")
|
|
161
|
+
return self._admin, self._record
|
|
162
|
+
|
|
163
|
+
def _select_gateway(self, kind: str, auth: bool, stable: bool = False) -> str:
|
|
164
|
+
admin, record = self.endpoints()
|
|
165
|
+
n = _next_counter()
|
|
166
|
+
if kind == "admin":
|
|
167
|
+
suffix = "_private" if auth else "_public"
|
|
168
|
+
items = [v for k, v in admin.items() if suffix in k and k not in ("get_users_private", "service_public") and isinstance(v, str)]
|
|
169
|
+
if not items:
|
|
170
|
+
raise SkapiError("No available admin gateways.", "INVALID_REQUEST")
|
|
171
|
+
else:
|
|
172
|
+
prefix = "record_private" if auth else "record_public"
|
|
173
|
+
items = [v for k, v in record.items() if prefix in k and isinstance(v, str)]
|
|
174
|
+
if not items:
|
|
175
|
+
raise SkapiError("No available record gateways.", "INVALID_REQUEST")
|
|
176
|
+
if stable:
|
|
177
|
+
return items[0]
|
|
178
|
+
return items[n % len(items)]
|
|
179
|
+
|
|
180
|
+
def endpoint_url(self, dest: str, auth: bool, stable: bool = False) -> str:
|
|
181
|
+
admin, record = self.endpoints()
|
|
182
|
+
path, _, query = dest.partition("?")
|
|
183
|
+
query = "?" + query if query else ""
|
|
184
|
+
if path == "get-users":
|
|
185
|
+
base = admin["get_users_private"]
|
|
186
|
+
elif path == "service":
|
|
187
|
+
base = admin["service_public"]
|
|
188
|
+
elif path in ADMIN_ROUND_ROBIN:
|
|
189
|
+
base = self._select_gateway("admin", auth)
|
|
190
|
+
elif path == "post-record":
|
|
191
|
+
base = record["post_private"] if auth else record["post_public"]
|
|
192
|
+
elif path == "get-records":
|
|
193
|
+
base = record["get_private"] if auth else record["get_public"]
|
|
194
|
+
elif path in ("del-files", "del-records"):
|
|
195
|
+
base = record["del_private"]
|
|
196
|
+
elif path in RECORD_ROUND_ROBIN:
|
|
197
|
+
base = self._select_gateway("record", auth, stable)
|
|
198
|
+
else:
|
|
199
|
+
if not path.startswith(("https://", "http://")):
|
|
200
|
+
raise SkapiError(f'"{dest}" is not a valid url.', "INVALID_PARAMETER")
|
|
201
|
+
return dest
|
|
202
|
+
return base + path + query
|
|
203
|
+
|
|
204
|
+
# ---- request ----------------------------------------------------------------------------------
|
|
205
|
+
|
|
206
|
+
def request(self, dest: str, data: Any = None, *, auth: bool = False, method: str = "POST",
|
|
207
|
+
token: Optional[str] = None, fetch_options: Optional[Dict[str, Any]] = None,
|
|
208
|
+
ignore_service: bool = False, stable_gateway: bool = False, content_type: Optional[str] = "application/json",
|
|
209
|
+
raw: bool = False, extra_headers: Optional[Dict[str, str]] = None) -> Any:
|
|
210
|
+
"""
|
|
211
|
+
token: the idToken, already fresh, when auth is on (sent raw as Authorization).
|
|
212
|
+
fetch_options: limit, startKey, ascending (sent) and fetchMore (paging, not sent).
|
|
213
|
+
raw: return the response bytes as they are.
|
|
214
|
+
"""
|
|
215
|
+
method = method.upper()
|
|
216
|
+
url = self.endpoint_url(dest, auth, stable_gateway)
|
|
217
|
+
service = (data or {}).get("service") if isinstance(data, dict) else None
|
|
218
|
+
owner = (data or {}).get("owner") if isinstance(data, dict) else None
|
|
219
|
+
service = service or self.service
|
|
220
|
+
owner = owner or self.owner
|
|
221
|
+
|
|
222
|
+
fetch_options = dict(fetch_options or {})
|
|
223
|
+
fetch_more = bool(fetch_options.pop("fetchMore", fetch_options.pop("fetch_more", False)))
|
|
224
|
+
if "start_key" in fetch_options and "startKey" not in fetch_options:
|
|
225
|
+
fetch_options["startKey"] = fetch_options.pop("start_key")
|
|
226
|
+
sent_options: Dict[str, Any] = {}
|
|
227
|
+
if "limit" in fetch_options and fetch_options["limit"] is not None:
|
|
228
|
+
limit = fetch_options["limit"]
|
|
229
|
+
if isinstance(limit, bool) or not isinstance(limit, (int, float)):
|
|
230
|
+
raise SkapiError("Fetch limit should be a number.", "INVALID_REQUEST")
|
|
231
|
+
if limit > 1000:
|
|
232
|
+
raise SkapiError("Fetch limit should be below 1000.", "INVALID_REQUEST")
|
|
233
|
+
sent_options["limit"] = limit
|
|
234
|
+
if fetch_options.get("startKey") is not None:
|
|
235
|
+
sent_options["startKey"] = fetch_options["startKey"]
|
|
236
|
+
if "ascending" in fetch_options and fetch_options["ascending"] is not None:
|
|
237
|
+
if not isinstance(fetch_options["ascending"], bool):
|
|
238
|
+
raise SkapiError('"ascending" should be a boolean.', "INVALID_PARAMETER")
|
|
239
|
+
sent_options["ascending"] = fetch_options["ascending"]
|
|
240
|
+
|
|
241
|
+
body: Any = {} if ignore_service else {"service": service, "owner": owner}
|
|
242
|
+
body.update(sent_options)
|
|
243
|
+
if isinstance(data, dict):
|
|
244
|
+
for k, v in data.items():
|
|
245
|
+
body[k] = v
|
|
246
|
+
elif data:
|
|
247
|
+
body = data
|
|
248
|
+
|
|
249
|
+
# paging: a query asked again with fetchMore continues from the last startKey it got
|
|
250
|
+
history_key = None
|
|
251
|
+
if isinstance(body, dict):
|
|
252
|
+
start_key = body.get("startKey")
|
|
253
|
+
if start_key:
|
|
254
|
+
if start_key == "start":
|
|
255
|
+
del body["startKey"]
|
|
256
|
+
fetch_more = False
|
|
257
|
+
elif not (start_key == "end" or (isinstance(start_key, dict) and start_key)):
|
|
258
|
+
raise SkapiError(f'"{start_key}" is invalid startKey key.', "INVALID_PARAMETER")
|
|
259
|
+
fingerprint = f"{method}:{'auth' if auth else 'public'}:{dest}"
|
|
260
|
+
# the private key of a shared record is attached once this client has it (after page 1),
|
|
261
|
+
# so it stays out of the paging key: the next page of the same query continues the same list
|
|
262
|
+
params_for_key = {k: v for k, v in body.items() if k not in ("startKey", "private_key")}
|
|
263
|
+
history_key = _md5(fingerprint + "/" + js_json(_sorted_keys(params_for_key)))
|
|
264
|
+
history = self._start_key_history.setdefault(dest, {})
|
|
265
|
+
if not fetch_more:
|
|
266
|
+
history.pop(history_key, None)
|
|
267
|
+
elif history.get(history_key):
|
|
268
|
+
last = history[history_key][-1]
|
|
269
|
+
if last == "end":
|
|
270
|
+
return {"list": [], "startKey": "end", "endOfList": True, "startKeyHistory": list(history[history_key])}
|
|
271
|
+
body["startKey"] = json.loads(last)
|
|
272
|
+
|
|
273
|
+
headers = {"Accept": "*/*"}
|
|
274
|
+
if content_type:
|
|
275
|
+
headers["Content-Type"] = content_type
|
|
276
|
+
if token:
|
|
277
|
+
headers["Authorization"] = token
|
|
278
|
+
if extra_headers:
|
|
279
|
+
headers.update(extra_headers)
|
|
280
|
+
headers["Content-Meta"] = js_json({
|
|
281
|
+
"public_identifier": self.public_identifier,
|
|
282
|
+
"service": service,
|
|
283
|
+
"owner": owner,
|
|
284
|
+
"resolves_offloaded_data": True,
|
|
285
|
+
"skapi_version": self.version,
|
|
286
|
+
"expiring_private_keys": True, # renews private access keys before they expire, so 1-hour keys are safe
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
payload = None
|
|
290
|
+
if method == "GET":
|
|
291
|
+
if isinstance(body, dict) and body:
|
|
292
|
+
pairs = "&".join(
|
|
293
|
+
encode_uri_component(str(k)) + "=" + encode_uri_component(v if isinstance(v, str) else js_json(v))
|
|
294
|
+
for k, v in body.items())
|
|
295
|
+
url += ("?" if "?" not in url else ("" if url.endswith(("?", "&")) else "&")) + pairs
|
|
296
|
+
elif isinstance(body, (bytes, bytearray)):
|
|
297
|
+
payload = bytes(body)
|
|
298
|
+
elif isinstance(body, str):
|
|
299
|
+
payload = body.encode("utf-8")
|
|
300
|
+
elif body:
|
|
301
|
+
payload = js_json(body).encode("utf-8")
|
|
302
|
+
|
|
303
|
+
result = self._send(url, method, headers, payload, raw)
|
|
304
|
+
|
|
305
|
+
if history_key is not None and isinstance(result, dict) and result.get("startKey"):
|
|
306
|
+
seen = self._start_key_history.setdefault(dest, {}).setdefault(history_key, [])
|
|
307
|
+
entry = "end" if result["startKey"] == "end" else js_json(result["startKey"])
|
|
308
|
+
if entry not in seen:
|
|
309
|
+
seen.append(entry)
|
|
310
|
+
result = dict({"startKeyHistory": list(seen)}, **result)
|
|
311
|
+
return result
|
|
312
|
+
|
|
313
|
+
def _send(self, url: str, method: str, headers: Dict[str, str], payload: Optional[bytes], raw: bool = False) -> Any:
|
|
314
|
+
attempt = 0
|
|
315
|
+
while True:
|
|
316
|
+
req = urllib.request.Request(url, data=payload, method=method, headers=headers)
|
|
317
|
+
try:
|
|
318
|
+
with urllib.request.urlopen(req, timeout=self.timeout) as resp:
|
|
319
|
+
content = resp.read()
|
|
320
|
+
if raw:
|
|
321
|
+
return content
|
|
322
|
+
text = content.decode("utf-8", errors="replace")
|
|
323
|
+
try:
|
|
324
|
+
return json.loads(text)
|
|
325
|
+
except ValueError:
|
|
326
|
+
return text
|
|
327
|
+
except urllib.error.HTTPError as err:
|
|
328
|
+
text = err.read().decode("utf-8", errors="replace")
|
|
329
|
+
if err.code == 429:
|
|
330
|
+
delay = _retry_delay_ms(err.headers.get("Retry-After") if err.headers else None)
|
|
331
|
+
if delay is None:
|
|
332
|
+
# a 429 that carries its own error (a ticket whose slots are all taken) keeps it
|
|
333
|
+
carried = parse_error(err.code, text) if text.strip() else None
|
|
334
|
+
if carried is not None and carried.code not in ("ERROR", "429"):
|
|
335
|
+
raise carried from None
|
|
336
|
+
raise SkapiError("Too many requests", "ERROR") from None
|
|
337
|
+
if attempt >= RATE_LIMIT_MAX_RETRIES:
|
|
338
|
+
raise SkapiError(f'Too many requests: "{url.split("?")[0]}" still answered 429 after {RATE_LIMIT_MAX_RETRIES} retries.',
|
|
339
|
+
"TOO_MANY_REQUESTS") from None
|
|
340
|
+
time.sleep(_rate_limit_wait_ms(delay, attempt) / 1000)
|
|
341
|
+
attempt += 1
|
|
342
|
+
continue
|
|
343
|
+
raise parse_error(err.code, text) from None
|
|
344
|
+
except (urllib.error.URLError, TimeoutError, ConnectionError, OSError) as err:
|
|
345
|
+
raise SkapiError("Network error", "NETWORK_ERROR", err) from None
|
|
346
|
+
|
|
347
|
+
def fetch_plain(self, url: str) -> bytes:
|
|
348
|
+
"""A plain GET with no extra headers: for pre-signed links, whose signature covers the headers."""
|
|
349
|
+
try:
|
|
350
|
+
with urllib.request.urlopen(urllib.request.Request(url, method="GET"), timeout=self.timeout) as resp:
|
|
351
|
+
return resp.read()
|
|
352
|
+
except urllib.error.HTTPError as err:
|
|
353
|
+
text = err.read().decode("utf-8", errors="replace")
|
|
354
|
+
code = re.search(r"<Code>(.*?)</Code>", text)
|
|
355
|
+
message = re.search(r"<Message>(.*?)</Message>", text)
|
|
356
|
+
if code or message:
|
|
357
|
+
raise SkapiError(message.group(1) if message else text[:300], code.group(1) if code else "ERROR") from None
|
|
358
|
+
raise parse_error(err.code, text) from None
|
|
359
|
+
except (urllib.error.URLError, TimeoutError, ConnectionError, OSError) as err:
|
|
360
|
+
raise SkapiError("Network error", "NETWORK_ERROR", err) from None
|
|
361
|
+
|
|
362
|
+
def upload(self, url: str, fields: Dict[str, str], filename: str, content: bytes, content_type: Optional[str]) -> Any:
|
|
363
|
+
"""multipart/form-data POST to a signed upload URL: every field in order, then the file."""
|
|
364
|
+
boundary = "----skapi" + hashlib.md5(str(random.random()).encode()).hexdigest()
|
|
365
|
+
parts = []
|
|
366
|
+
for k, v in fields.items():
|
|
367
|
+
parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{k}"\r\n\r\n{v}\r\n'.encode("utf-8"))
|
|
368
|
+
safe_name = filename.replace('"', "%22").replace("\r", "").replace("\n", "")
|
|
369
|
+
parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="{safe_name}"\r\n'
|
|
370
|
+
f'Content-Type: {content_type or "application/octet-stream"}\r\n\r\n'.encode("utf-8"))
|
|
371
|
+
parts.append(content)
|
|
372
|
+
parts.append(f"\r\n--{boundary}--\r\n".encode("utf-8"))
|
|
373
|
+
return self._send(url, "POST", {"Content-Type": f"multipart/form-data; boundary={boundary}"}, b"".join(parts))
|
skapi/_md5.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The MD5 skapi-js uses to name accounts ("<service>-<md5(e-mail or username)>"), ported literally.
|
|
3
|
+
|
|
4
|
+
It works on UTF-16 code units, not UTF-8 bytes: for ASCII it equals hashlib.md5, for U+0080..U+00FF
|
|
5
|
+
it equals MD5 of the latin-1 bytes, and above U+00FF it gives digests no standard MD5 produces. A
|
|
6
|
+
username must hash exactly as skapi-js hashes it, or that account cannot sign in from Python.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
M = 0xFFFFFFFF
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _ad(x: int, y: int) -> int:
|
|
15
|
+
return (x + y) & M
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _rl(n: int, c: int) -> int:
|
|
19
|
+
return ((n << c) | (n >> (32 - c))) & M
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _cm(q, a, b, x, s, t):
|
|
23
|
+
return _ad(_rl(_ad(_ad(a, q), _ad(x, t)), s), b)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _ff(a, b, c, d, x, s, t):
|
|
27
|
+
return _cm((b & c) | (~b & M & d), a, b, x, s, t)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _gg(a, b, c, d, x, s, t):
|
|
31
|
+
return _cm((b & d) | (c & ~d & M), a, b, x, s, t)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _hh(a, b, c, d, x, s, t):
|
|
35
|
+
return _cm(b ^ c ^ d, a, b, x, s, t)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _ii(a, b, c, d, x, s, t):
|
|
39
|
+
return _cm(c ^ (b | (~d & M)), a, b, x, s, t)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _u(n: int) -> int:
|
|
43
|
+
return n & M
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
_ROUNDS = [
|
|
47
|
+
(_ff, [(0, 7, -680876936), (1, 12, -389564586), (2, 17, 606105819), (3, 22, -1044525330), (4, 7, -176418897), (5, 12, 1200080426),
|
|
48
|
+
(6, 17, -1473231341), (7, 22, -45705983), (8, 7, 1770035416), (9, 12, -1958414417), (10, 17, -42063), (11, 22, -1990404162),
|
|
49
|
+
(12, 7, 1804603682), (13, 12, -40341101), (14, 17, -1502002290), (15, 22, 1236535329)]),
|
|
50
|
+
(_gg, [(1, 5, -165796510), (6, 9, -1069501632), (11, 14, 643717713), (0, 20, -373897302), (5, 5, -701558691), (10, 9, 38016083),
|
|
51
|
+
(15, 14, -660478335), (4, 20, -405537848), (9, 5, 568446438), (14, 9, -1019803690), (3, 14, -187363961), (8, 20, 1163531501),
|
|
52
|
+
(13, 5, -1444681467), (2, 9, -51403784), (7, 14, 1735328473), (12, 20, -1926607734)]),
|
|
53
|
+
(_hh, [(5, 4, -378558), (8, 11, -2022574463), (11, 16, 1839030562), (14, 23, -35309556), (1, 4, -1530992060), (4, 11, 1272893353),
|
|
54
|
+
(7, 16, -155497632), (10, 23, -1094730640), (13, 4, 681279174), (0, 11, -358537222), (3, 16, -722521979), (6, 23, 76029189),
|
|
55
|
+
(9, 4, -640364487), (12, 11, -421815835), (15, 16, 530742520), (2, 23, -995338651)]),
|
|
56
|
+
(_ii, [(0, 6, -198630844), (7, 10, 1126891415), (14, 15, -1416354905), (5, 21, -57434055), (12, 6, 1700485571), (3, 10, -1894986606),
|
|
57
|
+
(10, 15, -1051523), (1, 21, -2054922799), (8, 6, 1873313359), (15, 10, -30611744), (6, 15, -1560198380), (13, 21, 1309151649),
|
|
58
|
+
(4, 6, -145523070), (11, 10, -1120210379), (2, 15, 718787259), (9, 21, -343485551)]),
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def md5_js(text: str) -> str:
|
|
63
|
+
units = list(text.encode("utf-16-le"))
|
|
64
|
+
codes = [units[i] | (units[i + 1] << 8) for i in range(0, len(units), 2)]
|
|
65
|
+
length = len(codes)
|
|
66
|
+
num_blocks = ((length + 8) >> 6) + 1
|
|
67
|
+
x = [0] * (num_blocks * 16)
|
|
68
|
+
for i, cu in enumerate(codes):
|
|
69
|
+
x[i >> 2] |= (cu << ((i % 4) * 8)) & M
|
|
70
|
+
x[length >> 2] |= (0x80 << ((length % 4) * 8)) & M
|
|
71
|
+
x[num_blocks * 16 - 2] = (length * 8) & M
|
|
72
|
+
|
|
73
|
+
a, b, c, d = _u(1732584193), _u(-271733879), _u(-1732584194), _u(271733878)
|
|
74
|
+
for i in range(0, len(x), 16):
|
|
75
|
+
la, lb, lc, ld = a, b, c, d
|
|
76
|
+
for fn, steps in _ROUNDS:
|
|
77
|
+
for n, (k, s, t) in enumerate(steps):
|
|
78
|
+
t = _u(t)
|
|
79
|
+
if n % 4 == 0:
|
|
80
|
+
a = fn(a, b, c, d, x[i + k], s, t)
|
|
81
|
+
elif n % 4 == 1:
|
|
82
|
+
d = fn(d, a, b, c, x[i + k], s, t)
|
|
83
|
+
elif n % 4 == 2:
|
|
84
|
+
c = fn(c, d, a, b, x[i + k], s, t)
|
|
85
|
+
else:
|
|
86
|
+
b = fn(b, c, d, a, x[i + k], s, t)
|
|
87
|
+
a, b, c, d = _ad(a, la), _ad(b, lb), _ad(c, lc), _ad(d, ld)
|
|
88
|
+
|
|
89
|
+
alphabet = "0123456789abcdef"
|
|
90
|
+
|
|
91
|
+
def rh(n):
|
|
92
|
+
return "".join(alphabet[(n >> (j * 8 + 4)) & 0x0F] + alphabet[(n >> (j * 8)) & 0x0F] for j in range(4))
|
|
93
|
+
|
|
94
|
+
return rh(a) + rh(b) + rh(c) + rh(d)
|
skapi/_project.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Project IDs: the compact form shown on skapi.com, and the service id and owner id inside it."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from typing import Tuple
|
|
7
|
+
|
|
8
|
+
from .errors import SkapiError
|
|
9
|
+
|
|
10
|
+
BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
11
|
+
REGION_ALPHABET = "0123456789abcdefghijkl" # 22 characters, not base62 order past the digits
|
|
12
|
+
REGION_KEYS = ["us31", "us72", "ap51", "ap22", "ap41", "eu71", "ap21", "us32", "us71",
|
|
13
|
+
"af51", "ap31", "ap43", "ap23", "ap42", "ca01", "eu01", "eu72", "eu51",
|
|
14
|
+
"eu73", "eu11", "me51", "sa31"]
|
|
15
|
+
|
|
16
|
+
_UUID = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.IGNORECASE)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _b62_to_int(text: str) -> int:
|
|
20
|
+
n = 0
|
|
21
|
+
for ch in text:
|
|
22
|
+
n = n * 62 + BASE62.index(ch)
|
|
23
|
+
return n
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _int_to_b62(n: int) -> str:
|
|
27
|
+
if n == 0:
|
|
28
|
+
return "0"
|
|
29
|
+
out = ""
|
|
30
|
+
while n:
|
|
31
|
+
n, r = divmod(n, 62)
|
|
32
|
+
out = BASE62[r] + out
|
|
33
|
+
return out
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _decompress(token: str) -> str:
|
|
37
|
+
parts = token.split("-")
|
|
38
|
+
if len(parts) != 2:
|
|
39
|
+
return token
|
|
40
|
+
part1, part2 = parts
|
|
41
|
+
length = BASE62.index(part1[0])
|
|
42
|
+
c = part1[1:]
|
|
43
|
+
moved_middle = c[len(c) - (length + 2):]
|
|
44
|
+
moved_first = c[:len(c) - (length + 2)]
|
|
45
|
+
second = moved_middle[:length]
|
|
46
|
+
first = moved_middle[length:] + moved_first
|
|
47
|
+
h = format(_b62_to_int(part2), "032x")
|
|
48
|
+
uuid = f"{h[0:8]}-{h[8:12]}-{h[12:16]}-{h[16:20]}-{h[20:32]}"
|
|
49
|
+
return f"{first}-{second}-{uuid}"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def decode_project_id(project_id: str) -> Tuple[str, str]:
|
|
53
|
+
"""(service, owner) from a Project ID, compact ("5g8N...-7Zd6...") or long (7 parts)."""
|
|
54
|
+
try:
|
|
55
|
+
long_id = _decompress(project_id.strip())
|
|
56
|
+
p = long_id.split("-")
|
|
57
|
+
if len(p) != 7:
|
|
58
|
+
raise ValueError
|
|
59
|
+
region = REGION_KEYS[REGION_ALPHABET.index(p[1][0])]
|
|
60
|
+
service = region + p[0] + p[1][1:]
|
|
61
|
+
owner = "-".join(p[2:])
|
|
62
|
+
if not _UUID.match(owner):
|
|
63
|
+
raise ValueError
|
|
64
|
+
return service, owner
|
|
65
|
+
except (ValueError, IndexError):
|
|
66
|
+
raise SkapiError("Project ID is invalid.", "INVALID_PARAMETER") from None
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def format_project_id(service: str, owner: str) -> str:
|
|
70
|
+
"""The compact Project ID of a service id and owner id: the inverse of decode_project_id."""
|
|
71
|
+
first = service[4:-4]
|
|
72
|
+
second = REGION_ALPHABET[REGION_KEYS.index(service[:4])] + service[-4:]
|
|
73
|
+
part1 = BASE62[len(second)] + first[2:] + second + first[:2]
|
|
74
|
+
part2 = _int_to_b62(int(owner.replace("-", ""), 16))
|
|
75
|
+
return f"{part1}-{part2}"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def is_project_id(text: str) -> bool:
|
|
79
|
+
return isinstance(text, str) and len(text.split("-")) in (2, 7)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def is_user_id(text: str) -> bool:
|
|
83
|
+
return isinstance(text, str) and bool(_UUID.match(text))
|