firebase / firebase-admin-python

Firebase Admin Python SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.03k stars 320 forks source link

FR: Remote Config Support #521

Open allentv opened 3 years ago

allentv commented 3 years ago

Node and Java have full support for Remote Config. I had worked on a PR for the Remote Config spec for Golang few months ago and was wondering if there are plans for Python support?

lahirumaramba commented 3 years ago

Hi @allentv, Thank you for the feature request! Adding Remote Config support to Admin Python SDK is something we plan to work on. Unfortunately, I cannot promise you a timeline as we have not started any work on this yet. This issue will track the progress once we start working on this feature.

Thanks again for contributing to the Admin Go SDK! As always, we greatly appreciate contributions to the codebase if you plan to work on adding Remote Config support to the Python SDK. Let me know if you are interested and we can start with putting together an API proposal first.

allentv commented 3 years ago

I am interested in working on the API proposal. What are the next steps?

lahirumaramba commented 3 years ago

Hi @allentv ! That is great! You can start by proposing an API similar to what we did in the Go repo. Please also refer to the released RC APIs in Admin Node.js and Java SDKs. Let me know if you have any questions or need help working on the API proposal. Once we agree on an API surface here I can start the internal review process for public APIs. Thank you!

allentv commented 3 years ago

I unfortunately don't have free time to work on this. Could you assign this to someone else who might be interested?

lahirumaramba commented 3 years ago

No problem! Thank you for you interest @allentv! We highly appreciate it!

MaitreyaBuddha commented 9 months ago

Here's something I threw together in case anyone finds this and wants a free baseline

class RemoteConfigClient:
    def __init__(self, app: App):
        self.app = app
        self.access_token = self._get_access_token()
        self.project_id = self.app.project_id
        self.base_url = f"https://firebaseremoteconfig.googleapis.com/v1/projects/{self.project_id}/remoteConfig"

    @property
    def config(self):
        return self.fetch_and_activate()

    def _get_access_token(self) -> str:
        access_token = self.app.credential.get_access_token().access_token
        return access_token

    def fetch_and_activate(self):
        response = requests.get(self.base_url, headers=self._get_headers())
        response.raise_for_status()
        return response.json()

    def _get_headers(self) -> dict:
        return {
            "Authorization": f"Bearer {self.access_token}",
            "Content-Type": "application/json; UTF-8"
        }

    def get_string(self, key: str, default='') -> str:
        parameters = self.config.get('parameters', {})
        return parameters.get(key, {}).get('defaultValue', {}).get('value', default)

    def get_bool(self, key: str, default=False) -> bool:
        parameters = self.config.get('parameters', {})
        return parameters.get(key, {}).get('defaultValue', {}).get('value', default)

Example usage:

remote_config = RemoteConfigClient(get_app())
remote_config.get_bool('is_feature_enabled_example')

And if this is enough of an API proposal (for some features), then great.