|
14 | 14 | import urllib3 |
15 | 15 |
|
16 | 16 | from ldclient.config import Config |
| 17 | +from ldclient.impl.datasource.feature_requester import LATEST_ALL_URI |
17 | 18 | from ldclient.impl.datasystem import BasisResult, SelectorStore, Update |
18 | 19 | from ldclient.impl.datasystem.protocolv2 import ( |
19 | 20 | Basis, |
|
22 | 23 | DeleteObject, |
23 | 24 | EventName, |
24 | 25 | IntentCode, |
| 26 | + ObjectKind, |
| 27 | + Payload, |
25 | 28 | PutObject, |
26 | 29 | Selector, |
27 | 30 | ServerIntent |
|
43 | 46 | DataSourceErrorKind, |
44 | 47 | DataSourceState |
45 | 48 | ) |
| 49 | +from ldclient.versioned_data_kind import FEATURES, SEGMENTS |
46 | 50 |
|
47 | 51 | POLLING_ENDPOINT = "/sdk/poll" |
48 | 52 |
|
@@ -123,6 +127,15 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]: |
123 | 127 | ), |
124 | 128 | ) |
125 | 129 |
|
| 130 | + fallback = result.exception.headers.get("X-LD-FD-Fallback") == 'true' |
| 131 | + if fallback: |
| 132 | + yield Update( |
| 133 | + state=DataSourceState.OFF, |
| 134 | + error=error_info, |
| 135 | + revert_to_fdv1=True |
| 136 | + ) |
| 137 | + break |
| 138 | + |
126 | 139 | status_code = result.exception.status |
127 | 140 | if is_http_error_recoverable(status_code): |
128 | 141 | # TODO(fdv2): Add support for environment ID |
@@ -158,6 +171,7 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]: |
158 | 171 | state=DataSourceState.VALID, |
159 | 172 | change_set=change_set, |
160 | 173 | environment_id=headers.get("X-LD-EnvID"), |
| 174 | + revert_to_fdv1=headers.get('X-LD-FD-Fallback') == 'true' |
161 | 175 | ) |
162 | 176 |
|
163 | 177 | if self._event.wait(self._poll_interval): |
@@ -262,7 +276,7 @@ def fetch(self, selector: Optional[Selector]) -> PollingResult: |
262 | 276 |
|
263 | 277 | if response.status >= 400: |
264 | 278 | return _Fail( |
265 | | - f"HTTP error {response}", UnsuccessfulResponseException(response.status) |
| 279 | + f"HTTP error {response}", UnsuccessfulResponseException(response.status, response.headers) |
266 | 280 | ) |
267 | 281 |
|
268 | 282 | headers = response.headers |
@@ -375,3 +389,118 @@ def build(self) -> PollingDataSource: |
375 | 389 | return PollingDataSource( |
376 | 390 | poll_interval=self._config.poll_interval, requester=requester |
377 | 391 | ) |
| 392 | + |
| 393 | + |
| 394 | +# pylint: disable=too-few-public-methods |
| 395 | +class Urllib3FDv1PollingRequester: |
| 396 | + """ |
| 397 | + Urllib3PollingRequesterFDv1 is a Requester that uses urllib3 to make HTTP |
| 398 | + requests. |
| 399 | + """ |
| 400 | + |
| 401 | + def __init__(self, config: Config): |
| 402 | + self._etag = None |
| 403 | + self._http = _http_factory(config).create_pool_manager(1, config.base_uri) |
| 404 | + self._config = config |
| 405 | + self._poll_uri = config.base_uri + LATEST_ALL_URI |
| 406 | + |
| 407 | + def fetch(self, selector: Optional[Selector]) -> PollingResult: |
| 408 | + """ |
| 409 | + Fetches the data for the given selector. |
| 410 | + Returns a Result containing a tuple of ChangeSet and any request headers, |
| 411 | + or an error if the data could not be retrieved. |
| 412 | + """ |
| 413 | + query_params = {} |
| 414 | + if self._config.payload_filter_key is not None: |
| 415 | + query_params["filter"] = self._config.payload_filter_key |
| 416 | + |
| 417 | + uri = self._poll_uri |
| 418 | + if len(query_params) > 0: |
| 419 | + filter_query = parse.urlencode(query_params) |
| 420 | + uri += f"?{filter_query}" |
| 421 | + |
| 422 | + hdrs = _headers(self._config) |
| 423 | + hdrs["Accept-Encoding"] = "gzip" |
| 424 | + |
| 425 | + if self._etag is not None: |
| 426 | + hdrs["If-None-Match"] = self._etag |
| 427 | + |
| 428 | + response = self._http.request( |
| 429 | + "GET", |
| 430 | + uri, |
| 431 | + headers=hdrs, |
| 432 | + timeout=urllib3.Timeout( |
| 433 | + connect=self._config.http.connect_timeout, |
| 434 | + read=self._config.http.read_timeout, |
| 435 | + ), |
| 436 | + retries=1, |
| 437 | + ) |
| 438 | + |
| 439 | + if response.status >= 400: |
| 440 | + return _Fail( |
| 441 | + f"HTTP error {response}", UnsuccessfulResponseException(response.status, response.headers) |
| 442 | + ) |
| 443 | + |
| 444 | + headers = response.headers |
| 445 | + |
| 446 | + if response.status == 304: |
| 447 | + return _Success(value=(ChangeSetBuilder.no_changes(), headers)) |
| 448 | + |
| 449 | + data = json.loads(response.data.decode("UTF-8")) |
| 450 | + etag = headers.get("ETag") |
| 451 | + |
| 452 | + if etag is not None: |
| 453 | + self._etag = etag |
| 454 | + |
| 455 | + log.debug( |
| 456 | + "%s response status:[%d] ETag:[%s]", |
| 457 | + uri, |
| 458 | + response.status, |
| 459 | + etag, |
| 460 | + ) |
| 461 | + |
| 462 | + changeset_result = fdv1_polling_payload_to_changeset(data) |
| 463 | + if isinstance(changeset_result, _Success): |
| 464 | + return _Success(value=(changeset_result.value, headers)) |
| 465 | + |
| 466 | + return _Fail( |
| 467 | + error=changeset_result.error, |
| 468 | + exception=changeset_result.exception, |
| 469 | + ) |
| 470 | + |
| 471 | + |
| 472 | +# pylint: disable=too-many-branches,too-many-return-statements |
| 473 | +def fdv1_polling_payload_to_changeset(data: dict) -> _Result[ChangeSet, str]: |
| 474 | + """ |
| 475 | + Converts a fdv1 polling payload into a ChangeSet. |
| 476 | + """ |
| 477 | + builder = ChangeSetBuilder() |
| 478 | + builder.start(IntentCode.TRANSFER_FULL) |
| 479 | + selector = Selector.no_selector() |
| 480 | + |
| 481 | + # FDv1 uses "flags" instead of "features", so we need to map accordingly |
| 482 | + # Map FDv1 JSON keys to ObjectKind enum values |
| 483 | + kind_mappings = [ |
| 484 | + (ObjectKind.FLAG, "flags"), |
| 485 | + (ObjectKind.SEGMENT, "segments") |
| 486 | + ] |
| 487 | + |
| 488 | + for kind, fdv1_key in kind_mappings: |
| 489 | + kind_data = data.get(fdv1_key) |
| 490 | + if kind_data is None: |
| 491 | + continue |
| 492 | + if not isinstance(kind_data, dict): |
| 493 | + return _Fail(error=f"Invalid format: {fdv1_key} is not a dictionary") |
| 494 | + |
| 495 | + for key in kind_data: |
| 496 | + flag_or_segment = kind_data.get(key) |
| 497 | + if flag_or_segment is None or not isinstance(flag_or_segment, dict): |
| 498 | + return _Fail(error=f"Invalid format: {key} is not a dictionary") |
| 499 | + |
| 500 | + version = flag_or_segment.get('version') |
| 501 | + if version is None: |
| 502 | + return _Fail(error=f"Invalid format: {key} does not have a version set") |
| 503 | + |
| 504 | + builder.add_put(kind, key, version, flag_or_segment) |
| 505 | + |
| 506 | + return _Success(builder.finish(selector)) |
0 commit comments