@@ -26,13 +26,16 @@ class ServerTemplateData:
26
26
"""Represents a Server Template Data class.
27
27
"""
28
28
def __init__ (self , resp ):
29
+
29
30
self ._parameters = resp .body .parameters
30
31
self ._conditions = resp .body .conditions
31
32
self ._version = resp .body .version
32
- self ._etag = resp .headers .get ('ETag' )
33
+ self ._parameterGroups = resp .body .parameterGroups
34
+ self ._etag = resp .headers .get ('etag' )
33
35
34
36
@property
35
37
def parameters (self ):
38
+ # TODO: convert to Parameters
36
39
return self ._parameters
37
40
38
41
@property
@@ -46,6 +49,10 @@ def version(self):
46
49
@property
47
50
def conditions (self ):
48
51
return self ._conditions
52
+
53
+ @property
54
+ def conditions (self ):
55
+ return self ._parameterGroups
49
56
50
57
class Parameter :
51
58
""" Representation of a remote config parameter."""
@@ -84,22 +91,26 @@ def use_in_app_default(self):
84
91
class ServerTemplate :
85
92
"""Represents a Server Template with implementations for loading and evaluting the tempalte.
86
93
"""
87
- def __init__ (self , app : App , default_config : Optional [Dict [str , str ]] = None ):
94
+ def __init__ (self , app : App = None , default_config : Optional [Dict [str , str ]] = None ):
88
95
self ._rc_service = _utils .get_app_service (app , _REMOTE_CONFIG_ATTRIBUTE , _RemoteConfigService )
89
96
90
97
# Field to represent the cached template. This gets set when the template is
91
98
# fetched from RC servers via the load API, or via the set API.
92
99
self ._cache = None
93
- for key in default_config :
94
- self ._stringified_default_config [key ] = default_config [key ]
100
+ if default_config is not None :
101
+ for key in default_config :
102
+ self ._stringified_default_config [key ] = default_config [key ]
103
+ else :
104
+ self ._stringified_default_config [key ] = None
95
105
96
106
async def load (self ):
97
107
self ._cache = await self ._rc_service .getServerTemplate ()
98
108
99
109
def evaluate (self , context : Optional [Dict [str , str | int ]]):
100
110
# Logic to process the cached template into a ServerConfig here
101
111
# TODO: add Condition evaluator
102
- return ServerConfig (config_values = context .values )
112
+ self ._evaluator = ConditionEvaluator (self ._cache .conditions , context )
113
+ return ServerConfig (config_values = self ._evaluator .evaluate ())
103
114
104
115
def set (self , template ):
105
116
if isinstance (template , str ):
@@ -115,13 +126,13 @@ def __init__(self, config_values):
115
126
self ._config_values = config_values # dictionary of param key to values
116
127
117
128
def get_boolean (self , key ):
118
- return self ._config_values [ key ]
129
+ return bool ( self .get_value ( key ))
119
130
120
131
def get_string (self , key ):
121
- return self ._config_values [ key ]
132
+ return str ( self .get_value ( key ))
122
133
123
134
def get_int (self , key ):
124
- return self ._config_values [ key ]
135
+ return int ( self .get_value ( key ))
125
136
126
137
def get_value (self , key ):
127
138
return self ._config_values [key ]
@@ -160,13 +171,27 @@ def _get_url_prefix(self):
160
171
return "/v1/projects/{0}" .format (self ._project_id )
161
172
162
173
163
- async def get_server_template (app : App , default_config : Optional [Dict [str , str ]] = None ):
174
+ class _ConditionEvaluator :
175
+ """ Internal class that facilitates sending requests to the Firebase Remote
176
+ Config backend API. """
177
+
178
+ def __init__ (self , context , conditions ):
179
+ self ._context = context
180
+ self ._conditions = conditions
181
+
182
+ def evaluate (self ):
183
+ # TODO: Write evaluator
184
+ return {}
185
+
186
+
187
+ async def get_server_template (app : App = None , default_config : Optional [Dict [str , str ]] = None ):
164
188
template = init_server_template (app , default_config )
165
189
await template .load ()
166
190
return template
167
191
168
- def init_server_template (app : App , default_config : Optional [Dict [str , str ]] = None ,
192
+ def init_server_template (app : App = None , default_config : Optional [Dict [str , str ]] = None ,
169
193
template_data : Optional [ServerTemplateData ] = None ):
170
194
template = ServerTemplate (app , default_config = default_config )
171
- template .set (template_data )
195
+ if template_data is not None :
196
+ template .set (template_data )
172
197
return template
0 commit comments