This commit is contained in:
Dan Schulte 2018-05-16 14:47:17 -07:00
Родитель 6c654a1671
Коммит f780228506
4 изменённых файлов: 63 добавлений и 1 удалений

Просмотреть файл

@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
import { HttpMethods } from "./webResource";
import { Mapper } from "./serializer";
/**
* A specification that defines an operation.
@ -11,4 +12,9 @@ export interface OperationSpec {
* The HTTP method that should be used by requests for this operation.
*/
httpMethod: HttpMethods;
/**
* The Mapper that will be used to serialize an HTTP request's body.
*/
requestBodyMapper?: Mapper;
}

Просмотреть файл

@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
import { HttpOperationResponse } from "../httpOperationResponse";
import { WebResource } from "../webResource";
import { BaseRequestPolicy, RequestPolicyCreator, RequestPolicy, RequestPolicyOptions } from "./requestPolicy";
import { Serializer, Mapper } from "../serializer";
/**
* Create a new serialization RequestPolicyCreator that will serialized HTTP request bodies as they
* pass through the HTTP pipeline.
*/
export function serializationPolicy(serializer: Serializer): RequestPolicyCreator {
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new SerializationPolicy(nextPolicy, options, serializer);
};
}
/**
* A RequestPolicy that will serialize HTTP request bodies as they pass through the HTTP pipeline.
*/
export class SerializationPolicy extends BaseRequestPolicy {
constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions, private readonly _serializer: Serializer) {
super(nextPolicy, options);
}
public async sendRequest(request: WebResource): Promise<HttpOperationResponse> {
this.serializeRequestBody(request);
return await this._nextPolicy.sendRequest(request);
}
/**
* Serialize the provided HTTP request's body based on the requestBodyMapper assigned to the HTTP
* request.
* @param {WebResource} request - The HTTP request that will have its body serialized.
*/
public serializeRequestBody(request: WebResource): void {
const requestBodyMapper: Mapper | undefined = request.operationSpec && request.operationSpec.requestBodyMapper;
if (requestBodyMapper) {
request.body = this._serializer.serialize(requestBodyMapper, request.body, "");
}
}
}

Просмотреть файл

@ -17,6 +17,8 @@ import { systemErrorRetryPolicy } from "./policies/systemErrorRetryPolicy";
import { Constants } from "./util/constants";
import * as utils from "./util/utils";
import { RequestPrepareOptions, WebResource } from "./webResource";
import { Serializer } from "./serializer";
import { serializationPolicy } from "./policies/serializationPolicy";
/**
* Options to be provided while creating the client.
@ -51,6 +53,11 @@ export interface ServiceClientOptions {
* in seconds for AutomaticRPRegistration. Default value is 30.
*/
rpRegistrationRetryTimeout?: number;
/**
* @property {Serializer} [serializer] - The serializer that will be used in the serialization
* request policy.
*/
serializer?: Serializer;
}
/**
@ -164,6 +171,7 @@ export class ServiceClient {
*/
async sendOperationRequest(httpRequest: WebResource, operationSpec: OperationSpec): Promise<HttpOperationResponse> {
httpRequest.method = operationSpec.httpMethod;
httpRequest.operationSpec = operationSpec;
return this.sendRequest(httpRequest);
}
@ -180,6 +188,10 @@ function createDefaultRequestPolicyCreators(credentials: ServiceClientCredential
defaultRequestPolicyCreators.push(msRestUserAgentPolicy(userAgentInfo));
}
if (options.serializer) {
defaultRequestPolicyCreators.push(serializationPolicy(options.serializer));
}
defaultRequestPolicyCreators.push(redirectPolicy());
defaultRequestPolicyCreators.push(rpRegistrationPolicy(options.rpRegistrationRetryTimeout));

Просмотреть файл

@ -3,6 +3,7 @@
import { generateUuid } from "./util/utils";
import { Serializer, Mapper } from "./serializer";
import { OperationSpec } from "./msRest";
export type HttpMethods = "GET" | "PUT" | "POST" | "DELETE" | "PATCH" | "HEAD" | "OPTIONS" | "TRACE";
/**
@ -21,7 +22,7 @@ export class WebResource {
rawResponse?: boolean;
formData?: any;
query?: { [key: string]: any; };
operationSpec?: OperationSpec;
constructor(url?: string, method?: HttpMethods, body?: any, query?: { [key: string]: any; }, headers: { [key: string]: any; } = {}, rawResponse = false) {
this.rawResponse = rawResponse;