1

I have an android application and user data is stored on the firestore. I want to fetch that data and manipulate the results on Google Cloud Functions (for ranking system) with python and directly send those results to Firebase Remote Config, so that user can fetch the data from there.

In official document they have showed the same thing in pictureimgage.

But the description is totally different.Documentation.

So my question is: Can I send the Results which are calculated on the google cloud function directly to the firebase remote config?

Also we have a REST API to get and change Firebase Remote Config values. Is that possible that we can call this api at our Google Cloude Function?

. I am using this code which requires a token,however I am using this cloud function on the based on same account as Remore config

def _get():

Retrieve the current Firebase Remote Config template from server and store it locally.

headers = {
'Authorization': 'Bearer ' + _get_access_token()
 }
resp = requests.get(REMOTE_CONFIG_URL, headers=headers)

if resp.status_code == 200:
  with io.open('config.json', 'wb') as f:
    f.write(resp.text.encode('utf-8'))

 print('Retrieved template has been written to config.json')
 print('ETag from server: {}'.format(resp.headers['ETag']))
else:
 print('Unable to get template')
 print(resp.text)

Here is the code for Publish the data

`def _publish(etag):
 """Publish local template to Firebase server.
 Args:
  etag: ETag for safe (avoid race conditions)
 """
 with open('**config.json**', 'r', encoding='utf-8') as f:
 content = f.read()
 headers = {
  'Authorization': 'Bearer ' + _get_access_token(),
  'Content-Type': 'application/json; UTF-8',
  'If-Match': etag
}
 resp =  requests.
 put(REMOTE_CONFIG_URL,
 data=content.encode('utf-8'),headers=headers)
if resp.status_code == 200:
  print('Template has been published.')
  print('ETag from server: {}'.format(resp.headers['ETag']))
else:
  print('Unable to publish template.')
  print(resp.text)`

Just like I can access the firestore database without authentication in my cloud function why can't I update the Remote config without config.json? Function only have 2 files main.py and Requirements

If Authentication is necessary for google cloud function to update the Remote Config where can I store the config.json file?

Irfan Yaqub
  • 402
  • 4
  • 14
  • 2
    There is an API for updating remote config and yes you can do this with Cloud functions. Is there any reason to use remote config? Firebase realtime database seems ideal if this is just used for updating users with latest results. Also checkout: [Firebase remote config vs database](https://stackoverflow.com/questions/45001096/firebase-remote-config-vs-database) – Dharmaraj Dec 31 '21 at 09:46
  • My application have a sort of game. And the game have different levels. We record the completion time of all the players. Now I want to apply some data science functions on that data so that user can see their ranking worldwide. You told me that is possible. Can you please explain how or give me the link where i can explore. I have read all the documentation but they did not mention this. – Irfan Yaqub Dec 31 '21 at 10:24
  • Is it like a periodic function? Then you can use [Firebase Scheduled Functions](https://firebase.google.com/docs/functions/schedule-functions), run your logic in them and update realtime database. Your clients would be listening to database and data would be update immediately. – Dharmaraj Dec 31 '21 at 10:29
  • Let me simplify. I will pull the data( Time of game completion of all users) in google cloud function. There i will use python to genrate an array of top 100 numbers for each level. Then i want to replace those values which is in the form of json on my remote config. I am doing this because Ranking does not change oftenly. So there is no need to update data everyday. – Irfan Yaqub Dec 31 '21 at 10:38
  • If it is still not clear let me know ill update the question. – Irfan Yaqub Dec 31 '21 at 10:38
  • 1
    Yes, it is possible to call the [Remote Config REST API](https://firebase.google.com/docs/reference/remote-config/rest) (and pretty much any other REST API) from Cloud Functions. Did you try anything yet? If so, can you show us the [minimal code that reproduces where you got stuck](http://stackoverflow.com/help/mcve)? – Frank van Puffelen Dec 31 '21 at 16:49
  • @FrankvanPuffelen do I need to have my credentials like token etc if remote config and cloud functions are based on the same account? – Irfan Yaqub Jan 28 '22 at 10:14

1 Answers1

2

It seems you'll be manually triggering the function whenever required. You can create an HTTP function and verify that it's you who has triggered it so no one else can.

from flask import escape
import functions_framework

def update_data_http(request):
    # 1. Verify the function is called by you
    # 2. Perform the calculations
    # 3. Update the result in Remote Config / Realtime Database
    return 'Data updated!!'

Firebase Admin SDK for Python doesn't include Remote Config so you would have to use Remote Config REST API to update the data. Using Realtime database for this might be a bit easier since you can update data directly using Admin SDK.

Can I send the Results which are calculated on the google cloud function directly to the firebase remote config?

Yes, using the REST API linked above.

Is that possible that we can call this api at our Google Cloude Function?

You can call any API from your Cloud function. Refer to this answer for calling APIs using Python: Making a request to a RESTful API using python

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84