|
| 1 | +/* eslint-disable */ |
| 2 | +/* |
| 3 | + * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * A copy of the License is located at |
| 8 | + * |
| 9 | + * http://aws.amazon.com/apache2.0 |
| 10 | + * |
| 11 | + * or in the "license" file accompanying this file. This file is distributed |
| 12 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 13 | + * express or implied. See the License for the specific language governing |
| 14 | + * permissions and limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +var apigClientFactory = {}; |
| 18 | +apigClientFactory.newClient = function (config) { |
| 19 | + var apigClient = { }; |
| 20 | + if(config === undefined) { |
| 21 | + config = { |
| 22 | + accessKey: '', |
| 23 | + secretKey: '', |
| 24 | + sessionToken: '', |
| 25 | + region: '', |
| 26 | + apiKey: undefined, |
| 27 | + defaultContentType: 'application/json', |
| 28 | + defaultAcceptType: 'application/json' |
| 29 | + }; |
| 30 | + } |
| 31 | + if(config.accessKey === undefined) { |
| 32 | + config.accessKey = ''; |
| 33 | + } |
| 34 | + if(config.secretKey === undefined) { |
| 35 | + config.secretKey = ''; |
| 36 | + } |
| 37 | + if(config.apiKey === undefined) { |
| 38 | + config.apiKey = ''; |
| 39 | + } |
| 40 | + if(config.sessionToken === undefined) { |
| 41 | + config.sessionToken = ''; |
| 42 | + } |
| 43 | + if(config.region === undefined) { |
| 44 | + config.region = 'YOUR_PRIMARY_AWS_REGION'; |
| 45 | + } |
| 46 | + //If defaultContentType is not defined then default to application/json |
| 47 | + if(config.defaultContentType === undefined) { |
| 48 | + config.defaultContentType = 'application/json'; |
| 49 | + } |
| 50 | + //If defaultAcceptType is not defined then default to application/json |
| 51 | + if(config.defaultAcceptType === undefined) { |
| 52 | + config.defaultAcceptType = 'application/json'; |
| 53 | + } |
| 54 | + |
| 55 | + // extract endpoint and path from url |
| 56 | + let invokeUrl = `https://${window.config.restApiId}.execute-api.${window.config.region}.amazonaws.com/prod`, |
| 57 | + endpoint = /(^https?:\/\/[^\/]+)/g.exec(invokeUrl)[1], |
| 58 | + pathComponent = invokeUrl.substring(endpoint.length) |
| 59 | + |
| 60 | + var sigV4ClientConfig = { |
| 61 | + accessKey: config.accessKey, |
| 62 | + secretKey: config.secretKey, |
| 63 | + sessionToken: config.sessionToken, |
| 64 | + serviceName: 'execute-api', |
| 65 | + region: config.region, |
| 66 | + endpoint: endpoint, |
| 67 | + defaultContentType: config.defaultContentType, |
| 68 | + defaultAcceptType: config.defaultAcceptType |
| 69 | + }; |
| 70 | + |
| 71 | + var authType = 'NONE'; |
| 72 | + if (sigV4ClientConfig.accessKey !== undefined && sigV4ClientConfig.accessKey !== '' && sigV4ClientConfig.secretKey !== undefined && sigV4ClientConfig.secretKey !== '') { |
| 73 | + authType = 'AWS_IAM'; |
| 74 | + } |
| 75 | + |
| 76 | + var simpleHttpClientConfig = { |
| 77 | + endpoint: endpoint, |
| 78 | + defaultContentType: config.defaultContentType, |
| 79 | + defaultAcceptType: config.defaultAcceptType |
| 80 | + }; |
| 81 | + |
| 82 | + var apiGatewayClient = apiGateway.core.apiGatewayClientFactory.newClient(simpleHttpClientConfig, sigV4ClientConfig); |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + apigClient.rootOptions = function (params, body, additionalParams) { |
| 87 | + if(additionalParams === undefined) { additionalParams = {}; } |
| 88 | + |
| 89 | + apiGateway.core.utils.assertParametersDefined(params, [], ['body']); |
| 90 | + |
| 91 | + var rootOptionsRequest = { |
| 92 | + verb: 'options'.toUpperCase(), |
| 93 | + path: pathComponent + uritemplate('/').expand(apiGateway.core.utils.parseParametersToObject(params, [])), |
| 94 | + headers: apiGateway.core.utils.parseParametersToObject(params, []), |
| 95 | + queryParams: apiGateway.core.utils.parseParametersToObject(params, []), |
| 96 | + body: body |
| 97 | + }; |
| 98 | + |
| 99 | + |
| 100 | + return apiGatewayClient.makeRequest(rootOptionsRequest, authType, additionalParams, config.apiKey); |
| 101 | + }; |
| 102 | + |
| 103 | + |
| 104 | + apigClient.get = function (path, params, body, additionalParams) { |
| 105 | + if(additionalParams === undefined) { additionalParams = {}; } |
| 106 | + |
| 107 | + apiGateway.core.utils.assertParametersDefined(params, [], ['body']); |
| 108 | + |
| 109 | + var proxyOptionsRequest = { |
| 110 | + verb: 'GET', |
| 111 | + path: pathComponent + path, |
| 112 | + headers: apiGateway.core.utils.parseParametersToObject(params, []), |
| 113 | + queryParams: apiGateway.core.utils.parseParametersToObject(params, ['start', 'end', 'sdkType']), |
| 114 | + body: body |
| 115 | + }; |
| 116 | + |
| 117 | + return apiGatewayClient.makeRequest(proxyOptionsRequest, authType, additionalParams, config.apiKey); |
| 118 | + }; |
| 119 | + |
| 120 | + apigClient.post = function (path, params, body, additionalParams) { |
| 121 | + if(additionalParams === undefined) { additionalParams = {}; } |
| 122 | + |
| 123 | + apiGateway.core.utils.assertParametersDefined(params, [], ['body']); |
| 124 | + |
| 125 | + var proxyOptionsRequest = { |
| 126 | + verb: 'POST', |
| 127 | + path: pathComponent + path, |
| 128 | + headers: apiGateway.core.utils.parseParametersToObject(params, []), |
| 129 | + queryParams: apiGateway.core.utils.parseParametersToObject(params, []), |
| 130 | + body: body |
| 131 | + }; |
| 132 | + |
| 133 | + |
| 134 | + return apiGatewayClient.makeRequest(proxyOptionsRequest, authType, additionalParams, config.apiKey); |
| 135 | + }; |
| 136 | + |
| 137 | + apigClient.put = function (path, params, body, additionalParams) { |
| 138 | + if(additionalParams === undefined) { additionalParams = {}; } |
| 139 | + |
| 140 | + apiGateway.core.utils.assertParametersDefined(params, [], ['body']); |
| 141 | + |
| 142 | + var proxyOptionsRequest = { |
| 143 | + verb: 'PUT', |
| 144 | + path: pathComponent + path, |
| 145 | + headers: apiGateway.core.utils.parseParametersToObject(params, []), |
| 146 | + queryParams: apiGateway.core.utils.parseParametersToObject(params, []), |
| 147 | + body: body |
| 148 | + }; |
| 149 | + |
| 150 | + |
| 151 | + return apiGatewayClient.makeRequest(proxyOptionsRequest, authType, additionalParams, config.apiKey); |
| 152 | + }; |
| 153 | + |
| 154 | + apigClient.delete = function (path, params, body, additionalParams) { |
| 155 | + if(additionalParams === undefined) { additionalParams = {}; } |
| 156 | + |
| 157 | + apiGateway.core.utils.assertParametersDefined(params, [], ['body']); |
| 158 | + |
| 159 | + var proxyOptionsRequest = { |
| 160 | + verb: 'DELETE', |
| 161 | + path: pathComponent + path, |
| 162 | + headers: apiGateway.core.utils.parseParametersToObject(params, []), |
| 163 | + queryParams: apiGateway.core.utils.parseParametersToObject(params, []), |
| 164 | + body: body |
| 165 | + }; |
| 166 | + |
| 167 | + |
| 168 | + return apiGatewayClient.makeRequest(proxyOptionsRequest, authType, additionalParams, config.apiKey); |
| 169 | + }; |
| 170 | + |
| 171 | + |
| 172 | + return apigClient; |
| 173 | +}; |
0 commit comments