Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud function to fetch data from third party server

I am new to Google Cloud Functions features and implementation. So I want to know that is it possible to make HTTP or HTTPS request to third party server API using Cloud function and if yes then how? And when I receive data in response can I store it into my firebase database using same cloud function instance?

And how can I make this request to be called periodically or schedule it? Thanks in advance

like image 508
Ankit Gupta Avatar asked Jan 09 '19 13:01

Ankit Gupta


People also ask

What is Lambda equivalent in GCP?

Azure Functions, compared to AWS Lambda and Google Cloud Functions, is more flexible and complex about how users deploy serverless functions as part of a larger workload. Azure Functions users can deploy code directly on the Azure Functions service or run the software inside Docker containers.

Which trigger is not supported by Cloud Functions?

In Cloud Functions (2nd gen), Pub/Sub triggers and Cloud Storage triggers are implemented as particular types of Eventarc triggers. Note: Eventarc does not currently support direct events from Firestore, Google Analytics for Firebase, or Firebase Authentication.

What is the difference between on call and on request function?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.

What is FaaS Gcp?

Use open source FaaS (function as a service) framework to run functions across multiple environments and prevent lock-in. Supported environments include Cloud Functions, local development environment, on-premises, Cloud Run, Cloud Run for Anthos, and other Knative-based serverless environments. Pricing.


1 Answers

Here's how to do it using node-fetch.

Your Cloud Function:

const fetch = require('node-fetch');

exports.functionName= (req, res) => {
  const fetchFromURL = async () => await (await fetch('https://yourURL.com')).json();

  fetchFromURL().then((data) => {
    // do something with data (received from URL).
  });
};

You will need to add the "node-fetch" dependency to your function's package.json as well.

Your package.json:

{
  "name": "sample-http",
  "version": "0.0.1",
  "dependencies": {
    "node-fetch": "^2.6.1"
  }
}
like image 82
Mimina Avatar answered Oct 06 '22 00:10

Mimina