LoginSignup
1
0

More than 5 years have passed since last update.

python-firebase-adminのsdkを使ってfirebaseのRemoteConfigを更新したかった

Last updated at Posted at 2018-08-23

ない!

image.jpg

ない! RemoteConfigの項目がない!
SDKからはRemoteConfigが更新できない!(らしい!!)

世界はなぜ僕を追い詰めるのか
REST APIはあるので、APIから更新してみようと思います。

GoogleAppEngine から firebaseのRemoteConfigを更新してみる

このページから確認できてしまいます。
GETで登録中の値を、POSTで指定の項目を更新できます。
どういうパラメータを送ればいいのかは……↓のリポジトリから忖度してください。

制作物

ソースを用意してみたので、もし良ければ参考までに。
こうするともっと簡単にできたり、便利なライブラリがあったりしたら、コメントなどいただければ!

実装例

import json

import requests
from google.auth.app_engine import Credentials, get_project_id
from google.auth.transport import requests as google_request


class RemoteConfigRequest:
    def __init__(self):
        credentials = Credentials(
            scopes=[
                "https://www.googleapis.com/auth/firebase.remoteconfig"
            ]
        )
        credentials.refresh(google_request.Request())

        self.request_url = "https://firebaseremoteconfig.googleapis.com/v1/projects/{}/remoteConfig".format(
            get_project_id()
        )
        self.token = 'Bearer {}'.format(credentials.token)

    def get(self):
        r = requests.get(self.request_url, headers={
            'Authorization': self.token
        })
        return r.json()

    def put(self, data):
        put_data = {
            "parameters": data
        }

        r = requests.put(self.request_url, json.dumps(put_data), headers={
            'Authorization': self.token,
            'If-Match': '*'
        })

        return r.json()

Credentials情報をGAE、または事前に取得しておいたservice_account.jsonから作成してあげればOKです!
それを元にREST API実行用のtokenを取得してあげれば、後は指示通りにリクエストを投げるだけで済んでしまいます。
If-Matchを上手く設定してあげないと更新できないので、商用で使う場合は……少し賢く使ってあげないとダメそうです。

ね、簡単でしょう?

最後に

株式会社ネコカリでは猫の手も借りたい🔥炎上中🔥なお仕事を募集しています!
firebaseを使うお仕事でもなんでもウェルカム。
一緒に働くメンバーも募集していますので、よかったら是非!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0