dynatrace-openfeature-provider-python 0.2.0__tar.gz → 0.3.1__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 (23) hide show
  1. dynatrace_openfeature_provider_python-0.3.1/PKG-INFO +95 -0
  2. dynatrace_openfeature_provider_python-0.3.1/README.md +75 -0
  3. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/pyproject.toml +30 -6
  4. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/pyproject.toml.orig +18 -6
  5. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_cdn.py +262 -0
  6. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_cdn_config.py +40 -0
  7. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_polling.py +250 -0
  8. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_retry.py +17 -14
  9. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_sse.py +157 -0
  10. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/_sse_message.py +49 -0
  11. dynatrace_openfeature_provider_python-0.3.1/src/dynatrace/openfeature/provider.py +502 -0
  12. dynatrace_openfeature_provider_python-0.2.0/PKG-INFO +0 -96
  13. dynatrace_openfeature_provider_python-0.2.0/README.md +0 -78
  14. dynatrace_openfeature_provider_python-0.2.0/src/dynatrace/openfeature/_cdn.py +0 -167
  15. dynatrace_openfeature_provider_python-0.2.0/src/dynatrace/openfeature/_polling.py +0 -130
  16. dynatrace_openfeature_provider_python-0.2.0/src/dynatrace/openfeature/provider.py +0 -305
  17. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/LICENSE +0 -0
  18. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/__init__.py +0 -0
  19. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_metrics_aggregator.py +0 -0
  20. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_metrics_flusher.py +0 -0
  21. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/_metrics_hook.py +0 -0
  22. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/options.py +0 -0
  23. {dynatrace_openfeature_provider_python-0.2.0 → dynatrace_openfeature_provider_python-0.3.1}/src/dynatrace/openfeature/py.typed +0 -0
@@ -0,0 +1,95 @@
1
+ Metadata-Version: 2.4
2
+ Name: dynatrace-openfeature-provider-python
3
+ Version: 0.3.1
4
+ Summary: The official Dynatrace OpenFeature provider for Python
5
+ Keywords: openfeature,dynatrace,feature-flags,feature-management
6
+ Author: Dynatrace
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Classifier: Programming Language :: Python
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: License :: OSI Approved :: Apache Software License
12
+ Requires-Dist: openfeature-sdk>=0.8.2
13
+ Requires-Dist: openfeature-flagd-core>=2.0.0,<3.0.0
14
+ Requires-Dist: requests>=2.27.0
15
+ Requires-Dist: urllib3>=2.3.0
16
+ Requires-Dist: launchdarkly-eventsource>=1.7.3,<2.0.0
17
+ Requires-Python: >=3.10
18
+ Project-URL: Homepage, https://github.com/Dynatrace/openfeature-provider-python
19
+ Description-Content-Type: text/markdown
20
+
21
+ # Dynatrace OpenFeature Provider for Python
22
+
23
+ The official [Dynatrace](https://dynatrace.com) [OpenFeature](https://openfeature.dev) provider for Python server-side applications.
24
+
25
+ The provider evaluates flags in-process: on initialization it fetches your flag configuration from the Dynatrace CDN and refreshes it in the background, so evaluations are local and do not make a network call.
26
+
27
+ Changes are picked up through a server-sent events (SSE) stream advertised in the flag configuration: a change notification triggers an immediate re-fetch, and polling every 10 minutes acts as a safety net. While the stream is unavailable or disabled, the provider polls every 10 seconds instead.
28
+
29
+ > **Status:** Early alpha — not yet ready for production use.
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install dynatrace-openfeature-provider-python
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ```python
40
+ from openfeature import api
41
+ from openfeature.evaluation_context import EvaluationContext
42
+
43
+ from dynatrace.openfeature import DynatraceProvider
44
+
45
+ api.set_provider(DynatraceProvider("dt01.server_us_<key>"))
46
+
47
+ client = api.get_client()
48
+ ctx = EvaluationContext(targeting_key="user-123")
49
+ enabled = client.get_boolean_value("my-flag", False, ctx)
50
+
51
+ # On shutdown:
52
+ api.shutdown()
53
+ ```
54
+
55
+ Once the provider is registered, all flag evaluation goes through the OpenFeature API. See the
56
+ [OpenFeature Python SDK documentation](https://openfeature.dev/docs/reference/technologies/server/python/)
57
+ for evaluation contexts, hooks, events, and the rest of the SDK surface.
58
+
59
+ ## Configuration
60
+
61
+ Pass options as keyword arguments to `DynatraceProvider`:
62
+
63
+ ```python
64
+ api.set_provider(
65
+ DynatraceProvider(
66
+ sdk_key,
67
+ config_origin="https://cdn.eu.fm.dynatracelabs.com",
68
+ metrics_origin="https://metrics.eu.fm.dynatracelabs.com",
69
+ )
70
+ )
71
+ ```
72
+
73
+ | Option | Type | Default | Description |
74
+ | --- | --- | --- | --- |
75
+ | `config_origin` | `str` | derived from SDK key | Override the flag configuration origin. |
76
+ | `metrics_origin` | `str` | derived from SDK key | Override the evaluation metrics ingest origin. |
77
+ | `disable_sse` | `bool` | `False` | Don't attempt an SSE connection and poll only, every 10 seconds. |
78
+
79
+ ## Running the e2e tests
80
+
81
+ The shared [provider test harness](https://github.com/Dynatrace/fm-provider-test-harness) gherkin suite runs against its mock server in a container, so Docker is required:
82
+
83
+ ```bash
84
+ uv run poe e2e
85
+ ```
86
+
87
+ This initializes the `fm-provider-test-harness` submodule and runs the suite with pytest-bdd. Set `SKIP_HARNESS_UPDATE=1` to keep a local harness checkout, and `MOCKSERVER_IMAGE` to use a locally built mock server image.
88
+
89
+ ## Contributing
90
+
91
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
92
+
93
+ ## License
94
+
95
+ Apache 2.0 — see [LICENSE](LICENSE).
@@ -0,0 +1,75 @@
1
+ # Dynatrace OpenFeature Provider for Python
2
+
3
+ The official [Dynatrace](https://dynatrace.com) [OpenFeature](https://openfeature.dev) provider for Python server-side applications.
4
+
5
+ The provider evaluates flags in-process: on initialization it fetches your flag configuration from the Dynatrace CDN and refreshes it in the background, so evaluations are local and do not make a network call.
6
+
7
+ Changes are picked up through a server-sent events (SSE) stream advertised in the flag configuration: a change notification triggers an immediate re-fetch, and polling every 10 minutes acts as a safety net. While the stream is unavailable or disabled, the provider polls every 10 seconds instead.
8
+
9
+ > **Status:** Early alpha — not yet ready for production use.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ pip install dynatrace-openfeature-provider-python
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ```python
20
+ from openfeature import api
21
+ from openfeature.evaluation_context import EvaluationContext
22
+
23
+ from dynatrace.openfeature import DynatraceProvider
24
+
25
+ api.set_provider(DynatraceProvider("dt01.server_us_<key>"))
26
+
27
+ client = api.get_client()
28
+ ctx = EvaluationContext(targeting_key="user-123")
29
+ enabled = client.get_boolean_value("my-flag", False, ctx)
30
+
31
+ # On shutdown:
32
+ api.shutdown()
33
+ ```
34
+
35
+ Once the provider is registered, all flag evaluation goes through the OpenFeature API. See the
36
+ [OpenFeature Python SDK documentation](https://openfeature.dev/docs/reference/technologies/server/python/)
37
+ for evaluation contexts, hooks, events, and the rest of the SDK surface.
38
+
39
+ ## Configuration
40
+
41
+ Pass options as keyword arguments to `DynatraceProvider`:
42
+
43
+ ```python
44
+ api.set_provider(
45
+ DynatraceProvider(
46
+ sdk_key,
47
+ config_origin="https://cdn.eu.fm.dynatracelabs.com",
48
+ metrics_origin="https://metrics.eu.fm.dynatracelabs.com",
49
+ )
50
+ )
51
+ ```
52
+
53
+ | Option | Type | Default | Description |
54
+ | --- | --- | --- | --- |
55
+ | `config_origin` | `str` | derived from SDK key | Override the flag configuration origin. |
56
+ | `metrics_origin` | `str` | derived from SDK key | Override the evaluation metrics ingest origin. |
57
+ | `disable_sse` | `bool` | `False` | Don't attempt an SSE connection and poll only, every 10 seconds. |
58
+
59
+ ## Running the e2e tests
60
+
61
+ The shared [provider test harness](https://github.com/Dynatrace/fm-provider-test-harness) gherkin suite runs against its mock server in a container, so Docker is required:
62
+
63
+ ```bash
64
+ uv run poe e2e
65
+ ```
66
+
67
+ This initializes the `fm-provider-test-harness` submodule and runs the suite with pytest-bdd. Set `SKIP_HARNESS_UPDATE=1` to keep a local harness checkout, and `MOCKSERVER_IMAGE` to use a locally built mock server image.
68
+
69
+ ## Contributing
70
+
71
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
72
+
73
+ ## License
74
+
75
+ Apache 2.0 — see [LICENSE](LICENSE).
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "dynatrace-openfeature-provider-python"
7
- version = "0.2.0"
7
+ version = "0.3.1"
8
8
  description = "The official Dynatrace OpenFeature provider for Python"
9
9
  readme = "README.md"
10
10
  license = "Apache-2.0"
@@ -22,8 +22,10 @@ keywords = [
22
22
  ]
23
23
  dependencies = [
24
24
  "openfeature-sdk>=0.8.2",
25
- "openfeature-flagd-core>=1.0.0,<2.0.0",
25
+ "openfeature-flagd-core>=2.0.0,<3.0.0",
26
26
  "requests>=2.27.0",
27
+ "urllib3>=2.3.0",
28
+ "launchdarkly-eventsource>=1.7.3,<2.0.0",
27
29
  ]
28
30
  requires-python = ">=3.10"
29
31
 
@@ -43,6 +45,8 @@ dev = [
43
45
  "pre-commit>=4.0.0",
44
46
  "ruff>=0.15.13",
45
47
  "types-requests>=2.32.0,<3.0.0",
48
+ "pytest-bdd>=8.1.0,<9.0.0",
49
+ "testcontainers>=4.13.0,<5.0.0",
46
50
  ]
47
51
 
48
52
  [tool.uv.build-backend]
@@ -54,8 +58,14 @@ namespace = true
54
58
  omit = ["tests/**"]
55
59
 
56
60
  [tool.mypy]
57
- mypy_path = "src"
58
- files = "src"
61
+ mypy_path = [
62
+ "src",
63
+ "tests/e2e",
64
+ ]
65
+ files = [
66
+ "src",
67
+ "tests/e2e",
68
+ ]
59
69
  python_version = "3.10"
60
70
  namespace_packages = true
61
71
  explicit_package_bases = true
@@ -66,6 +76,13 @@ pretty = true
66
76
  strict = true
67
77
  disallow_any_generics = false
68
78
 
79
+ [[tool.mypy.overrides]]
80
+ module = [
81
+ "ld_eventsource",
82
+ "ld_eventsource.*",
83
+ ]
84
+ ignore_missing_imports = true
85
+
69
86
  [tool.ruff]
70
87
  target-version = "py310"
71
88
  exclude = [
@@ -147,8 +164,8 @@ keep-runtime-typing = true
147
164
  strict = true
148
165
 
149
166
  [tool.poe.tasks]
150
- test = "pytest tests"
151
- test-cov = "coverage run -m pytest tests"
167
+ test = "pytest tests --ignore=tests/e2e"
168
+ test-cov = "coverage run -m pytest tests --ignore=tests/e2e"
152
169
  cov-report = "coverage xml"
153
170
  cov = [
154
171
  "test-cov",
@@ -157,3 +174,10 @@ cov = [
157
174
  mypy = "mypy"
158
175
  lint = "ruff check src tests"
159
176
  fmt = "ruff format src tests"
177
+
178
+ [tool.poe.tasks.e2e]
179
+ sequence = [
180
+ { shell = '[ -n "$SKIP_HARNESS_UPDATE" ] || git submodule update --init fm-provider-test-harness' },
181
+ "pytest tests/e2e",
182
+ ]
183
+ default_item_type = "cmd"
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "dynatrace-openfeature-provider-python"
7
- version = "0.2.0"
7
+ version = "0.3.1"
8
8
  description = "The official Dynatrace OpenFeature provider for Python"
9
9
  readme = "README.md"
10
10
  authors = [{ name = "Dynatrace" }]
@@ -18,8 +18,12 @@ classifiers = [
18
18
  keywords = ["openfeature", "dynatrace", "feature-flags", "feature-management"]
19
19
  dependencies = [
20
20
  "openfeature-sdk>=0.8.2",
21
- "openfeature-flagd-core>=1.0.0,<2.0.0",
21
+ "openfeature-flagd-core>=2.0.0,<3.0.0",
22
22
  "requests>=2.27.0",
23
+ # 2.3 added HTTPResponse.shutdown, which _cdn uses to enforce the fetch deadline
24
+ "urllib3>=2.3.0",
25
+ # SSE client (ld_eventsource); the floor is the release _sse was built and verified against
26
+ "launchdarkly-eventsource>=1.7.3,<2.0.0",
23
27
  ]
24
28
  requires-python = ">=3.10"
25
29
 
@@ -36,6 +40,8 @@ dev = [
36
40
  "pre-commit>=4.0.0",
37
41
  "ruff>=0.15.13",
38
42
  "types-requests>=2.32.0,<3.0.0",
43
+ "pytest-bdd>=8.1.0,<9.0.0",
44
+ "testcontainers>=4.13.0,<5.0.0",
39
45
  ]
40
46
 
41
47
  [tool.uv.build-backend]
@@ -47,8 +53,8 @@ namespace = true
47
53
  omit = ["tests/**"]
48
54
 
49
55
  [tool.mypy]
50
- mypy_path = "src"
51
- files = "src"
56
+ mypy_path = ["src", "tests/e2e"]
57
+ files = ["src", "tests/e2e"]
52
58
  python_version = "3.10"
53
59
  namespace_packages = true
54
60
  explicit_package_bases = true
@@ -59,6 +65,11 @@ pretty = true
59
65
  strict = true
60
66
  disallow_any_generics = false
61
67
 
68
+ [[tool.mypy.overrides]]
69
+ # ld_eventsource ships no type stubs or py.typed marker
70
+ module = ["ld_eventsource", "ld_eventsource.*"]
71
+ ignore_missing_imports = true
72
+
62
73
  [tool.ruff]
63
74
  target-version = "py310"
64
75
  exclude = [".git", ".venv", "__pycache__", "venv"]
@@ -100,10 +111,11 @@ keep-runtime-typing = true
100
111
  strict = true
101
112
 
102
113
  [tool.poe.tasks]
103
- test = "pytest tests"
104
- test-cov = "coverage run -m pytest tests"
114
+ test = "pytest tests --ignore=tests/e2e"
115
+ test-cov = "coverage run -m pytest tests --ignore=tests/e2e"
105
116
  cov-report = "coverage xml"
106
117
  cov = ["test-cov", "cov-report"]
107
118
  mypy = "mypy"
108
119
  lint = "ruff check src tests"
109
120
  fmt = "ruff format src tests"
121
+ e2e = { sequence = [{ shell = "[ -n \"$SKIP_HARNESS_UPDATE\" ] || git submodule update --init fm-provider-test-harness" }, "pytest tests/e2e"], default_item_type = "cmd" }
@@ -0,0 +1,262 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import threading
5
+ import time
6
+ from collections.abc import Callable
7
+ from dataclasses import dataclass
8
+ from datetime import datetime
9
+ from email.utils import parsedate_to_datetime
10
+
11
+ import requests
12
+ from urllib3.util import Timeout as Urllib3Timeout
13
+
14
+ from ._retry import (
15
+ MAX_ATTEMPTS,
16
+ MAX_BACKOFF_S,
17
+ RetryStrategy,
18
+ default_should_retry_status,
19
+ )
20
+ from .options import DynatraceProviderOptions
21
+
22
+ log = logging.getLogger(__name__)
23
+
24
+ REQUEST_TIMEOUT_S: float = 4.0
25
+
26
+ #: worst-case wall time of a fetch (all attempts + backoff); stays under the shortest poll interval so two polls never overlap (providers.md 2.1); asserted in DynatraceProvider
27
+ MAX_FETCH_DURATION_S: float = REQUEST_TIMEOUT_S * MAX_ATTEMPTS + MAX_BACKOFF_S
28
+
29
+ _DEFAULT_RETRY_AFTER_MS: float = 60_000.0
30
+ _BODY_CHUNK_BYTES = 64 * 1024
31
+ _BUDGET_EXCEEDED = "response body exceeded the fetch budget"
32
+
33
+
34
+ @dataclass
35
+ class FetchResult:
36
+ status: int
37
+ body: str | None
38
+ etag: str | None
39
+ last_modified: str | None
40
+ skipped: bool = False
41
+
42
+ @property
43
+ def is_not_modified(self) -> bool:
44
+ return self.status == 304
45
+
46
+ @property
47
+ def is_rate_limited(self) -> bool:
48
+ return self.status == 429
49
+
50
+ @property
51
+ def is_success(self) -> bool:
52
+ return 200 <= self.status < 300
53
+
54
+ @property
55
+ def last_modified_epoch_s(self) -> float | None:
56
+ parsed = _parse_http_date(self.last_modified)
57
+ return parsed.timestamp() if parsed is not None else None
58
+
59
+
60
+ @dataclass
61
+ class _CacheState:
62
+ etag: str | None = None
63
+ last_modified_header: str | None = None
64
+ last_modified_at: datetime | None = None
65
+
66
+
67
+ def _should_retry_fetch(result: FetchResult) -> bool:
68
+ return default_should_retry_status(result.status)
69
+
70
+
71
+ class CdnFetcher:
72
+ def __init__(
73
+ self,
74
+ options: DynatraceProviderOptions,
75
+ retry_strategy: RetryStrategy | None = None,
76
+ *,
77
+ clock: Callable[[], float] | None = None,
78
+ ) -> None:
79
+ self._url = options.cdn_url
80
+ self._retry = retry_strategy or RetryStrategy()
81
+ self._session = requests.Session()
82
+ self._clock: Callable[[], float] = clock or time.time
83
+ self._cache = _CacheState()
84
+ self._pending_cache: _CacheState | None = None
85
+ self._rate_limited_until: float = 0.0
86
+ self._fetch_lock = threading.RLock()
87
+
88
+ def is_rate_limited(self, now: float | None = None) -> bool:
89
+ t = now if now is not None else self._clock()
90
+ return t < self._rate_limited_until
91
+
92
+ def is_newer_than_committed(self, last_modified_epoch_s: float) -> bool:
93
+ """Whether a Last-Modified is newer than the config in force; true if that config is undated."""
94
+ committed = self._cache.last_modified_at
95
+ return committed is None or last_modified_epoch_s > committed.timestamp()
96
+
97
+ def commit(self) -> None:
98
+ """Promote the validators staged by the last successful fetch.
99
+
100
+ Call this only once the body has been applied. Until then the previous validators stay in
101
+ force, so a body that is empty or fails to apply is re-requested in full instead of being
102
+ answered with a 304.
103
+ """
104
+ pending = self._pending_cache
105
+ if pending is not None:
106
+ self._cache = pending
107
+ self._pending_cache = None
108
+
109
+ def invalidate_cache(self) -> None:
110
+ """Forget the validators, so the next fetch is unconditional."""
111
+ self._cache = _CacheState()
112
+ self._pending_cache = None
113
+
114
+ def fetch(self, cancel: threading.Event | None = None) -> FetchResult:
115
+ """Fetch the config, or skip without making a request.
116
+
117
+ A skip (fetch in flight, section 2.1.1; or an open Retry-After window,
118
+ section 2.3) is neither success nor failure and must not be reported as
119
+ either. PollingManager serialises its own fetches, so the in-flight
120
+ guard is for callers outside it -- initialize() fetches on the caller's
121
+ thread.
122
+ """
123
+ if self.is_rate_limited():
124
+ log.debug("rate limited; skipping fetch")
125
+ return FetchResult(0, None, None, None, skipped=True)
126
+ if not self._fetch_lock.acquire(blocking=False):
127
+ log.debug("fetch already in progress; skipping")
128
+ return FetchResult(0, None, None, None, skipped=True)
129
+ try:
130
+ return self._retry.execute(
131
+ self._do_fetch,
132
+ should_retry=_should_retry_fetch,
133
+ retryable_exceptions=(OSError, requests.RequestException),
134
+ cancel=cancel,
135
+ )
136
+ finally:
137
+ self._fetch_lock.release()
138
+
139
+ def _do_fetch(self) -> FetchResult:
140
+ self._pending_cache = None
141
+ cache = self._cache
142
+ headers: dict[str, str] = {}
143
+ if cache.etag is not None:
144
+ headers["If-None-Match"] = cache.etag
145
+ if cache.last_modified_header is not None:
146
+ headers["If-Modified-Since"] = cache.last_modified_header
147
+
148
+ deadline = time.monotonic() + REQUEST_TIMEOUT_S
149
+ # total bounds connect + time-to-first-byte as one budget (a tuple would give each phase the full timeout)
150
+ timeout = Urllib3Timeout(
151
+ connect=REQUEST_TIMEOUT_S,
152
+ read=REQUEST_TIMEOUT_S,
153
+ total=REQUEST_TIMEOUT_S,
154
+ )
155
+ with self._session.get(
156
+ self._url,
157
+ headers=headers,
158
+ timeout=timeout, # type: ignore[arg-type]
159
+ # each hop gets a fresh timeout, so following a chain would blow the budget (Java never follows one either)
160
+ allow_redirects=False,
161
+ stream=True,
162
+ ) as resp:
163
+ if resp.status_code == 304:
164
+ return FetchResult(304, None, cache.etag, cache.last_modified_header)
165
+
166
+ if resp.status_code == 429:
167
+ retry_after = resp.headers.get("Retry-After")
168
+ delay_ms = _parse_retry_after_ms(retry_after, self._clock())
169
+ self._rate_limited_until = self._clock() + delay_ms / 1000.0
170
+ log.warning("rate limited (429); pausing fetches for %.0fms", delay_ms)
171
+ return FetchResult(429, None, None, None)
172
+
173
+ new_etag = resp.headers.get("ETag")
174
+ new_last_modified = resp.headers.get("Last-Modified")
175
+ body = _read_body_within(resp, deadline)
176
+
177
+ if resp.status_code >= 200 and resp.status_code < 300:
178
+ new_lm_at = _parse_http_date(new_last_modified)
179
+ if (
180
+ new_lm_at is not None
181
+ and cache.last_modified_at is not None
182
+ and not new_lm_at > cache.last_modified_at
183
+ ):
184
+ log.warning(
185
+ "CDN returned 200 but Last-Modified (%s) is not later than stored (%s);"
186
+ " treating as 304 (suspected stale node)",
187
+ new_last_modified,
188
+ cache.last_modified_header,
189
+ )
190
+ return FetchResult(304, None, cache.etag, cache.last_modified_header)
191
+ self._pending_cache = _CacheState(
192
+ etag=new_etag,
193
+ last_modified_header=new_last_modified,
194
+ last_modified_at=new_lm_at,
195
+ )
196
+
197
+ return FetchResult(resp.status_code, body, new_etag, new_last_modified)
198
+
199
+
200
+ def _read_body_within(resp: requests.Response, deadline: float) -> str:
201
+ """Read the response body, giving up once ``deadline`` passes.
202
+
203
+ No timeout in the stack bounds this: they all measure inactivity between
204
+ socket reads, so a server trickling bytes resets the clock on every one and
205
+ MAX_FETCH_DURATION_S stops being a bound. Checking the clock between chunks
206
+ is not enough either, since a read blocks until its chunk is full, so the
207
+ deadline interrupts the read itself via HTTPResponse.shutdown.
208
+ """
209
+ raw = resp.raw
210
+ expired = threading.Event()
211
+
212
+ def _abort() -> None:
213
+ expired.set()
214
+ try:
215
+ raw.shutdown()
216
+ except Exception: # connection already released, or nothing to interrupt
217
+ log.debug("could not shut down the response socket", exc_info=True)
218
+
219
+ watchdog = threading.Timer(max(0.0, deadline - time.monotonic()), _abort)
220
+ watchdog.daemon = True
221
+ watchdog.start()
222
+ chunks: list[bytes] = []
223
+ try:
224
+ chunks.extend(resp.iter_content(_BODY_CHUNK_BYTES))
225
+ except (requests.RequestException, OSError):
226
+ # A torn-down socket surfaces as a read error.
227
+ if expired.is_set():
228
+ raise requests.Timeout(_BUDGET_EXCEEDED) from None
229
+ raise
230
+ finally:
231
+ watchdog.cancel()
232
+
233
+ if expired.is_set():
234
+ # Arrived, just not in time.
235
+ raise requests.Timeout(_BUDGET_EXCEEDED)
236
+
237
+ return b"".join(chunks).decode(resp.encoding or "utf-8", errors="replace")
238
+
239
+
240
+ def _parse_http_date(value: str | None) -> datetime | None:
241
+ if not value:
242
+ return None
243
+ try:
244
+ return parsedate_to_datetime(value.strip())
245
+ except Exception:
246
+ log.warning("unparseable HTTP-date %r", value)
247
+ return None
248
+
249
+
250
+ def _parse_retry_after_ms(retry_after: str | None, now: float) -> float:
251
+ if not retry_after or not retry_after.strip():
252
+ return _DEFAULT_RETRY_AFTER_MS
253
+ try:
254
+ return float(retry_after.strip()) * 1000.0
255
+ except ValueError:
256
+ pass
257
+ try:
258
+ dt = parsedate_to_datetime(retry_after.strip())
259
+ return max((dt.timestamp() - now) * 1000.0, 0.0)
260
+ except Exception:
261
+ log.warning("unparseable Retry-After %r; using default", retry_after)
262
+ return _DEFAULT_RETRY_AFTER_MS
@@ -0,0 +1,40 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import typing
5
+
6
+ #: ``eventStreams[].type`` value identifying the SSE change-notification stream
7
+ STREAM_TYPE_SSE = "sse"
8
+
9
+
10
+ def _non_blank(value: typing.Any) -> str | None:
11
+ return value if isinstance(value, str) and value.strip() else None
12
+
13
+
14
+ def extract_sse_url(config_json: str | None, fallback_origin: str | None) -> str | None:
15
+ """Return the SSE URL of the first ``sse`` entry in ``eventStreams``.
16
+ A non-blank ``url`` wins; else ``endpoint.requestUri`` on ``endpoint.origin`` or ``fallback_origin``."""
17
+ if not config_json or not config_json.strip():
18
+ return None
19
+ try:
20
+ config: typing.Any = json.loads(config_json)
21
+ except ValueError:
22
+ return None
23
+ streams = config.get("eventStreams") if isinstance(config, dict) else None
24
+ if not isinstance(streams, list):
25
+ return None
26
+ for stream in streams:
27
+ if not isinstance(stream, dict) or stream.get("type") != STREAM_TYPE_SSE:
28
+ continue
29
+ url = _non_blank(stream.get("url"))
30
+ if url is not None:
31
+ return url
32
+ endpoint = stream.get("endpoint")
33
+ if not isinstance(endpoint, dict):
34
+ return None
35
+ request_uri = _non_blank(endpoint.get("requestUri"))
36
+ if request_uri is None:
37
+ return None
38
+ origin = _non_blank(endpoint.get("origin")) or _non_blank(fallback_origin)
39
+ return origin + request_uri if origin is not None else None
40
+ return None