rest.js
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var util = require('../util');
var populateHostPrefix = require('./helpers').populateHostPrefix;
function populateMethod(req) {
req.httpRequest.method = req.service.api.operations[req.operation].httpMethod;
}
function generateURI(endpointPath, operationPath, input, params) {
var uri = [endpointPath, operationPath].join('/');
uri = uri.replace(/\/+/g, '/');
var queryString = {}, queryStringSet = false;
util.each(input.members, function (name, member) {
var paramValue = params[name];
if (paramValue === null || paramValue === undefined) return;
if (member.location === 'uri') {
var regex = new RegExp('\\{' + member.name + '(\\+)?\\}');
uri = uri.replace(regex, function(_, plus) {
var fn = plus ? util.uriEscapePath : util.uriEscape;
return fn(String(paramValue));
});
} else if (member.location === 'querystring') {
queryStringSet = true;
if (member.type === 'list') {
queryString[member.name] = paramValue.map(function(val) {
return util.uriEscape(member.member.toWireFormat(val).toString());
});
} else if (member.type === 'map') {
util.each(paramValue, function(key, value) {
if (Array.isArray(value)) {
queryString[key] = value.map(function(val) {
return util.uriEscape(String(val));
});
} else {
queryString[key] = util.uriEscape(String(value));
}
});
} else {
queryString[member.name] = util.uriEscape(member.toWireFormat(paramValue).toString());
}
}
});
if (queryStringSet) {
uri += (uri.indexOf('?') >= 0 ? '&' : '?');
var parts = [];
util.arrayEach(Object.keys(queryString).sort(), function(key) {
if (!Array.isArray(queryString[key])) {
queryString[key] = [queryString[key]];
}
for (var i = 0; i < queryString[key].length; i++) {
parts.push(util.uriEscape(String(key)) + '=' + queryString[key][i]);
}
});
uri += parts.join('&');
}
return uri;
}
function populateURI(req) {
var operation = req.service.api.operations[req.operation];
var input = operation.input;
var uri = generateURI(req.httpRequest.endpoint.path, operation.httpPath, input, req.params);
req.httpRequest.path = uri;
}
function populateHeaders(req) {
var operation = req.service.api.operations[req.operation];
util.each(operation.input.members, function (name, member) {
var value = req.params[name];
if (value === null || value === undefined) return;
if (member.location === 'headers' && member.type === 'map') {
util.each(value, function(key, memberValue) {
req.httpRequest.headers[member.name + key] = memberValue;
});
} else if (member.location === 'header') {
value = member.toWireFormat(value).toString();
if (member.isJsonValue) {
value = util.base64.encode(value);
}
req.httpRequest.headers[member.name] = value;
}
});
}
function buildRequest(req) {
populateMethod(req);
populateURI(req);
populateHeaders(req);
populateHostPrefix(req);
}
function extractError() {
}
function extractData(resp) {
var req = resp.request;
var data = {};
var r = resp.httpResponse;
var operation = req.service.api.operations[req.operation];
var output = operation.output;
// normalize headers names to lower-cased keys for matching
var headers = {};
util.each(r.headers, function (k, v) {
headers[k.toLowerCase()] = v;
});
util.each(output.members, function(name, member) {
var header = (member.name || name).toLowerCase();
if (member.location === 'headers' && member.type === 'map') {
data[name] = {};
var location = member.isLocationName ? member.name : '';
var pattern = new RegExp('^' + location + '(.+)', 'i');
util.each(r.headers, function (k, v) {
var result = k.match(pattern);
if (result !== null) {
data[name][result[1]] = v;
}
});
} else if (member.location === 'header') {
if (headers[header] !== undefined) {
var value = member.isJsonValue ?
util.base64.decode(headers[header]) :
headers[header];
data[name] = member.toType(value);
}
} else if (member.location === 'statusCode') {
data[name] = parseInt(r.statusCode, 10);
}
});
resp.data = data;
}
/**
* @api private
*/
module.exports = {
buildRequest: buildRequest,
extractError: extractError,
extractData: extractData,
generateURI: generateURI
};