query.js
3.03 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
var AWS = require('../core');
var util = require('../util');
var QueryParamSerializer = require('../query/query_param_serializer');
var Shape = require('../model/shape');
var populateHostPrefix = require('./helpers').populateHostPrefix;
function buildRequest(req) {
var operation = req.service.api.operations[req.operation];
var httpRequest = req.httpRequest;
httpRequest.headers['Content-Type'] =
'application/x-www-form-urlencoded; charset=utf-8';
httpRequest.params = {
Version: req.service.api.apiVersion,
Action: operation.name
};
// convert the request parameters into a list of query params,
// e.g. Deeply.NestedParam.0.Name=value
var builder = new QueryParamSerializer();
builder.serialize(req.params, operation.input, function(name, value) {
httpRequest.params[name] = value;
});
httpRequest.body = util.queryParamsToString(httpRequest.params);
populateHostPrefix(req);
}
function extractError(resp) {
var data, body = resp.httpResponse.body.toString();
if (body.match('<UnknownOperationException')) {
data = {
Code: 'UnknownOperation',
Message: 'Unknown operation ' + resp.request.operation
};
} else {
try {
data = new AWS.XML.Parser().parse(body);
} catch (e) {
data = {
Code: resp.httpResponse.statusCode,
Message: resp.httpResponse.statusMessage
};
}
}
if (data.requestId && !resp.requestId) resp.requestId = data.requestId;
if (data.Errors) data = data.Errors;
if (data.Error) data = data.Error;
if (data.Code) {
resp.error = util.error(new Error(), {
code: data.Code,
message: data.Message
});
} else {
resp.error = util.error(new Error(), {
code: resp.httpResponse.statusCode,
message: null
});
}
}
function extractData(resp) {
var req = resp.request;
var operation = req.service.api.operations[req.operation];
var shape = operation.output || {};
var origRules = shape;
if (origRules.resultWrapper) {
var tmp = Shape.create({type: 'structure'});
tmp.members[origRules.resultWrapper] = shape;
tmp.memberNames = [origRules.resultWrapper];
util.property(shape, 'name', shape.resultWrapper);
shape = tmp;
}
var parser = new AWS.XML.Parser();
// TODO: Refactor XML Parser to parse RequestId from response.
if (shape && shape.members && !shape.members._XAMZRequestId) {
var requestIdShape = Shape.create(
{ type: 'string' },
{ api: { protocol: 'query' } },
'requestId'
);
shape.members._XAMZRequestId = requestIdShape;
}
var data = parser.parse(resp.httpResponse.body.toString(), shape);
resp.requestId = data._XAMZRequestId || data.requestId;
if (data._XAMZRequestId) delete data._XAMZRequestId;
if (origRules.resultWrapper) {
if (data[origRules.resultWrapper]) {
util.update(data, data[origRules.resultWrapper]);
delete data[origRules.resultWrapper];
}
}
resp.data = data;
}
/**
* @api private
*/
module.exports = {
buildRequest: buildRequest,
extractError: extractError,
extractData: extractData
};