17
17
"""
18
18
19
19
import json
20
- from typing import Dict , Optional
20
+ from typing import Any , Dict , Optional
21
+ import requests
21
22
from firebase_admin import App , _http_client , _utils
22
23
import firebase_admin
23
24
@@ -31,12 +32,41 @@ def __init__(self, etag, template_data):
31
32
Args:
32
33
etag: The string to be used for initialize the ETag property.
33
34
template_data: The data to be parsed for getting the parameters and conditions.
35
+
36
+ Raises:
37
+ ValueError: If the template data is not valid.
34
38
"""
35
- self ._parameters = template_data ['parameters' ]
36
- self ._conditions = template_data ['conditions' ]
37
- self ._version = template_data ['version' ]
38
- self ._parameter_groups = template_data ['parameterGroups' ]
39
- self ._etag = etag
39
+ if 'parameters' in template_data :
40
+ if template_data ['parameters' ] is not None :
41
+ self ._parameters = template_data ['parameters' ]
42
+ else :
43
+ raise ValueError ('Remote Config parameters must be a non-null object' )
44
+ else :
45
+ self ._parameters = {}
46
+
47
+ if 'conditions' in template_data :
48
+ if template_data ['conditions' ] is not None :
49
+ self ._conditions = template_data ['conditions' ]
50
+ else :
51
+ raise ValueError ('Remote Config conditions must be a non-null object' )
52
+ else :
53
+ self ._conditions = []
54
+
55
+ self ._version = ''
56
+ if 'version' in template_data :
57
+ self ._version = template_data ['version' ]
58
+
59
+ if 'parameterGroups' in template_data :
60
+ if template_data ['parameterGroups' ] is not None :
61
+ self ._parameter_groups = template_data ['parameterGroups' ]
62
+ else :
63
+ raise ValueError ('Remote Config parameterGroups must be a non-null object' )
64
+ else :
65
+ self .parameter_groups = {}
66
+
67
+ self ._etag = ''
68
+ if etag is not None and isinstance (etag , str ):
69
+ self ._etag = etag
40
70
41
71
@property
42
72
def parameters (self ):
@@ -90,14 +120,13 @@ def evaluate(self, context):
90
120
self ._evaluator = _ConditionEvaluator (self ._cache .conditions , context )
91
121
return ServerConfig (config_values = self ._evaluator .evaluate ())
92
122
93
- def set (self , template ):
123
+ def set (self , template : ServerTemplateData ):
94
124
"""Updates the cache to store the given template is of type ServerTemplateData.
95
125
96
126
Args:
97
127
template: An object of type ServerTemplateData to be cached.
98
128
"""
99
- if isinstance (template , ServerTemplateData ):
100
- self ._cache = template
129
+ self ._cache = template
101
130
102
131
103
132
class ServerConfig :
@@ -140,21 +169,27 @@ def __init__(self, app):
140
169
base_url = remote_config_base_url ,
141
170
headers = rc_headers , timeout = timeout )
142
171
143
-
144
172
def get_server_template (self ):
145
173
"""Requests for a server template and converts the response to an instance of
146
174
ServerTemplateData for storing the template parameters and conditions."""
147
175
url_prefix = self ._get_url_prefix ()
148
- headers , response_json = self ._client .headers_and_body ('get' ,
149
- url = url_prefix + '/namespaces/ \
150
- firebase-server/serverRemoteConfig' )
151
- return ServerTemplateData (headers .get ('ETag' ), response_json )
176
+ try :
177
+ headers , response_json = self ._client .headers_and_body (
178
+ 'get' , url = url_prefix + '/namespaces/firebase-server/serverRemoteConfig' )
179
+ except requests .exceptions .RequestException as error :
180
+ raise self ._handle_remote_config_error (error )
181
+ else :
182
+ return ServerTemplateData (headers .get ('etag' ), response_json )
152
183
153
184
def _get_url_prefix (self ):
154
- # Returns project prefix for url, in the format of
155
- # /v1/projects/${projectId}
185
+ """Returns project prefix for url, in the format of /v1/projects/${projectId}"""
156
186
return "/v1/projects/{0}" .format (self ._project_id )
157
187
188
+ @classmethod
189
+ def _handle_remote_config_error (cls , error : Any ):
190
+ """Handles errors received from the Cloud Functions API."""
191
+ return _utils .handle_platform_error_from_requests (error )
192
+
158
193
159
194
class _ConditionEvaluator :
160
195
"""Internal class that facilitates sending requests to the Firebase Remote
0 commit comments