Hi All,
I have set a context variable in proxy preflow using a javascript. In node js I am access that variable.
I am preparing a final json response in node.js. I am getting target response and capturing it in a variable in nodjs. And finally I am preparing response by mapping fields from target response to one final json object and sending that json object as final response to Client.
I am mapping lijke below for example:
finalResponse.YearOfBrth = targetResponse.My_KR_Year_Of_Birth;
hete KR is country code,so instead of using the entire name as My_KR_Year_Of_Birth I want to map it as My_{country}_Year_Of_Birth where country is from context variable.
How can I achieve this so that I can generalise the mapping for different country codes as the remaining part of the field name would be same for other countries.
Regards
Ravi
This seems like mostly a nodejs question.
I suppose you would dynamically determine the lookup key in the hash, depending on an inbound context variable. It might look like this:
// index.js // ------------------------------------------------------------------ var apigee = require('apigee-access'); var express = require('express'); var request = require('request'); var sprintf = require("sprintf-js").sprintf; var moment = require('moment-timezone'); var app = express(); var x = 0; app.get('/yob', function(req, resp) { var requestOptions = { timeout : 66000, // in ms uri: 'https://dchiesa-first-project.appspot.com/yob', method: 'get', json: true, headers: { 'accept' : 'application/json', 'user-agent' : 'requestjs node-response-shape' } }; var countryCode = apigee.getVariable(req, 'country_code') + ''; var putativeKey = sprintf("My_%s_Year_Of_Birth", countryCode); request(requestOptions, function(e, httpResp, body) { var actualKey = (body[putativeKey]) ? putativeKey : "My_KR_Year_Of_Birth"; var finalResponse = { now : moment().tz('GMT').format(), countryCode : countryCode, actualKey : actualKey, YearOfBirth : body[actualKey] }; resp.header('content-type','application/json') .status(200) .send(finalResponse); }); }); // default behavior app.all(/^\/.*/, function(request, response) { response.header('Content-Type', 'application/json') .status(404) .send('{ "message" : "This is not the server you\'re looking for." }\n'); }); app.listen(3000, function () { console.log('Example app listening on port 3000!'); });
And in the request flow, you need to make sure your context variable ("country_code") is set to something reasonable.