-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathclient_entity.py
More file actions
371 lines (301 loc) · 15.1 KB
/
client_entity.py
File metadata and controls
371 lines (301 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import json
import logging
import sys
from typing import Any, Callable
from urllib.parse import urlparse
import requests
from big_segment_store_fixture import BigSegmentStoreFixture
from hook import PostingHook
from ldclient import *
from ldclient import (
Context,
ExecutionOrder,
MigratorBuilder,
MigratorFn,
Operation,
Stage
)
from ldclient.config import BigSegmentsConfig
from ldclient.datasystem import (
custom,
fdv1_fallback_ds_builder,
polling_ds_builder,
streaming_ds_builder
)
from ldclient.feature_store import CacheConfig
from ldclient.impl.datasourcev2.polling import PollingDataSourceBuilder
from ldclient.integrations import Consul, DynamoDB, Redis
from ldclient.interfaces import DataStoreMode
class ClientEntity:
def __init__(self, tag, config):
self.log = logging.getLogger(tag)
opts = {"sdk_key": config["credential"]}
tags = config.get('tags', {})
if tags:
opts['application'] = {
'id': tags.get('applicationId', ''),
'version': tags.get('applicationVersion', ''),
}
datasystem_config = config.get('dataSystem')
if datasystem_config is not None:
datasystem = custom()
init_configs = datasystem_config.get('initializers')
if init_configs is not None:
initializers = []
for init_config in init_configs:
polling = init_config.get('polling')
if polling is not None:
polling_builder = polling_ds_builder()
_set_optional_value(polling, "baseUri", polling_builder.base_uri)
_set_optional_time(polling, "pollIntervalMs", polling_builder.poll_interval)
initializers.append(polling_builder)
datasystem.initializers(initializers)
sync_configs = datasystem_config.get('synchronizers')
if sync_configs is not None:
sync_builders = []
fallback_builder = None
for sync_config in sync_configs:
streaming = sync_config.get('streaming')
if streaming is not None:
builder = streaming_ds_builder()
_set_optional_value(streaming, "baseUri", builder.base_uri)
_set_optional_time(streaming, "initialRetryDelayMs", builder.initial_reconnect_delay)
sync_builders.append(builder)
elif sync_config.get('polling') is not None:
polling = sync_config.get('polling')
builder = polling_ds_builder()
_set_optional_value(polling, "baseUri", builder.base_uri)
_set_optional_time(polling, "pollIntervalMs", builder.poll_interval)
sync_builders.append(builder)
fallback_builder = fdv1_fallback_ds_builder()
_set_optional_value(polling, "baseUri", fallback_builder.base_uri)
_set_optional_time(polling, "pollIntervalMs", fallback_builder.poll_interval)
if sync_builders:
datasystem.synchronizers(*sync_builders)
if fallback_builder is not None:
datasystem.fdv1_compatible_synchronizer(fallback_builder)
if datasystem_config.get("payloadFilter") is not None:
opts["payload_filter_key"] = datasystem_config["payloadFilter"]
# Handle persistent data store configuration for dataSystem
store_config = datasystem_config.get("store")
if store_config is not None:
persistent_store_config = store_config.get("persistentDataStore")
if persistent_store_config is not None:
store = _create_persistent_store(persistent_store_config)
# Parse store mode (0 = READ_ONLY, 1 = READ_WRITE)
store_mode_value = datasystem_config.get("storeMode", 0)
store_mode = DataStoreMode.READ_WRITE if store_mode_value == 1 else DataStoreMode.READ_ONLY
datasystem.data_store(store, store_mode)
opts["datasystem_config"] = datasystem.build()
elif config.get("streaming") is not None:
streaming = config["streaming"]
if streaming.get("baseUri") is not None:
opts["stream_uri"] = streaming["baseUri"]
if streaming.get("filter") is not None:
opts["payload_filter_key"] = streaming["filter"]
_set_optional_time_prop(streaming, "initialRetryDelayMs", opts, "initial_reconnect_delay")
elif config.get("polling") is not None:
opts['stream'] = False
polling = config["polling"]
if polling.get("baseUri") is not None:
opts["base_uri"] = polling["baseUri"]
if polling.get("filter") is not None:
opts["payload_filter_key"] = polling["filter"]
_set_optional_time_prop(polling, "pollIntervalMs", opts, "poll_interval")
else:
opts['use_ldd'] = True
if config.get("events") is not None:
events = config["events"]
opts["enable_event_compression"] = events.get("enableGzip", False)
if events.get("baseUri") is not None:
opts["events_uri"] = events["baseUri"]
if events.get("capacity") is not None:
opts["events_max_pending"] = events["capacity"]
opts["diagnostic_opt_out"] = not events.get("enableDiagnostics", False)
opts["all_attributes_private"] = events.get("allAttributesPrivate", False)
opts["private_attributes"] = events.get("globalPrivateAttributes", {})
_set_optional_time_prop(events, "flushIntervalMs", opts, "flush_interval")
opts["omit_anonymous_contexts"] = events.get("omitAnonymousContexts", False)
else:
opts["send_events"] = False
if config.get("hooks") is not None:
opts["hooks"] = [PostingHook(h["name"], h["callbackUri"], h.get("data", {}), h.get("errors", {})) for h in config["hooks"]["hooks"]]
if config.get("bigSegments") is not None:
big_params = config["bigSegments"]
big_config = {"store": BigSegmentStoreFixture(big_params["callbackUri"])}
if big_params.get("userCacheSize") is not None:
big_config["context_cache_size"] = big_params["userCacheSize"]
_set_optional_time_prop(big_params, "userCacheTimeMs", big_config, "context_cache_time")
_set_optional_time_prop(big_params, "statusPollIntervalMs", big_config, "status_poll_interval")
_set_optional_time_prop(big_params, "staleAfterMs", big_config, "stale_after")
opts["big_segments"] = BigSegmentsConfig(**big_config)
if config.get("persistentDataStore") is not None:
opts["feature_store"] = _create_persistent_store(config["persistentDataStore"])
start_wait = config.get("startWaitTimeMs") or 5000
config = Config(**opts)
self.client = client.LDClient(config, start_wait / 1000.0)
def is_initializing(self) -> bool:
return self.client.is_initialized()
def evaluate(self, params: dict) -> dict:
response = {}
if params.get("detail", False):
detail = self.client.variation_detail(params["flagKey"], Context.from_dict(params["context"]), params["defaultValue"])
response["value"] = detail.value
response["variationIndex"] = detail.variation_index
response["reason"] = detail.reason
else:
response["value"] = self.client.variation(params["flagKey"], Context.from_dict(params["context"]), params["defaultValue"])
return response
def evaluate_all(self, params: dict):
opts = {}
opts["client_side_only"] = params.get("clientSideOnly", False)
opts["with_reasons"] = params.get("withReasons", False)
opts["details_only_for_tracked_flags"] = params.get("detailsOnlyForTrackedFlags", False)
state = self.client.all_flags_state(Context.from_dict(params["context"]), **opts)
return {"state": state.to_json_dict()}
def track(self, params: dict):
self.client.track(params["eventKey"], Context.from_dict(params["context"]), params["data"], params.get("metricValue", None))
def identify(self, params: dict):
self.client.identify(Context.from_dict(params["context"]))
def flush(self):
self.client.flush()
def secure_mode_hash(self, params: dict) -> dict:
return {"result": self.client.secure_mode_hash(Context.from_dict(params["context"]))}
def context_build(self, params: dict) -> dict:
if params.get("multi"):
b = Context.multi_builder()
for c in params.get("multi"):
b.add(self._context_build_single(c))
return self._context_response(b.build())
return self._context_response(self._context_build_single(params["single"]))
def _context_build_single(self, params: dict) -> Context:
b = Context.builder(params["key"])
if "kind" in params:
b.kind(params["kind"])
if "name" in params:
b.name(params["name"])
if "anonymous" in params:
b.anonymous(params["anonymous"])
if "custom" in params:
for k, v in params.get("custom").items():
b.set(k, v)
if "private" in params:
for attr in params.get("private"):
b.private(attr)
return b.build()
def context_convert(self, params: dict) -> dict:
input = params["input"]
try:
props = json.loads(input)
return self._context_response(Context.from_dict(props))
except Exception as e:
return {"error": str(e)}
def _context_response(self, c: Context) -> dict:
if c.valid:
return {"output": c.to_json_string()}
return {"error": c.error}
def get_big_segment_store_status(self) -> dict:
status = self.client.big_segment_store_status_provider.status
return {"available": status.available, "stale": status.stale}
def migration_variation(self, params: dict) -> dict:
stage, _ = self.client.migration_variation(params["key"], Context.from_dict(params["context"]), Stage.from_str(params["defaultStage"]))
return {'result': stage.value}
def migration_operation(self, params: dict) -> dict:
builder = MigratorBuilder(self.client)
if params["readExecutionOrder"] == "concurrent":
params["readExecutionOrder"] = "parallel"
builder.read_execution_order(ExecutionOrder.from_str(params["readExecutionOrder"]))
builder.track_latency(params["trackLatency"])
builder.track_errors(params["trackErrors"])
def callback(endpoint) -> MigratorFn:
def fn(payload) -> Result:
response = requests.post(endpoint, data=payload)
if response.status_code == 200:
return Result.success(response.text)
return Result.error(f"Request failed with status code {response.status_code}")
return fn
if params["trackConsistency"]:
builder.read(callback(params["oldEndpoint"]), callback(params["newEndpoint"]), lambda lhs, rhs: lhs == rhs)
else:
builder.read(callback(params["oldEndpoint"]), callback(params["newEndpoint"]))
builder.write(callback(params["oldEndpoint"]), callback(params["newEndpoint"]))
migrator = builder.build()
if isinstance(migrator, str):
return {"result": migrator}
if params["operation"] == Operation.READ.value:
result = migrator.read(params["key"], Context.from_dict(params["context"]), Stage.from_str(params["defaultStage"]), params["payload"])
return {"result": result.value if result.is_success() else result.error}
result = migrator.write(params["key"], Context.from_dict(params["context"]), Stage.from_str(params["defaultStage"]), params["payload"])
return {"result": result.authoritative.value if result.authoritative.is_success() else result.authoritative.error}
def close(self):
self.client.close()
self.log.info('Test ended')
def _set_optional_time_prop(params_in: dict, name_in: str, params_out: dict, name_out: str):
if params_in.get(name_in) is not None:
params_out[name_out] = params_in[name_in] / 1000.0
def _set_optional_time(params_in: dict, name_in: str, func: Callable[[float], Any]):
if params_in.get(name_in) is not None:
func(params_in[name_in] / 1000.0)
def _set_optional_value(params_in: dict, name_in: str, func: Callable[[Any], Any]):
if params_in.get(name_in) is not None:
func(params_in[name_in])
def _create_persistent_store(persistent_store_config: dict):
"""
Creates a persistent store instance based on the configuration.
Used for both v2 and v3 (dataSystem) configurations.
"""
store_params = persistent_store_config["store"]
store_type = store_params["type"]
dsn = store_params["dsn"]
prefix = store_params.get("prefix")
# Parse cache configuration
cache_config = persistent_store_config.get("cache", {})
cache_mode = cache_config.get("mode", "ttl")
if cache_mode == "off":
caching = CacheConfig.disabled()
elif cache_mode == "infinite":
caching = CacheConfig(expiration=sys.maxsize)
elif cache_mode == "ttl":
ttl_seconds = cache_config.get("ttl", 15)
caching = CacheConfig(expiration=ttl_seconds)
else:
caching = CacheConfig.default()
# Create the appropriate store based on type
if store_type == "redis":
return Redis.new_feature_store(
url=dsn,
prefix=prefix or Redis.DEFAULT_PREFIX,
caching=caching
)
elif store_type == "dynamodb":
# Parse endpoint from DSN (handle URLs without scheme)
parsed = urlparse(dsn) if '://' in dsn else urlparse(f'http://{dsn}')
endpoint_url = f"{parsed.scheme}://{parsed.netloc}"
# Import boto3 for DynamoDB configuration
import boto3
# Create DynamoDB client with test credentials
dynamodb_opts = {
'endpoint_url': endpoint_url,
'region_name': 'us-east-1',
'aws_access_key_id': 'dummy',
'aws_secret_access_key': 'dummy'
}
return DynamoDB.new_feature_store(
table_name="sdk-contract-tests",
prefix=prefix,
dynamodb_opts=dynamodb_opts,
caching=caching
)
elif store_type == "consul":
# Parse host and port from DSN
parsed = urlparse(dsn) if '://' in dsn else urlparse(f'http://{dsn}')
host = parsed.hostname or 'localhost'
port = parsed.port or 8500
return Consul.new_feature_store(
host=host,
port=port,
prefix=prefix,
caching=caching
)
else:
raise ValueError(f"Unsupported data store type: {store_type}")