-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoffline_handlers.py
More file actions
55 lines (40 loc) · 1.77 KB
/
offline_handlers.py
File metadata and controls
55 lines (40 loc) · 1.77 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
import json
from pathlib import Path
from typing import Protocol
from flagsmith.mappers import map_environment_document_to_context
from flagsmith.types import SDKEvaluationContext
class OfflineHandler(Protocol):
def get_evaluation_context(self) -> SDKEvaluationContext: ...
class EvaluationContextLocalFileHandler:
"""
Handler to load evaluation context from a local JSON file.
The JSON file should contain the full evaluation context as per Flagsmith Engine's specification.
JSON schema:
https://raw.githubusercontent.com/Flagsmith/flagsmith/main/sdk/evaluation-context.json
"""
def __init__(self, file_path: str) -> None:
self.evaluation_context: SDKEvaluationContext = json.loads(
Path(file_path).read_text(),
)
def get_evaluation_context(self) -> SDKEvaluationContext:
return self.evaluation_context
class EnvironmentDocumentLocalFileHandler:
"""
Handler to load evaluation context from a local JSON file containing the environment document.
The JSON file should contain the environment document as returned by the Flagsmith API.
API documentation:
https://api.flagsmith.com/api/v1/docs/#/api/api_v1_environment-document_list
"""
def __init__(self, file_path: str) -> None:
self.evaluation_context: SDKEvaluationContext = (
map_environment_document_to_context(
json.loads(
Path(file_path).read_text(),
),
)
)
def get_evaluation_context(self) -> SDKEvaluationContext:
return self.evaluation_context
# For backward compatibility, use the old class name for
# the local file handler implementation dependant on the environment document.
LocalFileHandler = EnvironmentDocumentLocalFileHandler