使用 Firebase Cloud Functions,我可以安排来自外部 API 的更新来更新 Firestore
Using Firebase Cloud Functions, can I schedule updates from an external API to update Firestore
这是我要完成的事情的想法。使用云函数,有没有办法在没有 axios 的情况下获取 API 数据?有没有办法在预定的 pubsub 函数中获取这些数据?
const functions = require('firebase-functions');
const axios = require('axios');
const cors = require('cors')({ origin: true });
exports.getVehicles = functions.https.onCall((req:any, res:any) => {
cors(req, res, () => {
if (req.method !== "GET") {
return res.status(401).json({
message: "Not allowed"
});
}
return axios.get('https://api.zubiecar.com/api/v2/zinc/vehicles', {
method: 'GET', // or 'PUT'
headers: {
'Content-Type': 'application/json',
"Zubie-Api-Key": "123456789"
},
})
.then((response:any) => {
console.log(response.data);
return res.status(200).json({
message: response.data.ip
})
})
.catch((err:any) => {
return res.status(500).json({
error: err
})
})
})
});
exports.updateDriverLocation = functions.pubsub.schedule('every 2 minutes').onRun(async(context:any) => {
//return array of driver objects from api
const update = await getVehicles();
//database
const DB = admin.firestore()
const REF = DB.collection("drivers")
const BATCH = DB.batch()
//update firestore with api response
update.forEach((vehicle:any) => {
BATCH.set( REF.doc(vehicle.nickname),
{vehicle},
{ merge: true }
)
})
await BATCH.commit()
return null;
});
基本上,我希望让我的 Firestore 数据库与 Zubie API 保持同步,Zubie 每两分钟更新一次车辆位置。或者,我正在使用 nextJS 并探索使用 useSWR 在页面加载时完成这些更新。然而,这也带来了自身的挑战。
回答
const getVehicles = async () => {
let url = `https://api.zubiecar.com/api/v2/zinc/vehicles`
let response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Zubie-Api-Key':'fooBar',
},
})
const json = await response.json()
return json
}
exports.updateVehicles = functions.pubsub
.schedule('every 5 minutes')
.onRun(async () => {
const DB = admin.firestore()
const REF = DB.collection('drivers')
const BATCH = DB.batch()
const {vehicles} = await getVehicles()
for (const key in vehicles) {
const vehicle = vehicles[key]
const {nickname} = vehicle
BATCH.set(REF.doc(nickname), {vehicle}, {merge: true})
}
await BATCH.commit()
return
})
Using cloud functions, is there a way to fetch API data without axios?
如果您想访问某些 API,您必须自己编写该代码。 Cloud Functions 不会为您做这些。 Cloud Functions 只是一个托管容器,它在触发时 运行 存储您的代码。
Is there a way to get this data inside a scheduled pubsub function?
当然,您可以编写一个计划函数来定期触发,并且您可以让该代码访问 API。那应该不会比你现在拥有的更难。您几乎可以重用所有代码。
Essentially, I'm looking to keep my Firestore database in sync with the Zubie API, which updates vehicle locations every two minutes.
您每 5 分钟最多只能 运行 预定的功能。它不能配置为 运行 更频繁。
这是我要完成的事情的想法。使用云函数,有没有办法在没有 axios 的情况下获取 API 数据?有没有办法在预定的 pubsub 函数中获取这些数据?
const functions = require('firebase-functions');
const axios = require('axios');
const cors = require('cors')({ origin: true });
exports.getVehicles = functions.https.onCall((req:any, res:any) => {
cors(req, res, () => {
if (req.method !== "GET") {
return res.status(401).json({
message: "Not allowed"
});
}
return axios.get('https://api.zubiecar.com/api/v2/zinc/vehicles', {
method: 'GET', // or 'PUT'
headers: {
'Content-Type': 'application/json',
"Zubie-Api-Key": "123456789"
},
})
.then((response:any) => {
console.log(response.data);
return res.status(200).json({
message: response.data.ip
})
})
.catch((err:any) => {
return res.status(500).json({
error: err
})
})
})
});
exports.updateDriverLocation = functions.pubsub.schedule('every 2 minutes').onRun(async(context:any) => {
//return array of driver objects from api
const update = await getVehicles();
//database
const DB = admin.firestore()
const REF = DB.collection("drivers")
const BATCH = DB.batch()
//update firestore with api response
update.forEach((vehicle:any) => {
BATCH.set( REF.doc(vehicle.nickname),
{vehicle},
{ merge: true }
)
})
await BATCH.commit()
return null;
});
基本上,我希望让我的 Firestore 数据库与 Zubie API 保持同步,Zubie 每两分钟更新一次车辆位置。或者,我正在使用 nextJS 并探索使用 useSWR 在页面加载时完成这些更新。然而,这也带来了自身的挑战。
回答
const getVehicles = async () => {
let url = `https://api.zubiecar.com/api/v2/zinc/vehicles`
let response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Zubie-Api-Key':'fooBar',
},
})
const json = await response.json()
return json
}
exports.updateVehicles = functions.pubsub
.schedule('every 5 minutes')
.onRun(async () => {
const DB = admin.firestore()
const REF = DB.collection('drivers')
const BATCH = DB.batch()
const {vehicles} = await getVehicles()
for (const key in vehicles) {
const vehicle = vehicles[key]
const {nickname} = vehicle
BATCH.set(REF.doc(nickname), {vehicle}, {merge: true})
}
await BATCH.commit()
return
})
Using cloud functions, is there a way to fetch API data without axios?
如果您想访问某些 API,您必须自己编写该代码。 Cloud Functions 不会为您做这些。 Cloud Functions 只是一个托管容器,它在触发时 运行 存储您的代码。
Is there a way to get this data inside a scheduled pubsub function?
当然,您可以编写一个计划函数来定期触发,并且您可以让该代码访问 API。那应该不会比你现在拥有的更难。您几乎可以重用所有代码。
Essentially, I'm looking to keep my Firestore database in sync with the Zubie API, which updates vehicle locations every two minutes.
您每 5 分钟最多只能 运行 预定的功能。它不能配置为 运行 更频繁。