skapi 2.4.0__tar.gz → 2.4.4__tar.gz

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.
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: skapi
3
- Version: 2.4.0
3
+ Version: 2.4.4
4
4
  Summary: Serverless backend for Python: sign-in, users and a database, with the Skapi API.
5
5
  Author: broadway Inc.
6
6
  License: Apache-2.0
7
7
  Project-URL: Homepage, https://www.skapi.com
8
- Project-URL: Documentation, https://docs.skapi.com
8
+ Project-URL: Documentation, https://py.skapi.com
9
9
  Keywords: backend,skapi,api,serverless,database,authentication
10
10
  Classifier: Programming Language :: Python :: 3
11
11
  Classifier: License :: OSI Approved :: Apache Software License
@@ -19,9 +19,9 @@ Provides-Extra: encryption
19
19
  Requires-Dist: cryptography>=3.4; extra == "encryption"
20
20
  Dynamic: license-file
21
21
 
22
- # skapi
22
+ # Skapi for Python
23
23
 
24
- The [Skapi](https://www.skapi.com) backend for Python: sign users in, keep their sessions, and read and write your project's database. Same API as [skapi-js](https://www.npmjs.com/package/skapi-js), in Python style.
24
+ The [Skapi](https://www.skapi.com) backend for Python: sign users in, keep their sessions, and read and write your project's database.
25
25
 
26
26
  ```
27
27
  pip install skapi
@@ -44,3 +44,5 @@ posts = skapi.get_records({"table": {"name": "posts", "access_group": "public"}}
44
44
  - Optional encryption of private records before upload, compatible with skapi-js: `pip install "skapi[encryption]"`.
45
45
 
46
46
  Documentation: https://py.skapi.com
47
+
48
+ PyPi: https://pypi.org/project/skapi/
@@ -1,6 +1,6 @@
1
- # skapi
1
+ # Skapi for Python
2
2
 
3
- The [Skapi](https://www.skapi.com) backend for Python: sign users in, keep their sessions, and read and write your project's database. Same API as [skapi-js](https://www.npmjs.com/package/skapi-js), in Python style.
3
+ The [Skapi](https://www.skapi.com) backend for Python: sign users in, keep their sessions, and read and write your project's database.
4
4
 
5
5
  ```
6
6
  pip install skapi
@@ -23,3 +23,5 @@ posts = skapi.get_records({"table": {"name": "posts", "access_group": "public"}}
23
23
  - Optional encryption of private records before upload, compatible with skapi-js: `pip install "skapi[encryption]"`.
24
24
 
25
25
  Documentation: https://py.skapi.com
26
+
27
+ PyPi: https://pypi.org/project/skapi/
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "skapi"
7
- version = "2.4.0"
7
+ version = "2.4.4"
8
8
  description = "Serverless backend for Python: sign-in, users and a database, with the Skapi API."
9
9
  readme = "README.md"
10
10
  license = { text = "Apache-2.0" }
@@ -26,7 +26,7 @@ encryption = ["cryptography>=3.4"]
26
26
 
27
27
  [project.urls]
28
28
  Homepage = "https://www.skapi.com"
29
- Documentation = "https://docs.skapi.com"
29
+ Documentation = "https://py.skapi.com"
30
30
 
31
31
  [tool.setuptools.packages.find]
32
32
  where = ["src"]
@@ -6,7 +6,7 @@ project's database with the same API as skapi-js.
6
6
  skapi = Skapi("<Project ID>")
7
7
  """
8
8
 
9
- __version__ = "2.4.0"
9
+ __version__ = "2.4.4"
10
10
 
11
11
  from .errors import SkapiError # noqa: E402
12
12
  from .client import Skapi # noqa: E402
@@ -27,6 +27,10 @@ METHODS = ("get", "post", "delete", "put", "patch", "head")
27
27
  UPPER_METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"]
28
28
  STREAM_DEGRADED_RETRIES = 3
29
29
  MULTIPART_LIMIT = 2 * 1024 * 1024 - 64 * 1024
30
+ # The shortest polling interval accepted, in milliseconds (skapi-js 2.4.4 MIN_POLL_INTERVAL_MS). A
31
+ # shorter one is refused with INVALID_PARAMETER, never raised quietly: applies to forward_request's
32
+ # `poll` (0 still means "do not poll"), forward_request_poll's `poll` and forward_request_stream's `poll`.
33
+ MIN_POLL_INTERVAL_MS = 1000
30
34
 
31
35
 
32
36
  def _jd(v):
@@ -34,6 +38,13 @@ def _jd(v):
34
38
  return json.dumps(v, default=repr, ensure_ascii=False)
35
39
 
36
40
 
41
+ def _assert_poll_interval(value: Any, name: str = "poll") -> None:
42
+ """Refuse an interval that is not a finite number of at least MIN_POLL_INTERVAL_MS."""
43
+ if isinstance(value, bool) or not isinstance(value, (int, float)) or value != value \
44
+ or value in (float("inf"), float("-inf")) or value < MIN_POLL_INTERVAL_MS:
45
+ raise _p(f'"{name}" should be a number of milliseconds, {MIN_POLL_INTERVAL_MS} or more.')
46
+
47
+
37
48
  def _p(message: str, code: str = "INVALID_PARAMETER") -> SkapiError:
38
49
  return SkapiError(message, code)
39
50
 
@@ -132,7 +143,7 @@ class ForwardMixin:
132
143
  """
133
144
  Calls another API through Skapi. options: url (required), method, headers, data, params,
134
145
  secretName (a client secret stored on skapi.com; put "$CLIENT_SECRET" where it goes),
135
- skapiHeaders, queue, expires, stream, poll (milliseconds between checks), multipart.
146
+ skapiHeaders, queue, expires, stream, poll (milliseconds between checks, 1000 or more; 0 does not poll), multipart.
136
147
 
137
148
  Without queue/poll/stream: returns the API's answer. With poll or stream: waits for the
138
149
  answer and returns it, handing streamed text to on_stream(text, seq) as it arrives. With a
@@ -185,8 +196,9 @@ class ForwardMixin:
185
196
  def _dispatch_forward(self, params: Dict[str, Any], on_stream: Optional[Callable], response_type: Optional[str]) -> Any:
186
197
  secret_named = bool(params.get("secretName"))
187
198
  poll = params.pop("poll", None)
188
- if isinstance(poll, (int, float)) and not isinstance(poll, bool) and poll < 0:
189
- raise _p('"poll" should be a non-negative number.')
199
+ # 0 (or False, or absent) means "do not poll"; any other number is the interval.
200
+ if isinstance(poll, (int, float)) and not isinstance(poll, bool) and poll != 0:
201
+ _assert_poll_interval(poll)
190
202
  latency = poll if isinstance(poll, (int, float)) and not isinstance(poll, bool) else (1000 if poll else 0)
191
203
  self._ensure_connection()
192
204
  signed_in = self._signed_in()
@@ -300,9 +312,8 @@ class ForwardMixin:
300
312
  """Waits for a queued request (its request_id, or its short id with url and method) and returns the answer."""
301
313
  if isinstance(poll, bool) or not isinstance(poll, (int, float)):
302
314
  raise _p('"poll" should be a number.')
303
- if poll < 0:
304
- raise _p('"poll" should be a non-negative number.')
305
315
  poll = poll or 1000
316
+ _assert_poll_interval(poll)
306
317
  full_id = self._request_id(request_id, url, method, service)
307
318
  self._ensure_connection()
308
319
  cursor, stalled = 0, 0
@@ -340,8 +351,7 @@ class ForwardMixin:
340
351
  options = dict(options or {})
341
352
  poll = options.get("poll")
342
353
  latency = poll if isinstance(poll, (int, float)) and not isinstance(poll, bool) else 1000
343
- if latency < 0:
344
- raise _p('"poll" should be a non-negative number.')
354
+ _assert_poll_interval(latency)
345
355
  if "since" in options and (isinstance(options["since"], bool) or not isinstance(options["since"], (int, float))):
346
356
  raise _p('"since" should be a number.')
347
357
  full_id = self._request_id(request_id, options.get("url"), options.get("method"), options.get("service"))
@@ -6,17 +6,19 @@ paging, matching skapi-js (src/utils/network.ts) so the server sees the same req
6
6
  from __future__ import annotations
7
7
 
8
8
  import hashlib
9
+ import http.client
9
10
  import json
10
11
  import math
11
12
  import random
12
13
  import re
14
+ import socket
13
15
  import threading
14
16
  import time
15
17
  import urllib.error
16
18
  import urllib.request
17
19
  from email.utils import parsedate_to_datetime
18
20
  from typing import Any, Dict, Optional
19
- from urllib.parse import quote
21
+ from urllib.parse import quote, urlsplit
20
22
 
21
23
  from .errors import SkapiError
22
24
 
@@ -24,6 +26,23 @@ DEFAULT_TARGET_CDN = "d3e9syvbtso631"
24
26
  ENDPOINT_VERSION = "v2"
25
27
  KNOWN_ERROR_CODES = ["INVALID_CORS", "INVALID_REQUEST", "SERVICE_DISABLED", "INVALID_PARAMETER", "ERROR", "EXISTS", "NOT_EXISTS"]
26
28
  RATE_LIMIT_MAX_RETRIES = 6
29
+ # Uploads (2.4.4): the body goes out in blocks of this size, each with its own `timeout`, so the
30
+ # limit is "no progress for `timeout` seconds", never the file's total time. urllib sends a bytes
31
+ # body with ONE sendall(), and since Python 3.5 the socket timeout covers that whole call: an
32
+ # upload taking longer than `timeout` was cut off while still moving.
33
+ UPLOAD_BLOCK_BYTES = 64 * 1024
34
+ # Once the whole body is handed off, MB can still drain from the OS buffers with nothing to show;
35
+ # the answer may take this many times `timeout` before the upload counts as stalled.
36
+ UPLOAD_SENT_WAIT_FACTOR = 5
37
+
38
+
39
+ def _timed_out(err: BaseException) -> bool:
40
+ """True when `err` is the socket limit running out (nothing moved for `timeout` seconds), also
41
+ when urllib wraps it in URLError. socket.timeout is TimeoutError from Python 3.10, an OSError
42
+ of its own on 3.9."""
43
+ if isinstance(err, urllib.error.URLError) and not isinstance(err, urllib.error.HTTPError):
44
+ err = err.reason if isinstance(err.reason, BaseException) else err
45
+ return isinstance(err, (socket.timeout, TimeoutError))
27
46
 
28
47
  ADMIN_ROUND_ROBIN = {
29
48
  "get-newsletters", "get-public-newsletters", "subscribe-newsletter", "subscribe-public-newsletter",
@@ -342,6 +361,10 @@ class Requester:
342
361
  continue
343
362
  raise parse_error(err.code, text) from None
344
363
  except (urllib.error.URLError, TimeoutError, ConnectionError, OSError) as err:
364
+ # A connection with nothing moving for `timeout` seconds fails with TIMEOUT, as in
365
+ # skapi-js 2.4.4 (its "No response for 60 seconds."); other failures stay NETWORK_ERROR.
366
+ if _timed_out(err):
367
+ raise SkapiError(f"No response for {self.timeout:g} seconds.", "TIMEOUT", err) from None
345
368
  raise SkapiError("Network error", "NETWORK_ERROR", err) from None
346
369
 
347
370
  def fetch_plain(self, url: str) -> bytes:
@@ -357,6 +380,8 @@ class Requester:
357
380
  raise SkapiError(message.group(1) if message else text[:300], code.group(1) if code else "ERROR") from None
358
381
  raise parse_error(err.code, text) from None
359
382
  except (urllib.error.URLError, TimeoutError, ConnectionError, OSError) as err:
383
+ if _timed_out(err):
384
+ raise SkapiError(f"No response for {self.timeout:g} seconds.", "TIMEOUT", err) from None
360
385
  raise SkapiError("Network error", "NETWORK_ERROR", err) from None
361
386
 
362
387
  def upload(self, url: str, fields: Dict[str, str], filename: str, content: bytes, content_type: Optional[str]) -> Any:
@@ -370,4 +395,55 @@ class Requester:
370
395
  f'Content-Type: {content_type or "application/octet-stream"}\r\n\r\n'.encode("utf-8"))
371
396
  parts.append(content)
372
397
  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))
398
+ return self._send_upload(url, {"Content-Type": f"multipart/form-data; boundary={boundary}"}, b"".join(parts), filename)
399
+
400
+ def _send_upload(self, url: str, headers: Dict[str, str], payload: bytes, filename: str) -> Any:
401
+ """POST an upload body with an idle limit: `timeout` per block sent, UPLOAD_SENT_WAIT_FACTOR x
402
+ `timeout` for the answer once all of it is sent. Same answers and 429 retries as _send."""
403
+ attempt = 0
404
+ while True:
405
+ parts = urlsplit(url)
406
+ conn_cls = http.client.HTTPSConnection if parts.scheme == "https" else http.client.HTTPConnection
407
+ conn = conn_cls(parts.hostname, parts.port, timeout=self.timeout)
408
+ path = (parts.path or "/") + ("?" + parts.query if parts.query else "")
409
+ sent = False
410
+ try:
411
+ conn.putrequest("POST", path)
412
+ for k, v in headers.items():
413
+ conn.putheader(k, v)
414
+ conn.putheader("Content-Length", str(len(payload)))
415
+ conn.endheaders()
416
+ view = memoryview(payload)
417
+ for i in range(0, len(payload), UPLOAD_BLOCK_BYTES):
418
+ conn.send(view[i:i + UPLOAD_BLOCK_BYTES])
419
+ sent = True
420
+ if conn.sock is not None and self.timeout:
421
+ conn.sock.settimeout(self.timeout * UPLOAD_SENT_WAIT_FACTOR)
422
+ resp = conn.getresponse()
423
+ status, retry_after, content = resp.status, resp.getheader("Retry-After"), resp.read()
424
+ except (socket.timeout, TimeoutError) as err:
425
+ wait = self.timeout * (UPLOAD_SENT_WAIT_FACTOR if sent else 1)
426
+ raise SkapiError(
427
+ f'Upload timeout: no answer for "{filename}" {wait:g} seconds after it was sent.' if sent
428
+ else f'Upload timeout: "{filename}" sent nothing for {wait:g} seconds.', "TIMEOUT", err) from None
429
+ except (OSError, http.client.HTTPException) as err:
430
+ raise SkapiError("Network error", "NETWORK_ERROR", err) from None
431
+ finally:
432
+ conn.close()
433
+ text = content.decode("utf-8", errors="replace")
434
+ if 200 <= status < 300:
435
+ try:
436
+ return json.loads(text)
437
+ except ValueError:
438
+ return text
439
+ if status == 429:
440
+ delay = _retry_delay_ms(retry_after)
441
+ if delay is None:
442
+ raise SkapiError("Too many requests", "ERROR") from None
443
+ if attempt >= RATE_LIMIT_MAX_RETRIES:
444
+ raise SkapiError(f'Too many requests: the upload of "{filename}" still answered 429 after {RATE_LIMIT_MAX_RETRIES} retries.',
445
+ "TOO_MANY_REQUESTS") from None
446
+ time.sleep(_rate_limit_wait_ms(delay, attempt) / 1000)
447
+ attempt += 1
448
+ continue
449
+ raise parse_error(status, text)
@@ -29,7 +29,8 @@ class Skapi(EncryptionMixin, DatabaseMixin, UserMixin, SocialMixin, ForwardMixin
29
29
  storage: where the signed-in session is kept between runs. None (default) keeps it in memory for
30
30
  this object only; a file path keeps it in that JSON file (created with mode 600); or any
31
31
  object with get_item(key), set_item(key, value) and remove_item(key).
32
- timeout: seconds to wait for each request.
32
+ timeout: seconds a request may go with nothing sent or received before it fails with TIMEOUT
33
+ (default 120). An upload may then wait 5 x timeout for its answer.
33
34
  encryption: True (or options) seals the data of private records in this client before upload.
34
35
  Needs the "cryptography" package: pip install "skapi[encryption]".
35
36
  """
@@ -1,11 +1,11 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: skapi
3
- Version: 2.4.0
3
+ Version: 2.4.4
4
4
  Summary: Serverless backend for Python: sign-in, users and a database, with the Skapi API.
5
5
  Author: broadway Inc.
6
6
  License: Apache-2.0
7
7
  Project-URL: Homepage, https://www.skapi.com
8
- Project-URL: Documentation, https://docs.skapi.com
8
+ Project-URL: Documentation, https://py.skapi.com
9
9
  Keywords: backend,skapi,api,serverless,database,authentication
10
10
  Classifier: Programming Language :: Python :: 3
11
11
  Classifier: License :: OSI Approved :: Apache Software License
@@ -19,9 +19,9 @@ Provides-Extra: encryption
19
19
  Requires-Dist: cryptography>=3.4; extra == "encryption"
20
20
  Dynamic: license-file
21
21
 
22
- # skapi
22
+ # Skapi for Python
23
23
 
24
- The [Skapi](https://www.skapi.com) backend for Python: sign users in, keep their sessions, and read and write your project's database. Same API as [skapi-js](https://www.npmjs.com/package/skapi-js), in Python style.
24
+ The [Skapi](https://www.skapi.com) backend for Python: sign users in, keep their sessions, and read and write your project's database.
25
25
 
26
26
  ```
27
27
  pip install skapi
@@ -44,3 +44,5 @@ posts = skapi.get_records({"table": {"name": "posts", "access_group": "public"}}
44
44
  - Optional encryption of private records before upload, compatible with skapi-js: `pip install "skapi[encryption]"`.
45
45
 
46
46
  Documentation: https://py.skapi.com
47
+
48
+ PyPi: https://pypi.org/project/skapi/
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes