[英]Using Firebase Cloud Functions, can I schedule updates from an external API to update Firestore
Here is an idea of what I'm trying to accomplish.这是我要完成的工作的一个想法。 Using cloud functions, is there a way to fetch API data without axios?
使用云函数,有没有办法在没有axios的情况下获取API数据? Is there a way to get this data inside a scheduled pubsub function?
有没有办法在预定的 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;
});
Essentially, I'm looking to keep my Firestore database in sync with the Zubie API, which updates vehicle locations every two minutes.从本质上讲,我希望我的 Firestore 数据库与 Zubie API 保持同步,它每两分钟更新一次车辆位置。 Alternatively, I am using nextJS and exploring the use of useSWR to accomplish these updates when a page loads.
或者,我正在使用 nextJS 并探索使用 useSWR 在页面加载时完成这些更新。 However, that is presenting its own challenges also.
然而,这也带来了自己的挑战。
ANSWER回答
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?
使用云函数,有没有办法在没有axios的情况下获取API数据?
If you want to access some API, you'll have to write that code yourself.如果您想访问某些 API,则必须自己编写该代码。 Cloud Functions will not do that for you.
Cloud Functions 不会为您执行此操作。 Cloud Functions is just a hosted container that runs your code when triggered.
Cloud Functions 只是一个托管容器,可在触发时运行您的代码。
Is there a way to get this data inside a scheduled pubsub function?
有没有办法在预定的 pubsub 函数中获取这些数据?
Sure, you can write a scheduled function to trigger periodically, and you can have that code access the API.当然,您可以编写一个计划函数来定期触发,并且您可以让该代码访问 API。 That should be no more difficult than what you have now.
这应该不会比你现在拥有的更困难。 You can reuse almost all the code.
您可以重用几乎所有代码。
Essentially, I'm looking to keep my Firestore database in sync with the Zubie API, which updates vehicle locations every two minutes.
从本质上讲,我希望我的 Firestore 数据库与 Zubie API 保持同步,它每两分钟更新一次车辆位置。
You can only run scheduled functions at most every 5 minutes.您最多只能每 5 分钟运行一次预定功能。 It can't be configured to run more frequently.
它无法配置为更频繁地运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.