microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
lib/app-center-node-client/src/codepush/operations/deploymentReleases.js
308lines · modecode
| 1 | /* |
| 2 | * Code generated by Microsoft (R) AutoRest Code Generator. |
| 3 | * Changes may cause incorrect behavior and will be lost if the code is |
| 4 | * regenerated. |
| 5 | */ |
| 6 | |
| 7 | 'use strict'; |
| 8 | |
| 9 | const msRest = require('ms-rest'); |
| 10 | const WebResource = msRest.WebResource; |
| 11 | |
| 12 | /** |
| 13 | * Modifies a CodePush release metadata under the given Deployment |
| 14 | * |
| 15 | * @param {string} appName The name of the application |
| 16 | * |
| 17 | * @param {string} deploymentName deployment name |
| 18 | * |
| 19 | * @param {string} ownerName The name of the owner |
| 20 | * |
| 21 | * @param {object} release Release modification. All fields are optional and |
| 22 | * only provided fields will get updated. |
| 23 | * |
| 24 | * @param {string} [release.targetBinaryRange] |
| 25 | * |
| 26 | * @param {string} [release.description] |
| 27 | * |
| 28 | * @param {boolean} [release.isDisabled] |
| 29 | * |
| 30 | * @param {boolean} [release.isMandatory] |
| 31 | * |
| 32 | * @param {number} [release.rollout] |
| 33 | * |
| 34 | * @param {string} releaseLabel release label |
| 35 | * |
| 36 | * @param {object} [options] Optional Parameters. |
| 37 | * |
| 38 | * @param {object} [options.customHeaders] Headers that will be added to the |
| 39 | * request |
| 40 | * |
| 41 | * @param {function} callback - The callback. |
| 42 | * |
| 43 | * @returns {function} callback(err, result, request, response) |
| 44 | * |
| 45 | * {Error} err - The Error object if an error occurred, null otherwise. |
| 46 | * |
| 47 | * {object} [result] - The deserialized result object if an error did not occur. |
| 48 | * See {@link CodePushRelease} for more information. |
| 49 | * |
| 50 | * {object} [request] - The HTTP Request object if an error did not occur. |
| 51 | * |
| 52 | * {stream} [response] - The HTTP Response stream if an error did not occur. |
| 53 | */ |
| 54 | function _update(appName, deploymentName, ownerName, release, releaseLabel, options, callback) { |
| 55 | /* jshint validthis: true */ |
| 56 | let client = this.client; |
| 57 | if(!callback && typeof options === 'function') { |
| 58 | callback = options; |
| 59 | options = null; |
| 60 | } |
| 61 | if (!callback) { |
| 62 | throw new Error('callback cannot be null.'); |
| 63 | } |
| 64 | // Validate |
| 65 | try { |
| 66 | if (appName === null || appName === undefined || typeof appName.valueOf() !== 'string') { |
| 67 | throw new Error('appName cannot be null or undefined and it must be of type string.'); |
| 68 | } |
| 69 | if (deploymentName === null || deploymentName === undefined || typeof deploymentName.valueOf() !== 'string') { |
| 70 | throw new Error('deploymentName cannot be null or undefined and it must be of type string.'); |
| 71 | } |
| 72 | if (ownerName === null || ownerName === undefined || typeof ownerName.valueOf() !== 'string') { |
| 73 | throw new Error('ownerName cannot be null or undefined and it must be of type string.'); |
| 74 | } |
| 75 | if (release === null || release === undefined) { |
| 76 | throw new Error('release cannot be null or undefined.'); |
| 77 | } |
| 78 | if (releaseLabel === null || releaseLabel === undefined || typeof releaseLabel.valueOf() !== 'string') { |
| 79 | throw new Error('releaseLabel cannot be null or undefined and it must be of type string.'); |
| 80 | } |
| 81 | } catch (error) { |
| 82 | return callback(error); |
| 83 | } |
| 84 | |
| 85 | // Construct URL |
| 86 | let baseUrl = this.client.baseUri; |
| 87 | let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'v0.1/apps/{owner_name}/{app_name}/deployments/{deployment_name}/releases/{release_label}'; |
| 88 | requestUrl = requestUrl.replace('{app_name}', encodeURIComponent(appName)); |
| 89 | requestUrl = requestUrl.replace('{deployment_name}', encodeURIComponent(deploymentName)); |
| 90 | requestUrl = requestUrl.replace('{owner_name}', encodeURIComponent(ownerName)); |
| 91 | requestUrl = requestUrl.replace('{release_label}', encodeURIComponent(releaseLabel)); |
| 92 | |
| 93 | // Create HTTP transport objects |
| 94 | let httpRequest = new WebResource(); |
| 95 | httpRequest.method = 'PATCH'; |
| 96 | httpRequest.url = requestUrl; |
| 97 | httpRequest.headers = {}; |
| 98 | // Set Headers |
| 99 | httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; |
| 100 | if(options) { |
| 101 | for(let headerName in options['customHeaders']) { |
| 102 | if (options['customHeaders'].hasOwnProperty(headerName)) { |
| 103 | httpRequest.headers[headerName] = options['customHeaders'][headerName]; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | // Serialize Request |
| 108 | let requestContent = null; |
| 109 | let requestModel = null; |
| 110 | try { |
| 111 | if (release !== null && release !== undefined) { |
| 112 | let requestModelMapper = new client.models['CodePushReleaseModification']().mapper(); |
| 113 | requestModel = client.serialize(requestModelMapper, release, 'release'); |
| 114 | requestContent = JSON.stringify(requestModel); |
| 115 | } |
| 116 | } catch (error) { |
| 117 | let serializationError = new Error(`Error "${error.message}" occurred in serializing the ` + |
| 118 | `payload - ${JSON.stringify(release, null, 2)}.`); |
| 119 | return callback(serializationError); |
| 120 | } |
| 121 | httpRequest.body = requestContent; |
| 122 | // Send Request |
| 123 | return client.pipeline(httpRequest, (err, response, responseBody) => { |
| 124 | if (err) { |
| 125 | return callback(err); |
| 126 | } |
| 127 | let statusCode = response.statusCode; |
| 128 | if (statusCode !== 200) { |
| 129 | let error = new Error(responseBody); |
| 130 | error.statusCode = response.statusCode; |
| 131 | error.request = msRest.stripRequest(httpRequest); |
| 132 | error.response = msRest.stripResponse(response); |
| 133 | if (responseBody === '') responseBody = null; |
| 134 | let parsedErrorResponse; |
| 135 | try { |
| 136 | parsedErrorResponse = JSON.parse(responseBody); |
| 137 | if (parsedErrorResponse) { |
| 138 | let internalError = null; |
| 139 | if (parsedErrorResponse.error) internalError = parsedErrorResponse.error; |
| 140 | error.code = internalError ? internalError.code : parsedErrorResponse.code; |
| 141 | error.message = internalError ? internalError.message : parsedErrorResponse.message; |
| 142 | } |
| 143 | } catch (defaultError) { |
| 144 | error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` + |
| 145 | `- "${responseBody}" for the default response.`; |
| 146 | return callback(error); |
| 147 | } |
| 148 | return callback(error); |
| 149 | } |
| 150 | // Create Result |
| 151 | let result = null; |
| 152 | if (responseBody === '') responseBody = null; |
| 153 | // Deserialize Response |
| 154 | if (statusCode === 200) { |
| 155 | let parsedResponse = null; |
| 156 | try { |
| 157 | parsedResponse = JSON.parse(responseBody); |
| 158 | result = JSON.parse(responseBody); |
| 159 | if (parsedResponse !== null && parsedResponse !== undefined) { |
| 160 | let resultMapper = new client.models['CodePushRelease']().mapper(); |
| 161 | result = client.deserialize(resultMapper, parsedResponse, 'result'); |
| 162 | } |
| 163 | } catch (error) { |
| 164 | let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`); |
| 165 | deserializationError.request = msRest.stripRequest(httpRequest); |
| 166 | deserializationError.response = msRest.stripResponse(response); |
| 167 | return callback(deserializationError); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return callback(null, result, httpRequest, response); |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | /** Class representing a DeploymentReleases. */ |
| 176 | class DeploymentReleases { |
| 177 | /** |
| 178 | * Create a DeploymentReleases. |
| 179 | * @param {CodepushClient} client Reference to the service client. |
| 180 | */ |
| 181 | constructor(client) { |
| 182 | this.client = client; |
| 183 | this._update = _update; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Modifies a CodePush release metadata under the given Deployment |
| 188 | * |
| 189 | * @param {string} appName The name of the application |
| 190 | * |
| 191 | * @param {string} deploymentName deployment name |
| 192 | * |
| 193 | * @param {string} ownerName The name of the owner |
| 194 | * |
| 195 | * @param {object} release Release modification. All fields are optional and |
| 196 | * only provided fields will get updated. |
| 197 | * |
| 198 | * @param {string} [release.targetBinaryRange] |
| 199 | * |
| 200 | * @param {string} [release.description] |
| 201 | * |
| 202 | * @param {boolean} [release.isDisabled] |
| 203 | * |
| 204 | * @param {boolean} [release.isMandatory] |
| 205 | * |
| 206 | * @param {number} [release.rollout] |
| 207 | * |
| 208 | * @param {string} releaseLabel release label |
| 209 | * |
| 210 | * @param {object} [options] Optional Parameters. |
| 211 | * |
| 212 | * @param {object} [options.customHeaders] Headers that will be added to the |
| 213 | * request |
| 214 | * |
| 215 | * @returns {Promise} A promise is returned |
| 216 | * |
| 217 | * @resolve {HttpOperationResponse<CodePushRelease>} - The deserialized result object. |
| 218 | * |
| 219 | * @reject {Error} - The error object. |
| 220 | */ |
| 221 | updateWithHttpOperationResponse(appName, deploymentName, ownerName, release, releaseLabel, options) { |
| 222 | let client = this.client; |
| 223 | let self = this; |
| 224 | return new Promise((resolve, reject) => { |
| 225 | self._update(appName, deploymentName, ownerName, release, releaseLabel, options, (err, result, request, response) => { |
| 226 | let httpOperationResponse = new msRest.HttpOperationResponse(request, response); |
| 227 | httpOperationResponse.body = result; |
| 228 | if (err) { reject(err); } |
| 229 | else { resolve(httpOperationResponse); } |
| 230 | return; |
| 231 | }); |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Modifies a CodePush release metadata under the given Deployment |
| 237 | * |
| 238 | * @param {string} appName The name of the application |
| 239 | * |
| 240 | * @param {string} deploymentName deployment name |
| 241 | * |
| 242 | * @param {string} ownerName The name of the owner |
| 243 | * |
| 244 | * @param {object} release Release modification. All fields are optional and |
| 245 | * only provided fields will get updated. |
| 246 | * |
| 247 | * @param {string} [release.targetBinaryRange] |
| 248 | * |
| 249 | * @param {string} [release.description] |
| 250 | * |
| 251 | * @param {boolean} [release.isDisabled] |
| 252 | * |
| 253 | * @param {boolean} [release.isMandatory] |
| 254 | * |
| 255 | * @param {number} [release.rollout] |
| 256 | * |
| 257 | * @param {string} releaseLabel release label |
| 258 | * |
| 259 | * @param {object} [options] Optional Parameters. |
| 260 | * |
| 261 | * @param {object} [options.customHeaders] Headers that will be added to the |
| 262 | * request |
| 263 | * |
| 264 | * @param {function} [optionalCallback] - The optional callback. |
| 265 | * |
| 266 | * @returns {function|Promise} If a callback was passed as the last parameter |
| 267 | * then it returns the callback else returns a Promise. |
| 268 | * |
| 269 | * {Promise} A promise is returned |
| 270 | * |
| 271 | * @resolve {CodePushRelease} - The deserialized result object. |
| 272 | * |
| 273 | * @reject {Error} - The error object. |
| 274 | * |
| 275 | * {function} optionalCallback(err, result, request, response) |
| 276 | * |
| 277 | * {Error} err - The Error object if an error occurred, null otherwise. |
| 278 | * |
| 279 | * {object} [result] - The deserialized result object if an error did not occur. |
| 280 | * See {@link CodePushRelease} for more information. |
| 281 | * |
| 282 | * {object} [request] - The HTTP Request object if an error did not occur. |
| 283 | * |
| 284 | * {stream} [response] - The HTTP Response stream if an error did not occur. |
| 285 | */ |
| 286 | update(appName, deploymentName, ownerName, release, releaseLabel, options, optionalCallback) { |
| 287 | let client = this.client; |
| 288 | let self = this; |
| 289 | if (!optionalCallback && typeof options === 'function') { |
| 290 | optionalCallback = options; |
| 291 | options = null; |
| 292 | } |
| 293 | if (!optionalCallback) { |
| 294 | return new Promise((resolve, reject) => { |
| 295 | self._update(appName, deploymentName, ownerName, release, releaseLabel, options, (err, result, request, response) => { |
| 296 | if (err) { reject(err); } |
| 297 | else { resolve(result); } |
| 298 | return; |
| 299 | }); |
| 300 | }); |
| 301 | } else { |
| 302 | return self._update(appName, deploymentName, ownerName, release, releaseLabel, options, optionalCallback); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | } |
| 307 | |
| 308 | module.exports = DeploymentReleases; |
| 309 | |