-
Notifications
You must be signed in to change notification settings - Fork 336
Implementation for Fetching and Caching Server Side Remote Config #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
43b6920
Initial Skeleton for SSRC Implementation
7dbed2c
Adding Implementation for RemoteConfigApiClient and ServerTemplate APIs
dc1d17b
Updating API signature
7dc118e
Minor update to API signature
ffe47d4
Adding comments and unit tests
b6acf10
Updating init params for ServerTemplateData
f5db7db
Adding validation errors and test
20749d7
Adding unit tests for init_server_template and get_server_template
681d353
Removing parameter groups
cde8b09
Addressing PR comments and fixing async flow during fetch call
b4b0aab
Fixing lint issues
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
# Copyright 2017 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Firebase Remote Config Module. | ||
This module has required APIs for the clients to use Firebase Remote Config with python. | ||
""" | ||
|
||
import json | ||
from typing import Any, Dict, Optional | ||
import requests | ||
from firebase_admin import App, _http_client, _utils | ||
import firebase_admin | ||
|
||
_REMOTE_CONFIG_ATTRIBUTE = '_remoteconfig' | ||
|
||
class ServerTemplateData: | ||
"""Represents a Server Template Data class.""" | ||
def __init__(self, etag, template_data): | ||
"""Initializes a new ServerTemplateData instance. | ||
|
||
Args: | ||
etag: The string to be used for initialize the ETag property. | ||
template_data: The data to be parsed for getting the parameters and conditions. | ||
|
||
Raises: | ||
ValueError: If the template data is not valid. | ||
""" | ||
if 'parameters' in template_data: | ||
if template_data['parameters'] is not None: | ||
self._parameters = template_data['parameters'] | ||
else: | ||
raise ValueError('Remote Config parameters must be a non-null object') | ||
else: | ||
self._parameters = {} | ||
|
||
if 'conditions' in template_data: | ||
if template_data['conditions'] is not None: | ||
self._conditions = template_data['conditions'] | ||
else: | ||
raise ValueError('Remote Config conditions must be a non-null object') | ||
else: | ||
self._conditions = [] | ||
|
||
self._version = '' | ||
if 'version' in template_data: | ||
self._version = template_data['version'] | ||
|
||
self._etag = '' | ||
if etag is not None and isinstance(etag, str): | ||
self._etag = etag | ||
|
||
@property | ||
def parameters(self): | ||
return self._parameters | ||
|
||
@property | ||
def etag(self): | ||
return self._etag | ||
|
||
@property | ||
def version(self): | ||
return self._version | ||
|
||
@property | ||
def conditions(self): | ||
return self._conditions | ||
|
||
|
||
class ServerTemplate: | ||
"""Represents a Server Template with implementations for loading and evaluting the tempalte.""" | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def __init__(self, app: App = None, default_config: Optional[Dict[str, str]] = None): | ||
"""Initializes a ServerTemplate instance. | ||
|
||
Args: | ||
app: App instance to be used. This is optional and the default app instance will | ||
be used if not present. | ||
default_config: The default config to be used in the evaluated config. | ||
""" | ||
self._rc_service = _utils.get_app_service(app, | ||
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService) | ||
|
||
# This gets set when the template is | ||
# fetched from RC servers via the load API, or via the set API. | ||
self._cache = None | ||
if default_config is not None: | ||
self._stringified_default_config = json.dumps(default_config) | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else: | ||
self._stringified_default_config = None | ||
|
||
async def load(self): | ||
"""Fetches the server template and caches the data.""" | ||
self._cache = self._rc_service.get_server_template() | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def evaluate(self): | ||
# Logic to process the cached template into a ServerConfig here. | ||
# TODO: Add and validate Condition evaluator. | ||
self._evaluator = _ConditionEvaluator(self._cache.parameters) | ||
return ServerConfig(config_values=self._evaluator.evaluate()) | ||
|
||
def set(self, template: ServerTemplateData): | ||
"""Updates the cache to store the given template is of type ServerTemplateData. | ||
|
||
Args: | ||
template: An object of type ServerTemplateData to be cached. | ||
""" | ||
self._cache = template | ||
|
||
|
||
class ServerConfig: | ||
"""Represents a Remote Config Server Side Config.""" | ||
def __init__(self, config_values): | ||
self._config_values = config_values # dictionary of param key to values | ||
|
||
def get_boolean(self, key): | ||
return bool(self.get_value(key)) | ||
|
||
def get_string(self, key): | ||
return str(self.get_value(key)) | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def get_int(self, key): | ||
return int(self.get_value(key)) | ||
|
||
def get_value(self, key): | ||
return self._config_values[key] | ||
|
||
|
||
class _RemoteConfigService: | ||
"""Internal class that facilitates sending requests to the Firebase Remote | ||
Config backend API. | ||
""" | ||
def __init__(self, app): | ||
"""Initialize a JsonHttpClient with necessary inputs. | ||
|
||
Args: | ||
app: App instance to be used for fetching app specific details required | ||
for initializing the http client. | ||
""" | ||
remote_config_base_url = 'https://firebaseremoteconfig.googleapis.com' | ||
self._project_id = app.project_id | ||
app_credential = app.credential.get_credential() | ||
rc_headers = { | ||
'X-FIREBASE-CLIENT': 'fire-admin-python/{0}'.format(firebase_admin.__version__), } | ||
timeout = app.options.get('httpTimeout', _http_client.DEFAULT_TIMEOUT_SECONDS) | ||
|
||
self._client = _http_client.JsonHttpClient(credential=app_credential, | ||
base_url=remote_config_base_url, | ||
headers=rc_headers, timeout=timeout) | ||
|
||
def get_server_template(self): | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Requests for a server template and converts the response to an instance of | ||
ServerTemplateData for storing the template parameters and conditions.""" | ||
url_prefix = self._get_url_prefix() | ||
try: | ||
headers, response_json = self._client.headers_and_body( | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'get', url=url_prefix+'/namespaces/firebase-server/serverRemoteConfig') | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
except requests.exceptions.RequestException as error: | ||
raise self._handle_remote_config_error(error) | ||
else: | ||
return ServerTemplateData(headers.get('etag'), response_json) | ||
|
||
def _get_url_prefix(self): | ||
"""Returns project prefix for url, in the format of /v1/projects/${projectId}""" | ||
return "/v1/projects/{0}".format(self._project_id) | ||
|
||
@classmethod | ||
def _handle_remote_config_error(cls, error: Any): | ||
"""Handles errors received from the Cloud Functions API.""" | ||
return _utils.handle_platform_error_from_requests(error) | ||
|
||
|
||
class _ConditionEvaluator: | ||
"""Internal class that facilitates sending requests to the Firebase Remote | ||
Config backend API.""" | ||
def __init__(self, parameters): | ||
self._parameters = parameters | ||
|
||
def evaluate(self): | ||
# TODO: Write logic for evaluator | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self._parameters | ||
|
||
|
||
async def get_server_template(app: App = None, default_config: Optional[Dict[str, str]] = None): | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Initializes a new ServerTemplate instance and fetches the server template. | ||
|
||
Args: | ||
app: App instance to be used. This is optional and the default app instance will | ||
be used if not present. | ||
default_config: The default config to be used in the evaluated config. | ||
|
||
Returns: | ||
ServerTemplate: An object having the cached server template to be used for evaluation. | ||
""" | ||
template = init_server_template(app=app, default_config=default_config) | ||
await template.load() | ||
return template | ||
|
||
def init_server_template(app: App = None, default_config: Optional[Dict[str, str]] = None, | ||
template_data: Optional[ServerTemplateData] = None): | ||
"""Initializes a new ServerTemplate instance. | ||
|
||
Args: | ||
app: App instance to be used. This is optional and the default app instance will | ||
be used if not present. | ||
default_config: The default config to be used in the evaluated config. | ||
template_data: An optional template data to be set on initialization. | ||
|
||
Returns: | ||
ServerTemplate: A new ServerTemplate instance initialized with an optional | ||
template and config. | ||
""" | ||
template = ServerTemplate(app=app, default_config=default_config) | ||
if template_data is not None: | ||
template.set(template_data) | ||
return template |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
# Copyright 2017 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Tests for firebase_admin.remote_config.""" | ||
import json | ||
import pytest | ||
import firebase_admin | ||
from firebase_admin import remote_config | ||
from firebase_admin.remote_config import _REMOTE_CONFIG_ATTRIBUTE | ||
from firebase_admin.remote_config import _RemoteConfigService, ServerTemplateData | ||
|
||
from firebase_admin import _utils | ||
from tests import testutils | ||
|
||
class MockAdapter(testutils.MockAdapter): | ||
"""A Mock HTTP Adapter that Firebase Remote Config with ETag in header.""" | ||
|
||
ETAG = 'etag' | ||
|
||
def __init__(self, data, status, recorder, etag=ETAG): | ||
testutils.MockAdapter.__init__(self, data, status, recorder) | ||
self._etag = etag | ||
|
||
def send(self, request, **kwargs): | ||
resp = super(MockAdapter, self).send(request, **kwargs) | ||
resp.headers = {'etag': self._etag} | ||
return resp | ||
|
||
|
||
class TestRemoteConfigServiceClient: | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@classmethod | ||
def setup_class(cls): | ||
cred = testutils.MockCredential() | ||
firebase_admin.initialize_app(cred, {'projectId': 'project-id'}) | ||
|
||
@classmethod | ||
def teardown_class(cls): | ||
testutils.cleanup_apps() | ||
|
||
def test_rc_instance_get_server_template(self): | ||
recorder = [] | ||
response = json.dumps({ | ||
'parameters': { | ||
'test_key': 'test_value' | ||
}, | ||
'conditions': [], | ||
'version': 'test' | ||
}) | ||
|
||
rc_instance = _utils.get_app_service(firebase_admin.get_app(), | ||
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService) | ||
rc_instance._client.session.mount( | ||
'https://firebaseremoteconfig.googleapis.com', | ||
MockAdapter(response, 200, recorder)) | ||
|
||
template = rc_instance.get_server_template() | ||
pijushcs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert template.parameters == dict(test_key="test_value") | ||
assert str(template.version) == 'test' | ||
assert str(template.etag) == 'etag' | ||
|
||
def test_rc_instance_get_server_template_empty_params(self): | ||
recorder = [] | ||
response = json.dumps({ | ||
'conditions': [], | ||
'version': 'test' | ||
}) | ||
|
||
rc_instance = _utils.get_app_service(firebase_admin.get_app(), | ||
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService) | ||
rc_instance._client.session.mount( | ||
'https://firebaseremoteconfig.googleapis.com', | ||
MockAdapter(response, 200, recorder)) | ||
|
||
template = rc_instance.get_server_template() | ||
|
||
assert template.parameters == {} | ||
assert str(template.version) == 'test' | ||
assert str(template.etag) == 'etag' | ||
|
||
|
||
class TestRemoteConfigService: | ||
@classmethod | ||
def setup_class(cls): | ||
cred = testutils.MockCredential() | ||
firebase_admin.initialize_app(cred, {'projectId': 'project-id'}) | ||
|
||
@classmethod | ||
def teardown_class(cls): | ||
testutils.cleanup_apps() | ||
|
||
def test_init_server_template(self): | ||
app = firebase_admin.get_app() | ||
template_data = { | ||
'conditions': [], | ||
'parameters': { | ||
'test_key': 'test_value' | ||
}, | ||
'version': '', | ||
} | ||
|
||
template = remote_config.init_server_template( | ||
app=app, | ||
template_data=ServerTemplateData('etag', template_data) | ||
) | ||
|
||
config = template.evaluate() | ||
assert config.get_string('test_key') == 'test_value' | ||
|
||
@pytest.mark.asyncio | ||
async def test_get_server_template(self): | ||
app = firebase_admin.get_app() | ||
rc_instance = _utils.get_app_service(app, | ||
_REMOTE_CONFIG_ATTRIBUTE, _RemoteConfigService) | ||
|
||
recorder = [] | ||
response = json.dumps({ | ||
'parameters': { | ||
'test_key': 'test_value' | ||
}, | ||
'conditions': [], | ||
'version': 'test' | ||
}) | ||
|
||
rc_instance._client.session.mount( | ||
'https://firebaseremoteconfig.googleapis.com', | ||
MockAdapter(response, 200, recorder)) | ||
|
||
template = await remote_config.get_server_template(app=app) | ||
|
||
config = template.evaluate() | ||
assert config.get_string('test_key') == 'test_value' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.