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