میتوانید تابعی را در پاسخ به رویدادهای Firebase Remote Config، ازجمله انتشار نسخه پیکربندی جدید یا برگشت به نسخه قدیمیتر، راهاندازی کنید. این راهنما نحوه ایجاد تابع پسزمینهای Remote Config را که تفاوت دو نسخه الگو را انجام میدهد شرح میدهد.
راهاندازی تابع Remote Config
برای راهاندازی تابع Remote Config، ابتدا واحدهای موردنیاز را وارد کنید:
Node.js
const {URL, URLSearchParams} = require("node:url");
// The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onConfigUpdated} = require("firebase-functions/remoteConfig");
const {requiresRole, requiresAPI} = require("firebase-functions");
const logger = require("firebase-functions/logger");
// The Firebase Admin SDK to obtain access tokens.
const {initializeApp, applicationDefault} = require("firebase-admin/app");
initializeApp();
const jsonDiff = require("json-diff");
پایتون
# 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 applicationDefault()
.getAccessToken();
const accessToken = accessTokenObj.access_token;
// Get the version number from the event object
const remoteConfigApi = new URL(
`https://firebaseremoteconfig.googleapis.com/v1/projects/${process.env.GCLOUD_PROJECT}/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 = await Promise.all(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 برای
ایجاد تفاوت و ساختن درخواست برای دریافت شیء الگو استفاده میکند.
پایتون
@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 برای ساختن و ارسال درخواست دریافت شیء الگو استفاده میکند.