|
1 | 1 | """
|
2 | 2 | This module houses FDv2 types and implementations of synchronizers and
|
3 | 3 | initializers for the datasystem.
|
| 4 | +
|
| 5 | +All types and implementations in this module are considered internal |
| 6 | +and are not part of the public API of the LaunchDarkly Python SDK. |
| 7 | +They are subject to change without notice and should not be used directly |
| 8 | +by users of the SDK. |
| 9 | +
|
| 10 | +You have been warned. |
4 | 11 | """
|
5 | 12 |
|
| 13 | +from abc import abstractmethod |
| 14 | +from dataclasses import dataclass |
| 15 | +from typing import Iterable, Mapping, Optional, Protocol, Tuple |
| 16 | + |
| 17 | +from ldclient.impl.datasystem.protocolv2 import ChangeSet, Selector |
| 18 | +from ldclient.impl.util import _Result |
| 19 | +from ldclient.interfaces import DataSourceErrorInfo, DataSourceState |
| 20 | + |
| 21 | +PollingResult = _Result[Tuple[ChangeSet, Mapping], str] |
| 22 | + |
| 23 | + |
| 24 | +class PollingRequester(Protocol): # pylint: disable=too-few-public-methods |
| 25 | + """ |
| 26 | + PollingRequester allows PollingDataSource to delegate fetching data to |
| 27 | + another component. |
| 28 | +
|
| 29 | + This is useful for testing the PollingDataSource without needing to set up |
| 30 | + a test HTTP server. |
| 31 | + """ |
| 32 | + |
| 33 | + @abstractmethod |
| 34 | + def fetch(self, selector: Optional[Selector]) -> PollingResult: |
| 35 | + """ |
| 36 | + Fetches the data for the given selector. |
| 37 | + Returns a Result containing a tuple of ChangeSet and any request headers, |
| 38 | + or an error if the data could not be retrieved. |
| 39 | + """ |
| 40 | + raise NotImplementedError |
| 41 | + |
| 42 | + |
| 43 | +@dataclass(frozen=True) |
| 44 | +class Update: |
| 45 | + """ |
| 46 | + Update represents the results of a synchronizer's ongoing sync |
| 47 | + method. |
| 48 | + """ |
| 49 | + |
| 50 | + state: DataSourceState |
| 51 | + change_set: Optional[ChangeSet] = None |
| 52 | + error: Optional[DataSourceErrorInfo] = None |
| 53 | + revert_to_fdv1: bool = False |
| 54 | + environment_id: Optional[str] = None |
| 55 | + |
| 56 | + |
| 57 | +class Synchronizer(Protocol): # pylint: disable=too-few-public-methods |
| 58 | + """ |
| 59 | + Synchronizer represents a component capable of synchronizing data from an external |
| 60 | + data source, such as a streaming or polling API. |
| 61 | +
|
| 62 | + It is responsible for yielding Update objects that represent the current state |
| 63 | + of the data source, including any changes that have occurred since the last |
| 64 | + synchronization. |
| 65 | + """ |
| 66 | + |
| 67 | + @abstractmethod |
| 68 | + def sync(self) -> Iterable[Update]: |
| 69 | + """ |
| 70 | + sync should begin the synchronization process for the data source, yielding |
| 71 | + Update objects until the connection is closed or an unrecoverable error |
| 72 | + occurs. |
| 73 | + """ |
| 74 | + raise NotImplementedError |
| 75 | + |
| 76 | + |
6 | 77 | __all__: list[str] = []
|
0 commit comments