रिमोट कॉन्फ़िगरेशन ट्रिगर


Firebase Remote Config इवेंट के जवाब में, किसी फ़ंक्शन को ट्रिगर किया जा सकता है. इन इवेंट में, नए कॉन्फ़िगरेशन वर्शन का पब्लिकेशन या पुराने वर्शन पर रोलबैक करना शामिल है. इस गाइड में, Remote Config बैकग्राउंड फ़ंक्शन बनाने का तरीका बताया गया है. यह फ़ंक्शन, टेंप्लेट के दो वर्शन के बीच का अंतर बताता है.

Remote Config फ़ंक्शन को ट्रिगर करना

Remote Config फ़ंक्शन को ट्रिगर करने के लिए, सबसे पहले ज़रूरी मॉड्यूल इंपोर्ट करें:

Node.js

 // The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onConfigUpdated} = require("firebase-functions/v2/remoteConfig");
const logger = require("firebase-functions/logger");
// The Firebase Admin SDK to obtain access tokens.
const admin = require("firebase-admin");
const app = admin.initializeApp();
const fetch = require("node-fetch");
const jsonDiff = require("json-diff");

Python

 # The Cloud Functions for Firebase SDK to set up triggers and logging.
from firebase_functions import remote_config_fn

# The Firebase Admin SDK to obtain access tokens.
import firebase_admin

app = firebase_admin.initialize_app()

import deepdiff
import requests

इसके बाद, अपडेट इवेंट के लिए हैंडलर तय करें. इस फ़ंक्शन में पास किए गए इवेंट ऑब्जेक्ट में, टेंप्लेट अपडेट के बारे में मेटाडेटा होता है. जैसे, अपडेट का नया वर्शन नंबर और समय. अपडेट करने वाले उपयोगकर्ता का ईमेल पता, नाम, और इमेज (अगर उपलब्ध हो) भी देखी जा सकती है.

यहां Remote Config फ़ंक्शन का एक उदाहरण दिया गया है, जो अपडेट किए गए हर वर्शन और उस वर्शन के बीच के अंतर को लॉग करता है जिसकी जगह इसे लाया गया है. यह फ़ंक्शन, टेंप्लेट ऑब्जेक्ट के वर्शन नंबर फ़ील्ड की जांच करता है. साथ ही, मौजूदा (हाल ही में अपडेट किया गया) वर्शन और उससे पहले के वर्शन को वापस लाता है:

Node.js

 exports.showconfigdiff = onConfigUpdated(async (event) => {
  try {
    // Obtain the access token from the Admin SDK
    const accessTokenObj = await admin.credential.applicationDefault()
        .getAccessToken();
    const accessToken = accessTokenObj.access_token;

    // Get the version number from the event object
    const remoteConfigApi = "https://firebaseremoteconfig.googleapis.com/v1/" +
        `projects/${app.options.projectId}/remoteConfig`;
    const currentVersion = event.data.versionNumber;
    const prevVersion = currentVersion - 1;
    const templatePromises = [];
    templatePromises.push(fetch(
        remoteConfigApi,
        {
          method: "POST",
          body: new URLSearchParams([["versionNumber", currentVersion + ""]]),
          headers: {Authorization: "Bearer " + accessToken},
        },
    ));
    templatePromises.push(fetch(
        remoteConfigApi,
        {
          method: "POST",
          body: new URLSearchParams([["versionNumber", prevVersion + ""]]),
          headers: {Authorization: "Bearer " + accessToken},
        },
    ));

    // Get the templates
    const responses = await Promise.all(templatePromises);
    const results = responses.map((r) => r.json());
    const currentTemplate = results[0];
    const previousTemplate = results[1];
    // Figure out the differences of the templates
    const diff = jsonDiff.diffString(previousTemplate, currentTemplate);
    // Log the difference
    logger.log(diff);
  } catch (error) {
    logger.error(error);
  }
});

इस सैंपल में, json-diff और request-promise मॉड्यूल का इस्तेमाल करके, डफ़रेंस बनाया गया है. साथ ही, टेंप्लेट ऑब्जेक्ट पाने के लिए अनुरोध बनाया गया है.

Python

 @remote_config_fn.on_config_updated()
def showconfigdiff(event: remote_config_fn.CloudEvent[remote_config_fn.ConfigUpdateData]) -> None:
    """Log the diff of the most recent Remote Config template change."""

    # Obtain an access token from the Admin SDK
    access_token = app.credential.get_access_token().access_token

    # Get the version number from the event object
    current_version = int(event.data.version_number)

    # Figure out the differences between templates
    remote_config_api = ("https://firebaseremoteconfig.googleapis.com/v1/"
                         f"projects/{app.project_id}/remoteConfig")
    current_template = requests.get(remote_config_api,
                                    params={"versionNumber": current_version},
                                    headers={"Authorization": f"Bearer {access_token}"})
    previous_template = requests.get(remote_config_api,
                                     params={"versionNumber": current_version - 1},
                                     headers={"Authorization": f"Bearer {access_token}"})
    diff = deepdiff.DeepDiff(previous_template, current_template)

    # Log the difference
    print(diff.pretty())

इस सैंपल में, अंतर देखने के लिए deepdiff का इस्तेमाल किया गया है. साथ ही, टेंप्लेट ऑब्जेक्ट पाने के लिए अनुरोध बनाने और भेजने के लिए requests का इस्तेमाल किया गया है.