...and I've tried Google and the FF Community, but without luck.
Why not use API Calls? The requirement needs better flexibility, and I am hiding the authorizations behind the Cloud Function.
So what I was looking for is a cloud function that will GET data from a target host and path, and return the data as JSON.
I'll copy-paste my current code (this is NOT working; Google Cloud Functions suggest that the call was successful, but the data is not being returned).
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const https = require('https');
exports.testGetCases = functions.region('australia-southeast1').
runWith({
timeoutSeconds: 60, // Consider increasing if necessary
memory: '512MB'
}).https.onCall(
(data, context) => {
return new Promise((resolve, reject) => {
const options = {
hostname: '{callHostUrl}',
port: 443,
path: '{callPath}',
method: 'GET',
headers: {
'Authorization': `Basic {basicAuthValue}`
}
};
const req = https.request(options, (res) => {
let dataReceived = '';
res.on('data', (chunk) => {
dataReceived += chunk;
// Process data in chunks if needed (e.g., parsing JSON)
// ...
});
res.on('end', () => {
try {
const responseJSON = JSON.parse(dataReceived);
return resolve(responseJSON.data); // Assuming data is in "data" property
} catch (error) {
reject(new functions.https.HttpsError('internal', 'Error processing response', error));
}
});
});
req.on('error', (error) => {
reject(new functions.https.HttpsError('internal', 'Error reading data', error));
});
req.end();
});
});
So I have these questions:
What did I miss in the code above?
Is there an existing working sample online for a Cloud Function that I need?
EDIT 4 September 2024: Answer found. Refer to replies below for further details.