microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
lib/app-center-node-client/src/codepush/operations/codePushDeploymentRelease.js
272lines · 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 | * Rollback the latest or a specific release for an app 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} [options] Optional Parameters. |
| 22 | * |
| 23 | * @param {object} [options.releaseLabel] The specific release label that you |
| 24 | * want to rollback to |
| 25 | * |
| 26 | * @param {string} [options.releaseLabel.label] |
| 27 | * |
| 28 | * @param {object} [options.customHeaders] Headers that will be added to the |
| 29 | * request |
| 30 | * |
| 31 | * @param {function} callback - The callback. |
| 32 | * |
| 33 | * @returns {function} callback(err, result, request, response) |
| 34 | * |
| 35 | * {Error} err - The Error object if an error occurred, null otherwise. |
| 36 | * |
| 37 | * {object} [result] - The deserialized result object if an error did not occur. |
| 38 | * See {@link CodePushRelease} for more information. |
| 39 | * |
| 40 | * {object} [request] - The HTTP Request object if an error did not occur. |
| 41 | * |
| 42 | * {stream} [response] - The HTTP Response stream if an error did not occur. |
| 43 | */ |
| 44 | function _rollback(appName, deploymentName, ownerName, options, callback) { |
| 45 | /* jshint validthis: true */ |
| 46 | let client = this.client; |
| 47 | if(!callback && typeof options === 'function') { |
| 48 | callback = options; |
| 49 | options = null; |
| 50 | } |
| 51 | if (!callback) { |
| 52 | throw new Error('callback cannot be null.'); |
| 53 | } |
| 54 | let releaseLabel = (options && options.releaseLabel !== undefined) ? options.releaseLabel : undefined; |
| 55 | // Validate |
| 56 | try { |
| 57 | if (appName === null || appName === undefined || typeof appName.valueOf() !== 'string') { |
| 58 | throw new Error('appName cannot be null or undefined and it must be of type string.'); |
| 59 | } |
| 60 | if (deploymentName === null || deploymentName === undefined || typeof deploymentName.valueOf() !== 'string') { |
| 61 | throw new Error('deploymentName cannot be null or undefined and it must be of type string.'); |
| 62 | } |
| 63 | if (ownerName === null || ownerName === undefined || typeof ownerName.valueOf() !== 'string') { |
| 64 | throw new Error('ownerName cannot be null or undefined and it must be of type string.'); |
| 65 | } |
| 66 | } catch (error) { |
| 67 | return callback(error); |
| 68 | } |
| 69 | |
| 70 | // Construct URL |
| 71 | let baseUrl = this.client.baseUri; |
| 72 | let requestUrl = baseUrl + (baseUrl.endsWith('/') ? '' : '/') + 'v0.1/apps/{owner_name}/{app_name}/deployments/{deployment_name}/rollback_release'; |
| 73 | requestUrl = requestUrl.replace('{app_name}', encodeURIComponent(appName)); |
| 74 | requestUrl = requestUrl.replace('{deployment_name}', encodeURIComponent(deploymentName)); |
| 75 | requestUrl = requestUrl.replace('{owner_name}', encodeURIComponent(ownerName)); |
| 76 | |
| 77 | // Create HTTP transport objects |
| 78 | let httpRequest = new WebResource(); |
| 79 | httpRequest.method = 'POST'; |
| 80 | httpRequest.url = requestUrl; |
| 81 | httpRequest.headers = {}; |
| 82 | // Set Headers |
| 83 | httpRequest.headers['Content-Type'] = 'application/json; charset=utf-8'; |
| 84 | if(options) { |
| 85 | for(let headerName in options['customHeaders']) { |
| 86 | if (options['customHeaders'].hasOwnProperty(headerName)) { |
| 87 | httpRequest.headers[headerName] = options['customHeaders'][headerName]; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | // Serialize Request |
| 92 | let requestContent = null; |
| 93 | let requestModel = null; |
| 94 | try { |
| 95 | if (releaseLabel !== null && releaseLabel !== undefined) { |
| 96 | let requestModelMapper = new client.models['CodePushReleaseLabel']().mapper(); |
| 97 | requestModel = client.serialize(requestModelMapper, releaseLabel, 'releaseLabel'); |
| 98 | requestContent = JSON.stringify(requestModel); |
| 99 | } |
| 100 | } catch (error) { |
| 101 | let serializationError = new Error(`Error "${error.message}" occurred in serializing the ` + |
| 102 | `payload - ${JSON.stringify(releaseLabel, null, 2)}.`); |
| 103 | return callback(serializationError); |
| 104 | } |
| 105 | httpRequest.body = requestContent; |
| 106 | // Send Request |
| 107 | return client.pipeline(httpRequest, (err, response, responseBody) => { |
| 108 | if (err) { |
| 109 | return callback(err); |
| 110 | } |
| 111 | let statusCode = response.statusCode; |
| 112 | if (statusCode !== 201) { |
| 113 | let error = new Error(responseBody); |
| 114 | error.statusCode = response.statusCode; |
| 115 | error.request = msRest.stripRequest(httpRequest); |
| 116 | error.response = msRest.stripResponse(response); |
| 117 | if (responseBody === '') responseBody = null; |
| 118 | let parsedErrorResponse; |
| 119 | try { |
| 120 | parsedErrorResponse = JSON.parse(responseBody); |
| 121 | if (parsedErrorResponse) { |
| 122 | let internalError = null; |
| 123 | if (parsedErrorResponse.error) internalError = parsedErrorResponse.error; |
| 124 | error.code = internalError ? internalError.code : parsedErrorResponse.code; |
| 125 | error.message = internalError ? internalError.message : parsedErrorResponse.message; |
| 126 | } |
| 127 | } catch (defaultError) { |
| 128 | error.message = `Error "${defaultError.message}" occurred in deserializing the responseBody ` + |
| 129 | `- "${responseBody}" for the default response.`; |
| 130 | return callback(error); |
| 131 | } |
| 132 | return callback(error); |
| 133 | } |
| 134 | // Create Result |
| 135 | let result = null; |
| 136 | if (responseBody === '') responseBody = null; |
| 137 | // Deserialize Response |
| 138 | if (statusCode === 201) { |
| 139 | let parsedResponse = null; |
| 140 | try { |
| 141 | parsedResponse = JSON.parse(responseBody); |
| 142 | result = JSON.parse(responseBody); |
| 143 | if (parsedResponse !== null && parsedResponse !== undefined) { |
| 144 | let resultMapper = new client.models['CodePushRelease']().mapper(); |
| 145 | result = client.deserialize(resultMapper, parsedResponse, 'result'); |
| 146 | } |
| 147 | } catch (error) { |
| 148 | let deserializationError = new Error(`Error ${error} occurred in deserializing the responseBody - ${responseBody}`); |
| 149 | deserializationError.request = msRest.stripRequest(httpRequest); |
| 150 | deserializationError.response = msRest.stripResponse(response); |
| 151 | return callback(deserializationError); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return callback(null, result, httpRequest, response); |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | /** Class representing a CodePushDeploymentRelease. */ |
| 160 | class CodePushDeploymentRelease { |
| 161 | /** |
| 162 | * Create a CodePushDeploymentRelease. |
| 163 | * @param {CodepushClient} client Reference to the service client. |
| 164 | */ |
| 165 | constructor(client) { |
| 166 | this.client = client; |
| 167 | this._rollback = _rollback; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Rollback the latest or a specific release for an app deployment |
| 172 | * |
| 173 | * @param {string} appName The name of the application |
| 174 | * |
| 175 | * @param {string} deploymentName deployment name |
| 176 | * |
| 177 | * @param {string} ownerName The name of the owner |
| 178 | * |
| 179 | * @param {object} [options] Optional Parameters. |
| 180 | * |
| 181 | * @param {object} [options.releaseLabel] The specific release label that you |
| 182 | * want to rollback to |
| 183 | * |
| 184 | * @param {string} [options.releaseLabel.label] |
| 185 | * |
| 186 | * @param {object} [options.customHeaders] Headers that will be added to the |
| 187 | * request |
| 188 | * |
| 189 | * @returns {Promise} A promise is returned |
| 190 | * |
| 191 | * @resolve {HttpOperationResponse<CodePushRelease>} - The deserialized result object. |
| 192 | * |
| 193 | * @reject {Error} - The error object. |
| 194 | */ |
| 195 | rollbackWithHttpOperationResponse(appName, deploymentName, ownerName, options) { |
| 196 | let client = this.client; |
| 197 | let self = this; |
| 198 | return new Promise((resolve, reject) => { |
| 199 | self._rollback(appName, deploymentName, ownerName, options, (err, result, request, response) => { |
| 200 | let httpOperationResponse = new msRest.HttpOperationResponse(request, response); |
| 201 | httpOperationResponse.body = result; |
| 202 | if (err) { reject(err); } |
| 203 | else { resolve(httpOperationResponse); } |
| 204 | return; |
| 205 | }); |
| 206 | }); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Rollback the latest or a specific release for an app deployment |
| 211 | * |
| 212 | * @param {string} appName The name of the application |
| 213 | * |
| 214 | * @param {string} deploymentName deployment name |
| 215 | * |
| 216 | * @param {string} ownerName The name of the owner |
| 217 | * |
| 218 | * @param {object} [options] Optional Parameters. |
| 219 | * |
| 220 | * @param {object} [options.releaseLabel] The specific release label that you |
| 221 | * want to rollback to |
| 222 | * |
| 223 | * @param {string} [options.releaseLabel.label] |
| 224 | * |
| 225 | * @param {object} [options.customHeaders] Headers that will be added to the |
| 226 | * request |
| 227 | * |
| 228 | * @param {function} [optionalCallback] - The optional callback. |
| 229 | * |
| 230 | * @returns {function|Promise} If a callback was passed as the last parameter |
| 231 | * then it returns the callback else returns a Promise. |
| 232 | * |
| 233 | * {Promise} A promise is returned |
| 234 | * |
| 235 | * @resolve {CodePushRelease} - The deserialized result object. |
| 236 | * |
| 237 | * @reject {Error} - The error object. |
| 238 | * |
| 239 | * {function} optionalCallback(err, result, request, response) |
| 240 | * |
| 241 | * {Error} err - The Error object if an error occurred, null otherwise. |
| 242 | * |
| 243 | * {object} [result] - The deserialized result object if an error did not occur. |
| 244 | * See {@link CodePushRelease} for more information. |
| 245 | * |
| 246 | * {object} [request] - The HTTP Request object if an error did not occur. |
| 247 | * |
| 248 | * {stream} [response] - The HTTP Response stream if an error did not occur. |
| 249 | */ |
| 250 | rollback(appName, deploymentName, ownerName, options, optionalCallback) { |
| 251 | let client = this.client; |
| 252 | let self = this; |
| 253 | if (!optionalCallback && typeof options === 'function') { |
| 254 | optionalCallback = options; |
| 255 | options = null; |
| 256 | } |
| 257 | if (!optionalCallback) { |
| 258 | return new Promise((resolve, reject) => { |
| 259 | self._rollback(appName, deploymentName, ownerName, options, (err, result, request, response) => { |
| 260 | if (err) { reject(err); } |
| 261 | else { resolve(result); } |
| 262 | return; |
| 263 | }); |
| 264 | }); |
| 265 | } else { |
| 266 | return self._rollback(appName, deploymentName, ownerName, options, optionalCallback); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | module.exports = CodePushDeploymentRelease; |