Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Start writing Firebase Functions
- // https://firebase.google.com/docs/functions/typescript
- import * as admin from 'firebase-admin';
- import fetch from 'node-fetch';
- import * as functions from 'firebase-functions';
- //replace with MTA API URL and key
- const myUrl = "https://jsonplaceholder.typicode.com/posts/1";
- var cachedMessage = {};
- const makeHttpRequest = async (url:string) => {
- try {
- console.log('making http request');
- const response = await fetch(url);
- console.log("fetched response:" + response);
- const json = await response.json();
- console.log("Formatted JSON:" + json);
- return json;
- } catch (error) {
- console.log(error);
- }
- };
- console.log('Calling makeHttpRequest(url)');
- makeHttpRequest(myUrl);
- console.log('await statement is being ignored if this is logged before makeHttpRequest(url) is complete');
- admin.initializeApp();
- //correct way to schedule cron instead of setInterval
- exports.scheduledFunction = functions.pubsub.schedule('every 2 minutes').onRun((context) => {
- cachedMessage = makeHttpRequest(myUrl);
- });
- //expose the response via endpoint
- export const requestEndpoint = functions.https.onRequest((request, response) => {
- response.send(cachedMessage)
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement