rest_json.js
2.86 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
var util = require('../util');
var Rest = require('./rest');
var Json = require('./json');
var JsonBuilder = require('../json/builder');
var JsonParser = require('../json/parser');
function populateBody(req) {
var builder = new JsonBuilder();
var input = req.service.api.operations[req.operation].input;
if (input.payload) {
var params = {};
var payloadShape = input.members[input.payload];
params = req.params[input.payload];
if (params === undefined) return;
if (payloadShape.type === 'structure') {
req.httpRequest.body = builder.build(params, payloadShape);
applyContentTypeHeader(req);
} else { // non-JSON payload
req.httpRequest.body = params;
if (payloadShape.type === 'binary' || payloadShape.isStreaming) {
applyContentTypeHeader(req, true);
}
}
} else {
var body = builder.build(req.params, input);
if (body !== '{}' || req.httpRequest.method !== 'GET') { //don't send empty body for GET method
req.httpRequest.body = body;
}
applyContentTypeHeader(req);
}
}
function applyContentTypeHeader(req, isBinary) {
var operation = req.service.api.operations[req.operation];
var input = operation.input;
if (!req.httpRequest.headers['Content-Type']) {
var type = isBinary ? 'binary/octet-stream' : 'application/json';
req.httpRequest.headers['Content-Type'] = type;
}
}
function buildRequest(req) {
Rest.buildRequest(req);
// never send body payload on HEAD/DELETE
if (['HEAD', 'DELETE'].indexOf(req.httpRequest.method) < 0) {
populateBody(req);
}
}
function extractError(resp) {
Json.extractError(resp);
}
function extractData(resp) {
Rest.extractData(resp);
var req = resp.request;
var operation = req.service.api.operations[req.operation];
var rules = req.service.api.operations[req.operation].output || {};
var parser;
var hasEventOutput = operation.hasEventOutput;
if (rules.payload) {
var payloadMember = rules.members[rules.payload];
var body = resp.httpResponse.body;
if (payloadMember.isEventStream) {
parser = new JsonParser();
resp.data[payload] = util.createEventStream(
AWS.HttpClient.streamsApiVersion === 2 ? resp.httpResponse.stream : body,
parser,
payloadMember
);
} else if (payloadMember.type === 'structure' || payloadMember.type === 'list') {
var parser = new JsonParser();
resp.data[rules.payload] = parser.parse(body, payloadMember);
} else if (payloadMember.type === 'binary' || payloadMember.isStreaming) {
resp.data[rules.payload] = body;
} else {
resp.data[rules.payload] = payloadMember.toType(body);
}
} else {
var data = resp.data;
Json.extractData(resp);
resp.data = util.merge(data, resp.data);
}
}
/**
* @api private
*/
module.exports = {
buildRequest: buildRequest,
extractError: extractError,
extractData: extractData
};