rest_xml.js
2.85 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
var AWS = require('../core');
var util = require('../util');
var Rest = require('./rest');
function populateBody(req) {
var input = req.service.api.operations[req.operation].input;
var builder = new AWS.XML.Builder();
var params = req.params;
var payload = input.payload;
if (payload) {
var payloadMember = input.members[payload];
params = params[payload];
if (params === undefined) return;
if (payloadMember.type === 'structure') {
var rootElement = payloadMember.name;
req.httpRequest.body = builder.toXML(params, payloadMember, rootElement, true);
} else { // non-xml payload
req.httpRequest.body = params;
}
} else {
req.httpRequest.body = builder.toXML(params, input, input.name ||
input.shape || util.string.upperFirst(req.operation) + 'Request');
}
}
function buildRequest(req) {
Rest.buildRequest(req);
// never send body payload on GET/HEAD
if (['GET', 'HEAD'].indexOf(req.httpRequest.method) < 0) {
populateBody(req);
}
}
function extractError(resp) {
Rest.extractError(resp);
var data;
try {
data = new AWS.XML.Parser().parse(resp.httpResponse.body.toString());
} catch (e) {
data = {
Code: resp.httpResponse.statusCode,
Message: resp.httpResponse.statusMessage
};
}
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) {
Rest.extractData(resp);
var parser;
var req = resp.request;
var body = resp.httpResponse.body;
var operation = req.service.api.operations[req.operation];
var output = operation.output;
var hasEventOutput = operation.hasEventOutput;
var payload = output.payload;
if (payload) {
var payloadMember = output.members[payload];
if (payloadMember.isEventStream) {
parser = new AWS.XML.Parser();
resp.data[payload] = util.createEventStream(
AWS.HttpClient.streamsApiVersion === 2 ? resp.httpResponse.stream : resp.httpResponse.body,
parser,
payloadMember
);
} else if (payloadMember.type === 'structure') {
parser = new AWS.XML.Parser();
resp.data[payload] = parser.parse(body.toString(), payloadMember);
} else if (payloadMember.type === 'binary' || payloadMember.isStreaming) {
resp.data[payload] = body;
} else {
resp.data[payload] = payloadMember.toType(body);
}
} else if (body.length > 0) {
parser = new AWS.XML.Parser();
var data = parser.parse(body.toString(), output);
util.update(resp.data, data);
}
}
/**
* @api private
*/
module.exports = {
buildRequest: buildRequest,
extractError: extractError,
extractData: extractData
};