-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoffline_handlers.py
More file actions
36 lines (26 loc) · 1.17 KB
/
offline_handlers.py
File metadata and controls
36 lines (26 loc) · 1.17 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
import json
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Protocol
from flagsmith.api.types import EnvironmentModel
from flagsmith.mappers import map_environment_document_to_context
class OfflineHandler(Protocol):
def get_environment(self) -> EnvironmentModel: ...
class BaseOfflineHandler(ABC):
@abstractmethod
def get_environment(self) -> EnvironmentModel:
raise NotImplementedError()
class LocalFileHandler:
"""
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:
environment_document = json.loads(Path(file_path).read_text())
# Make sure the document can be used for evaluation
map_environment_document_to_context(environment_document)
self.environment_document: EnvironmentModel = environment_document
def get_environment(self) -> EnvironmentModel:
return self.environment_document