Goal: Perform a simple cURL GET using an external REST endpoint.
-
This function basicCurlGet communicates with a public REST service.
-
Requires Eventing Storage (or metadata collection) and a "source" collection.
-
Needs a Binding of type URL Alias (as documented in the Scriptlet).
-
Will operate on any mutation of the KEY "make_curl_request::1".
-
The actual cURL request from the Eventing Function will be equivalent to either of the following (but test to make sure the service is live):
curl -q -X GET 'https://api.frankfurter.app/latest' curl -q -X GET 'https://api.ratesapi.io/api/latest'
-
Only logs the REST response JSON payload to the Application log file.
-
For a more complete example using this public REST endpoint (or to make your own service), refer to External REST via cURL GET.
-
basicCurlGet
-
Input Data/Mutation
-
Output Data/Logged
// To run configure the settings for this Function, basicCurlGet, as follows:
//
// Version 7.0+
// "Listen to Location"
// bulk.data.source
// "Eventing Storage"
// rr100.eventing.metadata
// Binding(s)
// 1. "binding type", "alias name...", "URL...", "misc.",
// "URL alias", "exchangeRateApi", "https://api.frankfurter.app/", "no auth"
//
// Version 6.X
// "Source Bucket"
// source
// "MetaData Bucket"
// metadata
// Binding(s)
// 1. "binding type", "alias name...", "URL...", "misc.",
// "URL alias", "exchangeRateApi", "https://api.frankfurter.app/", "no auth"
function OnUpdate(doc, meta) {
// You would typically filter to mutations of interest
if (meta.id !== 'make_curl_request::1') return;
try {
// only make a cURL GET request id we see a mutation on the above KEY
var request = { path: "latest" }; // easiest API call could supply YYYY-MM-DD
// perform the cURL request using the URL alias from the settings
var response = curl('GET', exchangeRateApi, request);
if (response.status != 200 && response.status != 302) {
log("cURL GET failed response.status:",response.status)
} else {
log("cURL GET success, response.body:",response.body)
// optional write to a bucket - requires a binding alias in settings
// dst_col[meta.id] = response.body;
}
} catch (e) {
log("cURL request had an exception:",e)
}
}
INPUT: KEY make_curl_request::1
{
"anything": 1
}
2021-07-18T18:13:57.566-07:00 [INFO] "cURL GET success, response.body:"
{
"amount": 1,
"base": "EUR",
"date": "2021-07-16",
"rates": {
"AUD": 1.5907,
"BGN": 1.9558,
"BRL": 6.0146,
"CAD": 1.4856,
"CHF": 1.0853,
"CNY": 7.6373,
"CZK": 25.538,
"DKK": 7.4381,
"GBP": 0.85298,
"HKD": 9.1684,
"HRK": 7.4968,
"HUF": 359.73,
"IDR": 17083,
"ILS": 3.8796,
"INR": 88.03,
"ISK": 145.9,
"JPY": 130.03,
"KRW": 1347.94,
"MXN": 23.459,
"MYR": 4.9681,
"NOK": 10.3878,
"NZD": 1.6836,
"PHP": 59.364,
"PLN": 4.5867,
"RON": 4.9285,
"RUB": 87.52,
"SEK": 10.2428,
"SGD": 1.5993,
"THB": 38.669,
"TRY": 10.0521,
"USD": 1.1802,
"ZAR": 16.984
}
}