-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapigClient.js
213 lines (162 loc) · 8.23 KB
/
apigClient.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
var apigClientFactory = {};
apigClientFactory.newClient = function (config) {
var apigClient = { };
if(config === undefined) {
config = {
accessKey: '',
secretKey: '',
sessionToken: '',
region: '',
apiKey: undefined,
defaultContentType: 'application/json',
defaultAcceptType: 'application/json'
};
}
if(config.accessKey === undefined) {
config.accessKey = '';
}
if(config.secretKey === undefined) {
config.secretKey = '';
}
if(config.apiKey === undefined) {
config.apiKey = '';
}
if(config.sessionToken === undefined) {
config.sessionToken = '';
}
if(config.region === undefined) {
config.region = 'us-east-1';
}
//If defaultContentType is not defined then default to application/json
if(config.defaultContentType === undefined) {
config.defaultContentType = 'application/json';
}
//If defaultAcceptType is not defined then default to application/json
if(config.defaultAcceptType === undefined) {
config.defaultAcceptType = 'application/json';
}
// extract endpoint and path from url
var invokeUrl = 'https://y3op33lkfd.execute-api.eu-central-1.amazonaws.com/PROD';
var endpoint = /(^https?:\/\/[^\/]+)/g.exec(invokeUrl)[1];
var pathComponent = invokeUrl.substring(endpoint.length);
var sigV4ClientConfig = {
accessKey: config.accessKey,
secretKey: config.secretKey,
sessionToken: config.sessionToken,
serviceName: 'execute-api',
region: config.region,
endpoint: endpoint,
defaultContentType: config.defaultContentType,
defaultAcceptType: config.defaultAcceptType
};
var authType = 'NONE';
if (sigV4ClientConfig.accessKey !== undefined && sigV4ClientConfig.accessKey !== '' && sigV4ClientConfig.secretKey !== undefined && sigV4ClientConfig.secretKey !== '') {
authType = 'AWS_IAM';
}
var simpleHttpClientConfig = {
endpoint: endpoint,
defaultContentType: config.defaultContentType,
defaultAcceptType: config.defaultAcceptType
};
var apiGatewayClient = apiGateway.core.apiGatewayClientFactory.newClient(simpleHttpClientConfig, sigV4ClientConfig);
apigClient.inPost = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, ['sectoken'], ['body']);
var inPostRequest = {
verb: 'post'.toUpperCase(),
path: pathComponent + uritemplate('/in').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, ['sectoken']),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(inPostRequest, authType, additionalParams, config.apiKey);
};
apigClient.inOptions = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, ['sectoken'], ['body']);
var inOptionsRequest = {
verb: 'options'.toUpperCase(),
path: pathComponent + uritemplate('/in').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, ['sectoken']),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(inOptionsRequest, authType, additionalParams, config.apiKey);
};
apigClient.outGet = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, ['sectoken'], ['body']);
var outGetRequest = {
verb: 'get'.toUpperCase(),
path: pathComponent + uritemplate('/out').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, ['sectoken']),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(outGetRequest, authType, additionalParams, config.apiKey);
};
apigClient.outOptions = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, ['sectoken'], ['body']);
var outOptionsRequest = {
verb: 'options'.toUpperCase(),
path: pathComponent + uritemplate('/out').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, ['sectoken']),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(outOptionsRequest, authType, additionalParams, config.apiKey);
};
apigClient.testGet = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, [], ['body']);
console.log(params);
var testGetRequest = {
verb: 'get'.toUpperCase(),
path: pathComponent + uritemplate('/test').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, []),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(testGetRequest, authType, additionalParams, config.apiKey);
};
apigClient.testPost = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, [], ['body']);
var testPostRequest = {
verb: 'post'.toUpperCase(),
path: pathComponent + uritemplate('/test').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, []),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(testPostRequest, authType, additionalParams, config.apiKey);
};
apigClient.testOptions = function (params, body, additionalParams) {
if(additionalParams === undefined) { additionalParams = {}; }
apiGateway.core.utils.assertParametersDefined(params, [], ['body']);
var testOptionsRequest = {
verb: 'options'.toUpperCase(),
path: pathComponent + uritemplate('/test').expand(apiGateway.core.utils.parseParametersToObject(params, [])),
headers: apiGateway.core.utils.parseParametersToObject(params, []),
queryParams: apiGateway.core.utils.parseParametersToObject(params, []),
body: body
};
return apiGatewayClient.makeRequest(testOptionsRequest, authType, additionalParams, config.apiKey);
};
return apigClient;
};