@azure/core-client
Advanced tools
Comparing version 1.1.3-alpha.20210521.1 to 1.1.3-alpha.20210610.1
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { __awaiter } from "tslib"; | ||
import { RestError } from "@azure/core-rest-pipeline"; | ||
@@ -32,7 +31,5 @@ import { XML_CHARKEY } from "./interfaces"; | ||
name: deserializationPolicyName, | ||
sendRequest(request, next) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield next(request); | ||
return deserializeResponseBody(jsonContentTypes, xmlContentTypes, response, updatedOptions, parseXML); | ||
}); | ||
async sendRequest(request, next) { | ||
const response = await next(request); | ||
return deserializeResponseBody(jsonContentTypes, xmlContentTypes, response, updatedOptions, parseXML); | ||
} | ||
@@ -72,54 +69,52 @@ }; | ||
} | ||
function deserializeResponseBody(jsonContentTypes, xmlContentTypes, response, options, parseXML) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const parsedResponse = yield parse(jsonContentTypes, xmlContentTypes, response, options, parseXML); | ||
if (!shouldDeserializeResponse(parsedResponse)) { | ||
return parsedResponse; | ||
} | ||
const operationInfo = getOperationRequestInfo(parsedResponse.request); | ||
const operationSpec = operationInfo === null || operationInfo === void 0 ? void 0 : operationInfo.operationSpec; | ||
if (!operationSpec || !operationSpec.responses) { | ||
return parsedResponse; | ||
} | ||
const responseSpec = getOperationResponseMap(parsedResponse); | ||
const { error, shouldReturnResponse } = handleErrorResponse(parsedResponse, operationSpec, responseSpec); | ||
if (error) { | ||
throw error; | ||
} | ||
else if (shouldReturnResponse) { | ||
return parsedResponse; | ||
} | ||
// An operation response spec does exist for current status code, so | ||
// use it to deserialize the response. | ||
if (responseSpec) { | ||
if (responseSpec.bodyMapper) { | ||
let valueToDeserialize = parsedResponse.parsedBody; | ||
if (operationSpec.isXML && responseSpec.bodyMapper.type.name === MapperTypeNames.Sequence) { | ||
valueToDeserialize = | ||
typeof valueToDeserialize === "object" | ||
? valueToDeserialize[responseSpec.bodyMapper.xmlElementName] | ||
: []; | ||
} | ||
try { | ||
parsedResponse.parsedBody = operationSpec.serializer.deserialize(responseSpec.bodyMapper, valueToDeserialize, "operationRes.parsedBody"); | ||
} | ||
catch (deserializeError) { | ||
const restError = new RestError(`Error ${deserializeError} occurred in deserializing the responseBody - ${parsedResponse.bodyAsText}`, { | ||
statusCode: parsedResponse.status, | ||
request: parsedResponse.request, | ||
response: parsedResponse | ||
}); | ||
throw restError; | ||
} | ||
async function deserializeResponseBody(jsonContentTypes, xmlContentTypes, response, options, parseXML) { | ||
const parsedResponse = await parse(jsonContentTypes, xmlContentTypes, response, options, parseXML); | ||
if (!shouldDeserializeResponse(parsedResponse)) { | ||
return parsedResponse; | ||
} | ||
const operationInfo = getOperationRequestInfo(parsedResponse.request); | ||
const operationSpec = operationInfo === null || operationInfo === void 0 ? void 0 : operationInfo.operationSpec; | ||
if (!operationSpec || !operationSpec.responses) { | ||
return parsedResponse; | ||
} | ||
const responseSpec = getOperationResponseMap(parsedResponse); | ||
const { error, shouldReturnResponse } = handleErrorResponse(parsedResponse, operationSpec, responseSpec); | ||
if (error) { | ||
throw error; | ||
} | ||
else if (shouldReturnResponse) { | ||
return parsedResponse; | ||
} | ||
// An operation response spec does exist for current status code, so | ||
// use it to deserialize the response. | ||
if (responseSpec) { | ||
if (responseSpec.bodyMapper) { | ||
let valueToDeserialize = parsedResponse.parsedBody; | ||
if (operationSpec.isXML && responseSpec.bodyMapper.type.name === MapperTypeNames.Sequence) { | ||
valueToDeserialize = | ||
typeof valueToDeserialize === "object" | ||
? valueToDeserialize[responseSpec.bodyMapper.xmlElementName] | ||
: []; | ||
} | ||
else if (operationSpec.httpMethod === "HEAD") { | ||
// head methods never have a body, but we return a boolean to indicate presence/absence of the resource | ||
parsedResponse.parsedBody = response.status >= 200 && response.status < 300; | ||
try { | ||
parsedResponse.parsedBody = operationSpec.serializer.deserialize(responseSpec.bodyMapper, valueToDeserialize, "operationRes.parsedBody"); | ||
} | ||
if (responseSpec.headersMapper) { | ||
parsedResponse.parsedHeaders = operationSpec.serializer.deserialize(responseSpec.headersMapper, parsedResponse.headers.toJSON(), "operationRes.parsedHeaders"); | ||
catch (deserializeError) { | ||
const restError = new RestError(`Error ${deserializeError} occurred in deserializing the responseBody - ${parsedResponse.bodyAsText}`, { | ||
statusCode: parsedResponse.status, | ||
request: parsedResponse.request, | ||
response: parsedResponse | ||
}); | ||
throw restError; | ||
} | ||
} | ||
return parsedResponse; | ||
}); | ||
else if (operationSpec.httpMethod === "HEAD") { | ||
// head methods never have a body, but we return a boolean to indicate presence/absence of the resource | ||
parsedResponse.parsedBody = response.status >= 200 && response.status < 300; | ||
} | ||
if (responseSpec.headersMapper) { | ||
parsedResponse.parsedHeaders = operationSpec.serializer.deserialize(responseSpec.headersMapper, parsedResponse.headers.toJSON(), "operationRes.parsedHeaders"); | ||
} | ||
} | ||
return parsedResponse; | ||
} | ||
@@ -199,42 +194,40 @@ function isOperationSpecEmpty(operationSpec) { | ||
} | ||
function parse(jsonContentTypes, xmlContentTypes, operationResponse, opts, parseXML) { | ||
async function parse(jsonContentTypes, xmlContentTypes, operationResponse, opts, parseXML) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!((_a = operationResponse.request.streamResponseStatusCodes) === null || _a === void 0 ? void 0 : _a.has(operationResponse.status)) && | ||
operationResponse.bodyAsText) { | ||
const text = operationResponse.bodyAsText; | ||
const contentType = operationResponse.headers.get("Content-Type") || ""; | ||
const contentComponents = !contentType | ||
? [] | ||
: contentType.split(";").map((component) => component.toLowerCase()); | ||
try { | ||
if (contentComponents.length === 0 || | ||
contentComponents.some((component) => jsonContentTypes.indexOf(component) !== -1)) { | ||
operationResponse.parsedBody = JSON.parse(text); | ||
return operationResponse; | ||
if (!((_a = operationResponse.request.streamResponseStatusCodes) === null || _a === void 0 ? void 0 : _a.has(operationResponse.status)) && | ||
operationResponse.bodyAsText) { | ||
const text = operationResponse.bodyAsText; | ||
const contentType = operationResponse.headers.get("Content-Type") || ""; | ||
const contentComponents = !contentType | ||
? [] | ||
: contentType.split(";").map((component) => component.toLowerCase()); | ||
try { | ||
if (contentComponents.length === 0 || | ||
contentComponents.some((component) => jsonContentTypes.indexOf(component) !== -1)) { | ||
operationResponse.parsedBody = JSON.parse(text); | ||
return operationResponse; | ||
} | ||
else if (contentComponents.some((component) => xmlContentTypes.indexOf(component) !== -1)) { | ||
if (!parseXML) { | ||
throw new Error("Parsing XML not supported."); | ||
} | ||
else if (contentComponents.some((component) => xmlContentTypes.indexOf(component) !== -1)) { | ||
if (!parseXML) { | ||
throw new Error("Parsing XML not supported."); | ||
} | ||
const body = yield parseXML(text, opts.xml); | ||
operationResponse.parsedBody = body; | ||
return operationResponse; | ||
} | ||
const body = await parseXML(text, opts.xml); | ||
operationResponse.parsedBody = body; | ||
return operationResponse; | ||
} | ||
catch (err) { | ||
const msg = `Error "${err}" occurred while parsing the response body - ${operationResponse.bodyAsText}.`; | ||
const errCode = err.code || RestError.PARSE_ERROR; | ||
const e = new RestError(msg, { | ||
code: errCode, | ||
statusCode: operationResponse.status, | ||
request: operationResponse.request, | ||
response: operationResponse | ||
}); | ||
throw e; | ||
} | ||
} | ||
return operationResponse; | ||
}); | ||
catch (err) { | ||
const msg = `Error "${err}" occurred while parsing the response body - ${operationResponse.bodyAsText}.`; | ||
const errCode = err.code || RestError.PARSE_ERROR; | ||
const e = new RestError(msg, { | ||
code: errCode, | ||
statusCode: operationResponse.status, | ||
request: operationResponse.request, | ||
response: operationResponse | ||
}); | ||
throw e; | ||
} | ||
} | ||
return operationResponse; | ||
} | ||
//# sourceMappingURL=deserializationPolicy.js.map |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { __awaiter } from "tslib"; | ||
import { XML_CHARKEY, XML_ATTRKEY } from "./interfaces"; | ||
@@ -20,13 +19,11 @@ import { MapperTypeNames } from "./serializer"; | ||
name: serializationPolicyName, | ||
sendRequest(request, next) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const operationInfo = getOperationRequestInfo(request); | ||
const operationSpec = operationInfo === null || operationInfo === void 0 ? void 0 : operationInfo.operationSpec; | ||
const operationArguments = operationInfo === null || operationInfo === void 0 ? void 0 : operationInfo.operationArguments; | ||
if (operationSpec && operationArguments) { | ||
serializeHeaders(request, operationArguments, operationSpec); | ||
serializeRequestBody(request, operationArguments, operationSpec, stringifyXML); | ||
} | ||
return next(request); | ||
}); | ||
async sendRequest(request, next) { | ||
const operationInfo = getOperationRequestInfo(request); | ||
const operationSpec = operationInfo === null || operationInfo === void 0 ? void 0 : operationInfo.operationSpec; | ||
const operationArguments = operationInfo === null || operationInfo === void 0 ? void 0 : operationInfo.operationArguments; | ||
if (operationSpec && operationArguments) { | ||
serializeHeaders(request, operationArguments, operationSpec); | ||
serializeRequestBody(request, operationArguments, operationSpec, stringifyXML); | ||
} | ||
return next(request); | ||
} | ||
@@ -33,0 +30,0 @@ }; |
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
import { __awaiter } from "tslib"; | ||
import { createPipelineRequest } from "@azure/core-rest-pipeline"; | ||
@@ -31,6 +30,4 @@ import { getStreamingResponseStatusCodes } from "./interfaceHelpers"; | ||
*/ | ||
sendRequest(request) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.pipeline.sendRequest(this._httpClient, request); | ||
}); | ||
async sendRequest(request) { | ||
return this.pipeline.sendRequest(this._httpClient, request); | ||
} | ||
@@ -43,71 +40,69 @@ /** | ||
*/ | ||
sendOperationRequest(operationArguments, operationSpec) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const baseUri = operationSpec.baseUrl || this._baseUri; | ||
if (!baseUri) { | ||
throw new Error("If operationSpec.baseUrl is not specified, then the ServiceClient must have a baseUri string property that contains the base URL to use."); | ||
} | ||
// Templatized URLs sometimes reference properties on the ServiceClient child class, | ||
// so we have to pass `this` below in order to search these properties if they're | ||
// not part of OperationArguments | ||
const url = getRequestUrl(baseUri, operationSpec, operationArguments, this); | ||
const request = createPipelineRequest({ | ||
url | ||
}); | ||
request.method = operationSpec.httpMethod; | ||
const operationInfo = getOperationRequestInfo(request); | ||
operationInfo.operationSpec = operationSpec; | ||
operationInfo.operationArguments = operationArguments; | ||
const contentType = operationSpec.contentType || this._requestContentType; | ||
if (contentType && operationSpec.requestBody) { | ||
request.headers.set("Content-Type", contentType); | ||
} | ||
const options = operationArguments.options; | ||
if (options) { | ||
const requestOptions = options.requestOptions; | ||
if (requestOptions) { | ||
if (requestOptions.timeout) { | ||
request.timeout = requestOptions.timeout; | ||
} | ||
if (requestOptions.onUploadProgress) { | ||
request.onUploadProgress = requestOptions.onUploadProgress; | ||
} | ||
if (requestOptions.onDownloadProgress) { | ||
request.onDownloadProgress = requestOptions.onDownloadProgress; | ||
} | ||
if (requestOptions.shouldDeserialize !== undefined) { | ||
operationInfo.shouldDeserialize = requestOptions.shouldDeserialize; | ||
} | ||
if (requestOptions.allowInsecureConnection) { | ||
request.allowInsecureConnection = true; | ||
} | ||
async sendOperationRequest(operationArguments, operationSpec) { | ||
const baseUri = operationSpec.baseUrl || this._baseUri; | ||
if (!baseUri) { | ||
throw new Error("If operationSpec.baseUrl is not specified, then the ServiceClient must have a baseUri string property that contains the base URL to use."); | ||
} | ||
// Templatized URLs sometimes reference properties on the ServiceClient child class, | ||
// so we have to pass `this` below in order to search these properties if they're | ||
// not part of OperationArguments | ||
const url = getRequestUrl(baseUri, operationSpec, operationArguments, this); | ||
const request = createPipelineRequest({ | ||
url | ||
}); | ||
request.method = operationSpec.httpMethod; | ||
const operationInfo = getOperationRequestInfo(request); | ||
operationInfo.operationSpec = operationSpec; | ||
operationInfo.operationArguments = operationArguments; | ||
const contentType = operationSpec.contentType || this._requestContentType; | ||
if (contentType && operationSpec.requestBody) { | ||
request.headers.set("Content-Type", contentType); | ||
} | ||
const options = operationArguments.options; | ||
if (options) { | ||
const requestOptions = options.requestOptions; | ||
if (requestOptions) { | ||
if (requestOptions.timeout) { | ||
request.timeout = requestOptions.timeout; | ||
} | ||
if (options.abortSignal) { | ||
request.abortSignal = options.abortSignal; | ||
if (requestOptions.onUploadProgress) { | ||
request.onUploadProgress = requestOptions.onUploadProgress; | ||
} | ||
if (options.tracingOptions) { | ||
request.tracingOptions = options.tracingOptions; | ||
if (requestOptions.onDownloadProgress) { | ||
request.onDownloadProgress = requestOptions.onDownloadProgress; | ||
} | ||
if (requestOptions.shouldDeserialize !== undefined) { | ||
operationInfo.shouldDeserialize = requestOptions.shouldDeserialize; | ||
} | ||
if (requestOptions.allowInsecureConnection) { | ||
request.allowInsecureConnection = true; | ||
} | ||
} | ||
if (this._allowInsecureConnection) { | ||
request.allowInsecureConnection = true; | ||
if (options.abortSignal) { | ||
request.abortSignal = options.abortSignal; | ||
} | ||
if (request.streamResponseStatusCodes === undefined) { | ||
request.streamResponseStatusCodes = getStreamingResponseStatusCodes(operationSpec); | ||
if (options.tracingOptions) { | ||
request.tracingOptions = options.tracingOptions; | ||
} | ||
try { | ||
const rawResponse = yield this.sendRequest(request); | ||
const flatResponse = flattenResponse(rawResponse, operationSpec.responses[rawResponse.status]); | ||
if (options === null || options === void 0 ? void 0 : options.onResponse) { | ||
options.onResponse(rawResponse, flatResponse); | ||
} | ||
return flatResponse; | ||
} | ||
if (this._allowInsecureConnection) { | ||
request.allowInsecureConnection = true; | ||
} | ||
if (request.streamResponseStatusCodes === undefined) { | ||
request.streamResponseStatusCodes = getStreamingResponseStatusCodes(operationSpec); | ||
} | ||
try { | ||
const rawResponse = await this.sendRequest(request); | ||
const flatResponse = flattenResponse(rawResponse, operationSpec.responses[rawResponse.status]); | ||
if (options === null || options === void 0 ? void 0 : options.onResponse) { | ||
options.onResponse(rawResponse, flatResponse); | ||
} | ||
catch (error) { | ||
if (error.response) { | ||
error.details = flattenResponse(error.response, operationSpec.responses[error.statusCode] || operationSpec.responses["default"]); | ||
} | ||
throw error; | ||
return flatResponse; | ||
} | ||
catch (error) { | ||
if (error.response) { | ||
error.details = flattenResponse(error.response, operationSpec.responses[error.statusCode] || operationSpec.responses["default"]); | ||
} | ||
}); | ||
throw error; | ||
} | ||
} | ||
@@ -114,0 +109,0 @@ } |
{ | ||
"name": "@azure/core-client", | ||
"version": "1.1.3-alpha.20210521.1", | ||
"version": "1.1.3-alpha.20210610.1", | ||
"description": "Core library for interfacing with AutoRest generated code", | ||
@@ -5,0 +5,0 @@ "sdk-type": "client", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
556231
5053