specstream-client 1.3.1__tar.gz → 1.3.2__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.
Files changed (19) hide show
  1. {specstream_client-1.3.1 → specstream_client-1.3.2}/PKG-INFO +1 -1
  2. {specstream_client-1.3.1 → specstream_client-1.3.2}/pyproject.toml +1 -1
  3. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/__init__.py +1 -1
  4. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/client.py +41 -4
  5. {specstream_client-1.3.1 → specstream_client-1.3.2}/tests/test_http_boundary.py +9 -1
  6. {specstream_client-1.3.1 → specstream_client-1.3.2}/.gitignore +0 -0
  7. {specstream_client-1.3.1 → specstream_client-1.3.2}/LICENSE +0 -0
  8. {specstream_client-1.3.1 → specstream_client-1.3.2}/README.md +0 -0
  9. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/__main__.py +0 -0
  10. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/authorization.py +0 -0
  11. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/cli.py +0 -0
  12. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/families.py +0 -0
  13. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/lifecycle_commands.py +0 -0
  14. {specstream_client-1.3.1 → specstream_client-1.3.2}/specstream_client/py.typed +0 -0
  15. {specstream_client-1.3.1 → specstream_client-1.3.2}/tests/__init__.py +0 -0
  16. {specstream_client-1.3.1 → specstream_client-1.3.2}/tests/test_cli.py +0 -0
  17. {specstream_client-1.3.1 → specstream_client-1.3.2}/tests/test_client.py +0 -0
  18. {specstream_client-1.3.1 → specstream_client-1.3.2}/tests/test_convergence.py +0 -0
  19. {specstream_client-1.3.1 → specstream_client-1.3.2}/tests/test_m16_exports.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: specstream-client
3
- Version: 1.3.1
3
+ Version: 1.3.2
4
4
  Summary: Python client for building lead lists and company lists from the live web. Every match comes with its source, priced before it runs.
5
5
  Project-URL: Homepage, https://specstream.ai
6
6
  Project-URL: Documentation, https://specstream.ai/docs/sdk
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "specstream-client"
3
- version = "1.3.1"
3
+ version = "1.3.2"
4
4
  description = "Python client for building lead lists and company lists from the live web. Every match comes with its source, priced before it runs."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -11,4 +11,4 @@ __all__ = [
11
11
  "SpecstreamError",
12
12
  "assert_family",
13
13
  ]
14
- __version__ = "1.3.0"
14
+ __version__ = "1.3.2"
@@ -4,9 +4,12 @@ from __future__ import annotations
4
4
 
5
5
  import json
6
6
  import re
7
+ import time
7
8
  import urllib.error
8
9
  import urllib.request
9
10
  from collections.abc import Callable
11
+ from datetime import UTC, datetime
12
+ from email.utils import parsedate_to_datetime
10
13
  from typing import Any, cast
11
14
  from urllib.parse import quote as quote_path
12
15
  from urllib.parse import urlencode, urljoin, urlsplit
@@ -15,7 +18,7 @@ from specstream_client.authorization import require_work_authorization
15
18
  from specstream_client.families import FAMILIES
16
19
 
17
20
  DEFAULT_BASE_URL = "https://api.specstream.ai"
18
- USER_AGENT = "specstream-python/1.3.0"
21
+ USER_AGENT = "specstream-python/1.3.2"
19
22
 
20
23
  OpenFn = Callable[[urllib.request.Request, float], Any]
21
24
 
@@ -27,6 +30,29 @@ class _NoRedirect(urllib.request.HTTPRedirectHandler):
27
30
  return None
28
31
 
29
32
 
33
+ RETRY_AFTER_MAX_S = 10.0
34
+
35
+
36
+ def _retry_after(exc: urllib.error.HTTPError) -> float | None:
37
+ """Seconds to wait before the one retry of a 503 that names its Retry-After, or None."""
38
+ if exc.code != 503 or exc.headers is None:
39
+ return None
40
+ value = (exc.headers.get("Retry-After") or "").strip()
41
+ try:
42
+ seconds = float(value)
43
+ except ValueError:
44
+ try:
45
+ when = parsedate_to_datetime(value)
46
+ except (TypeError, ValueError):
47
+ return None
48
+ if when.tzinfo is None:
49
+ return None
50
+ seconds = (when - datetime.now(UTC)).total_seconds()
51
+ if not 0 <= seconds <= RETRY_AFTER_MAX_S:
52
+ return None
53
+ return seconds
54
+
55
+
30
56
  class SpecstreamError(RuntimeError):
31
57
  def __init__(self, message: str, status: int | None = None, body: Any = None) -> None:
32
58
  super().__init__(message)
@@ -1595,9 +1621,20 @@ class Client:
1595
1621
  payload = None if body is None else json.dumps(body, separators=(",", ":")).encode("utf-8")
1596
1622
  req = urllib.request.Request(url, data=payload, method=method, headers=self._headers())
1597
1623
  try:
1598
- with self._opener(req, self.timeout) as resp:
1599
- status = getattr(resp, "status", 200)
1600
- text = resp.read().decode("utf-8", "replace")
1624
+ try:
1625
+ with self._opener(req, self.timeout) as resp:
1626
+ status = getattr(resp, "status", 200)
1627
+ text = resp.read().decode("utf-8", "replace")
1628
+ except urllib.error.HTTPError as busy:
1629
+ wait = _retry_after(busy)
1630
+ if wait is None:
1631
+ raise
1632
+ # The server did nothing and names when to ask again: ask once more.
1633
+ busy.close()
1634
+ time.sleep(wait)
1635
+ with self._opener(req, self.timeout) as resp:
1636
+ status = getattr(resp, "status", 200)
1637
+ text = resp.read().decode("utf-8", "replace")
1601
1638
  except urllib.error.HTTPError as exc:
1602
1639
  text = exc.read().decode("utf-8", "replace") if exc.fp else ""
1603
1640
  try:
@@ -29,7 +29,12 @@ def endpoint():
29
29
  status, body = cases.get(
30
30
  self.path.rsplit("/", 1)[-1], (200, '{"data":{"status":"completed"}}')
31
31
  )
32
+ busy = self.path.endswith("/busy") and seen.count(self.path) == 1
33
+ if busy:
34
+ status, body = 503, '{"error":{"code":"capacity"}}'
32
35
  self.send_response(status)
36
+ if busy:
37
+ self.send_header("Retry-After", "0")
33
38
  self.end_headers()
34
39
  self.wfile.write(body.encode())
35
40
 
@@ -48,7 +53,7 @@ def endpoint():
48
53
 
49
54
 
50
55
  def test_error_envelopes_and_malformed_successes_are_not_deliveries(endpoint):
51
- origin, _ = endpoint
56
+ origin, seen = endpoint
52
57
  client = Client("offline", base_url=origin)
53
58
  for case in ("error", "null", "array", "empty"):
54
59
  with pytest.raises(SpecstreamError) as failed:
@@ -59,6 +64,9 @@ def test_error_envelopes_and_malformed_successes_are_not_deliveries(endpoint):
59
64
  assert unavailable.value.status == 503
60
65
  assert unavailable.value.body == "<html>unavailable</html>"
61
66
  assert client.get_find("valid")["status"] == "completed"
67
+ assert client.get_find("busy")["status"] == "completed"
68
+ assert seen.count("/v1/finds/html") == 1
69
+ assert seen.count("/v1/finds/busy") == 2
62
70
 
63
71
 
64
72
  def test_redirect_does_not_forward_account_authority(endpoint):