Skip to content

Commit 7dbed2c

Browse files
author
Pijush Chakraborty
committed
Adding Implementation for RemoteConfigApiClient and ServerTemplate APIs
1 parent 43b6920 commit 7dbed2c

File tree

2 files changed

+87
-17
lines changed

2 files changed

+87
-17
lines changed

firebase_admin/_http_client.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,3 @@ def __init__(self, **kwargs):
148148

149149
def parse_body(self, resp):
150150
return resp.json()
151-
152-
153-
class RemoteConfigApiClient(HttpClient):
154-
"""An HTTP client that parses response messages as JSON."""
155-
156-
def __init__(self, **kwargs):
157-
HttpClient.__init__(self, **kwargs)
158-
159-
def parse_body(self, resp):
160-
return resp.json()

firebase_admin/remote_config.py

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from typing import Dict, Optional
2020
from firebase_admin import _http_client
21+
import firebase_admin
2122

2223
class RemoteConfig:
2324
"""Represents a Server Side Remote Config Class.
@@ -26,27 +27,73 @@ class RemoteConfig:
2627
"""
2728

2829
def __init__(self, app=None):
29-
timeout = app.options.get('httpTimeout', _http_client.DEFAULT_TIMEOUT_SECONDS)
3030
self._credential = app.credential.get_credential()
31-
self._api_client = _http_client.RemoteConfigApiClient(
32-
credential=self._credential, timeout=timeout)
31+
self._api_client = _RemoteConfigApiClient(app=app)
3332

3433
async def get_server_template(self, default_config: Optional[Dict[str, str]] = None):
3534
template = self.init_server_template(default_config)
3635
await template.load()
3736
return template
3837

3938
def init_server_template(self, default_config: Optional[Dict[str, str]] = None):
40-
template = ServerTemplate(self._api_client, default_config=default_config)
39+
template = ServerTemplate(client=self._api_client, default_config=default_config)
4140
# Logic to handle setting template_data here
4241
return template
4342

4443

4544
class ServerTemplateData:
4645
"""Represents a Server Template Data class.
4746
"""
48-
def __init__(self, template):
49-
self._template = template
47+
def __init__(self, resp):
48+
self._parameters = ...
49+
# Convert response['parameters'] to {string : Parameter}
50+
self._version = resp.body.version
51+
self._etag = resp.headers.get('ETag')
52+
53+
@property
54+
def parameters(self):
55+
return self._parameters
56+
57+
@property
58+
def etag(self):
59+
return self._etag
60+
61+
@property
62+
def version(self):
63+
return self._version
64+
65+
class Parameter:
66+
""" Representation of a remote config parameter."""
67+
68+
def __init__(self, default_value):
69+
self._default_value = default_value # ParameterValue
70+
71+
@property
72+
def default_value(self):
73+
return self._default_value
74+
75+
76+
class ParameterValue:
77+
""" Base class to represent remote parameter values. A
78+
ParameterValue could be either an ExplicitParameterValue or an
79+
InAppDefaultValue. """
80+
81+
82+
class ExplicitParameterValue(ParameterValue):
83+
def __init__(self, value):
84+
self._value = value
85+
86+
@property
87+
def value(self):
88+
return self._value
89+
90+
class InAppDefaultValue(ParameterValue):
91+
def __init__(self, use_in_app_default):
92+
self._use_in_app_default = use_in_app_default
93+
94+
@property
95+
def use_in_app_default(self):
96+
return self._use_in_app_default
5097

5198

5299
class ServerTemplate:
@@ -66,7 +113,7 @@ async def load(self):
66113

67114
def evaluate(self, context: Optional[Dict[str, str | int]]):
68115
# Logic to process the cached template into a ServerConfig here
69-
return ServerConfig(context.values)
116+
return ServerConfig(config_values=context.values)
70117

71118
def set(self, template):
72119
if isinstance(template, str):
@@ -92,3 +139,36 @@ def get_int(self, key):
92139

93140
def get_value(self, key):
94141
return self._config_values[key]
142+
143+
class _RemoteConfigApiClient:
144+
""" Internal class that facilitates sending requests to the Firebase Remote
145+
Config backend API. """
146+
147+
def __init__(self, app):
148+
# Initialize a JsonHttpClient with basic inputs. Referenced other
149+
# products' code in the Python SDK for what basic inputs to use.
150+
remote_config_base_url = 'https://firebaseremoteconfig.googleapis.com'
151+
self._project_id = app.project_id
152+
app_credential = app.credential.get_credential()
153+
rc_headers = {
154+
'X-FIREBASE-CLIENT': 'fire-admin-python/{0}'.format(firebase_admin.__version__), }
155+
timeout = app.options.get('httpTimeout', _http_client.DEFAULT_TIMEOUT_SECONDS)
156+
157+
self._client = _http_client.JsonHttpClient(credential=app_credential,
158+
base_url=remote_config_base_url,
159+
headers=rc_headers, timeout=timeout)
160+
161+
162+
def get_server_template(self):
163+
# Requests for server template and converts the response to
164+
# ServerTemplateData
165+
url_prefix = self._get_url_prefix()
166+
response_json = self._client.body('get',
167+
url=url_prefix+'/namespaces/ \
168+
firebase-server/serverRemoteConfig')
169+
return ServerTemplateData(response_json)
170+
171+
def _get_url_prefix(self):
172+
# Returns project prefix for url, in the format of
173+
# /v1/projects/${projectId}
174+
return "/v1/projects/{0}".format(self._project_id)

0 commit comments

Comments
 (0)