From e240203ca0d1e582c2bcac47a9a86b1b293fbd01 Mon Sep 17 00:00:00 2001 From: Rikki Gibson Date: Wed, 8 Aug 2018 17:00:35 -0700 Subject: [PATCH] RequestPolicyCreator -> RequestPolicyFactory (#184) * RequestPolicyCreator -> RequestPolicyFactory * Bump to v0.18 * Update changelog for 0.18 --- Changelog.md | 3 ++ lib/msRest.ts | 2 +- lib/policies/deserializationPolicy.ts | 10 +++-- lib/policies/exponentialRetryPolicy.ts | 10 +++-- lib/policies/generateClientRequestIdPolicy.ts | 10 +++-- lib/policies/logPolicy.ts | 10 +++-- lib/policies/msRestUserAgentPolicy.ts | 10 +++-- lib/policies/redirectPolicy.ts | 10 +++-- lib/policies/requestPolicy.ts | 6 ++- lib/policies/rpRegistrationPolicy.ts | 10 +++-- lib/policies/signingPolicy.ts | 10 +++-- lib/policies/systemErrorRetryPolicy.ts | 10 +++-- lib/serviceClient.ts | 38 +++++++++---------- package.json | 2 +- samples/node-sample.ts | 2 +- .../policies/deserializationPolicyTests.ts | 8 ++-- test/shared/serviceClientTests.ts | 8 ++-- 17 files changed, 91 insertions(+), 68 deletions(-) diff --git a/Changelog.md b/Changelog.md index 171b18c14d07..43f93f381ab3 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,6 @@ +### 0.18.0 - 2018-08-08 +- Replaced RequestPolicyCreator function with RequestPolicyFactory interface with create() method. + ### 0.5.0 - 2018-05-08 - Replace BaseFilter type with RequestPolicy. - Remove ServiceClient.pipeline() in favor of ServiceClient.sendRequest(). diff --git a/lib/msRest.ts b/lib/msRest.ts index b3173e4aa9c4..4881dec0d673 100644 --- a/lib/msRest.ts +++ b/lib/msRest.ts @@ -16,7 +16,7 @@ export { ServiceClient, ServiceClientOptions } from "./serviceClient"; export { QueryCollectionFormat } from "./queryCollectionFormat"; export { Constants } from "./util/constants"; export { logPolicy } from "./policies/logPolicy"; -export { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./policies/requestPolicy"; +export { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./policies/requestPolicy"; export { exponentialRetryPolicy } from "./policies/exponentialRetryPolicy"; export { systemErrorRetryPolicy } from "./policies/systemErrorRetryPolicy"; export { redirectPolicy } from "./policies/redirectPolicy"; diff --git a/lib/policies/deserializationPolicy.ts b/lib/policies/deserializationPolicy.ts index 35e52d44fa76..c2522446b963 100644 --- a/lib/policies/deserializationPolicy.ts +++ b/lib/policies/deserializationPolicy.ts @@ -9,15 +9,17 @@ import { RestError } from "../restError"; import { Mapper, MapperType } from "../serializer"; import * as utils from "../util/utils"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; /** * Create a new serialization RequestPolicyCreator that will serialized HTTP request bodies as they * pass through the HTTP pipeline. */ -export function deserializationPolicy(): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new DeserializationPolicy(nextPolicy, options); +export function deserializationPolicy(): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new DeserializationPolicy(nextPolicy, options); + } }; } diff --git a/lib/policies/exponentialRetryPolicy.ts b/lib/policies/exponentialRetryPolicy.ts index 603840d65bd2..b49b5a2391f5 100644 --- a/lib/policies/exponentialRetryPolicy.ts +++ b/lib/policies/exponentialRetryPolicy.ts @@ -4,7 +4,7 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import * as utils from "../util/utils"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; export interface RetryData { retryCount: number; @@ -18,9 +18,11 @@ export interface RetryError extends Error { innerError?: RetryError; } -export function exponentialRetryPolicy(retryCount?: number, retryInterval?: number, minRetryInterval?: number, maxRetryInterval?: number): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new ExponentialRetryPolicy(nextPolicy, options, retryCount, retryInterval, minRetryInterval, maxRetryInterval); +export function exponentialRetryPolicy(retryCount?: number, retryInterval?: number, minRetryInterval?: number, maxRetryInterval?: number): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new ExponentialRetryPolicy(nextPolicy, options, retryCount, retryInterval, minRetryInterval, maxRetryInterval); + } }; } diff --git a/lib/policies/generateClientRequestIdPolicy.ts b/lib/policies/generateClientRequestIdPolicy.ts index e271a1abde10..e85cafb0e76b 100644 --- a/lib/policies/generateClientRequestIdPolicy.ts +++ b/lib/policies/generateClientRequestIdPolicy.ts @@ -4,11 +4,13 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import * as utils from "../util/utils"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; -export function generateClientRequestIdPolicy(requestIdHeaderName = "x-ms-client-request-id"): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new GenerateClientRequestIdPolicy(nextPolicy, options, requestIdHeaderName); +export function generateClientRequestIdPolicy(requestIdHeaderName = "x-ms-client-request-id"): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new GenerateClientRequestIdPolicy(nextPolicy, options, requestIdHeaderName); + } }; } diff --git a/lib/policies/logPolicy.ts b/lib/policies/logPolicy.ts index b14818f6d166..4ab36babc489 100644 --- a/lib/policies/logPolicy.ts +++ b/lib/policies/logPolicy.ts @@ -3,11 +3,13 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; -export function logPolicy(logger: any = console.log): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new LogPolicy(nextPolicy, options, logger); +export function logPolicy(logger: any = console.log): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new LogPolicy(nextPolicy, options, logger); + } }; } diff --git a/lib/policies/msRestUserAgentPolicy.ts b/lib/policies/msRestUserAgentPolicy.ts index ab5c4e77f93f..f8a39fd2be91 100644 --- a/lib/policies/msRestUserAgentPolicy.ts +++ b/lib/policies/msRestUserAgentPolicy.ts @@ -7,13 +7,15 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import { Constants } from "../util/constants"; import { isNode } from "../util/utils"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; const HeaderConstants = Constants.HeaderConstants; -export function msRestUserAgentPolicy(userAgentInfo: Array): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new MsRestUserAgentPolicy(nextPolicy, options, userAgentInfo); +export function msRestUserAgentPolicy(userAgentInfo: Array): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new MsRestUserAgentPolicy(nextPolicy, options, userAgentInfo); + } }; } diff --git a/lib/policies/redirectPolicy.ts b/lib/policies/redirectPolicy.ts index e2576a8b1769..90aa3b030edd 100644 --- a/lib/policies/redirectPolicy.ts +++ b/lib/policies/redirectPolicy.ts @@ -4,11 +4,13 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import { URLBuilder } from "../url"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; -export function redirectPolicy(maximumRetries = 20): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new RedirectPolicy(nextPolicy, options, maximumRetries); +export function redirectPolicy(maximumRetries = 20): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new RedirectPolicy(nextPolicy, options, maximumRetries); + } }; } diff --git a/lib/policies/requestPolicy.ts b/lib/policies/requestPolicy.ts index 28fe7ef7f0ea..6089a0fd0e58 100644 --- a/lib/policies/requestPolicy.ts +++ b/lib/policies/requestPolicy.ts @@ -7,9 +7,11 @@ import { HttpPipelineLogLevel } from "../httpPipelineLogLevel"; import { WebResource } from "../webResource"; /** - * A function that creates a new RequestPolicy that uses the provided nextPolicy. + * Creates a new RequestPolicy per-request that uses the provided nextPolicy. */ -export type RequestPolicyCreator = (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => RequestPolicy; +export type RequestPolicyFactory = { + create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): RequestPolicy +}; export interface RequestPolicy { sendRequest(httpRequest: WebResource): Promise; diff --git a/lib/policies/rpRegistrationPolicy.ts b/lib/policies/rpRegistrationPolicy.ts index c3e94f4d0d73..4801c7fa03d5 100644 --- a/lib/policies/rpRegistrationPolicy.ts +++ b/lib/policies/rpRegistrationPolicy.ts @@ -3,11 +3,13 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import * as utils from "../util/utils"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; -export function rpRegistrationPolicy(retryTimeout = 30): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new RPRegistrationPolicy(nextPolicy, options, retryTimeout); +export function rpRegistrationPolicy(retryTimeout = 30): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new RPRegistrationPolicy(nextPolicy, options, retryTimeout); + } }; } diff --git a/lib/policies/signingPolicy.ts b/lib/policies/signingPolicy.ts index 534b04c951c1..c0d19230c970 100644 --- a/lib/policies/signingPolicy.ts +++ b/lib/policies/signingPolicy.ts @@ -4,11 +4,13 @@ import { ServiceClientCredentials } from "../credentials/serviceClientCredentials"; import { HttpOperationResponse } from "../httpOperationResponse"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicyCreator, RequestPolicy, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicyFactory, RequestPolicy, RequestPolicyOptions } from "./requestPolicy"; -export function signingPolicy(authenticationProvider: ServiceClientCredentials): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new SigningPolicy(nextPolicy, options, authenticationProvider); +export function signingPolicy(authenticationProvider: ServiceClientCredentials): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new SigningPolicy(nextPolicy, options, authenticationProvider); + } }; } diff --git a/lib/policies/systemErrorRetryPolicy.ts b/lib/policies/systemErrorRetryPolicy.ts index cba6b1cc46c1..2f4d14a9e65b 100644 --- a/lib/policies/systemErrorRetryPolicy.ts +++ b/lib/policies/systemErrorRetryPolicy.ts @@ -4,7 +4,7 @@ import { HttpOperationResponse } from "../httpOperationResponse"; import * as utils from "../util/utils"; import { WebResource } from "../webResource"; -import { BaseRequestPolicy, RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./requestPolicy"; +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; export interface RetryData { retryCount: number; @@ -18,9 +18,11 @@ export interface RetryError extends Error { innerError?: RetryError; } -export function systemErrorRetryPolicy(retryCount?: number, retryInterval?: number, minRetryInterval?: number, maxRetryInterval?: number): RequestPolicyCreator { - return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { - return new SystemErrorRetryPolicy(nextPolicy, options, retryCount, retryInterval, minRetryInterval, maxRetryInterval); +export function systemErrorRetryPolicy(retryCount?: number, retryInterval?: number, minRetryInterval?: number, maxRetryInterval?: number): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new SystemErrorRetryPolicy(nextPolicy, options, retryCount, retryInterval, minRetryInterval, maxRetryInterval); + } }; } diff --git a/lib/serviceClient.ts b/lib/serviceClient.ts index 71ab5cfdb11c..f1ca5275006b 100644 --- a/lib/serviceClient.ts +++ b/lib/serviceClient.ts @@ -15,7 +15,7 @@ import { exponentialRetryPolicy } from "./policies/exponentialRetryPolicy"; import { generateClientRequestIdPolicy } from "./policies/generateClientRequestIdPolicy"; import { msRestUserAgentPolicy } from "./policies/msRestUserAgentPolicy"; import { redirectPolicy } from "./policies/redirectPolicy"; -import { RequestPolicy, RequestPolicyCreator, RequestPolicyOptions } from "./policies/requestPolicy"; +import { RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./policies/requestPolicy"; import { rpRegistrationPolicy } from "./policies/rpRegistrationPolicy"; import { signingPolicy } from "./policies/signingPolicy"; import { systemErrorRetryPolicy } from "./policies/systemErrorRetryPolicy"; @@ -31,10 +31,10 @@ import { RequestPrepareOptions, WebResource, RequestOptionsBase } from "./webRes */ export interface ServiceClientOptions { /** - * An array of functions that will be invoked to create the RequestPolicy pipeline that will be + * An array of factories which get called to create the RequestPolicy pipeline * used to send a HTTP request on the wire. */ - requestPolicyCreators?: RequestPolicyCreator[]; + requestPolicyFactories?: RequestPolicyFactory[]; /** * The HttpClient that will be used to send HTTP requests. */ @@ -96,7 +96,7 @@ export class ServiceClient { private readonly _httpClient: HttpClient; private readonly _requestPolicyOptions: RequestPolicyOptions; - private readonly _requestPolicyCreators: RequestPolicyCreator[]; + private readonly _requestPolicyFactories: RequestPolicyFactory[]; private readonly _withCredentials: boolean; /** @@ -128,7 +128,7 @@ export class ServiceClient { this._httpClient = options.httpClient || new DefaultHttpClient(); this._requestPolicyOptions = new RequestPolicyOptions(options.httpPipelineLogger); - this._requestPolicyCreators = options.requestPolicyCreators || createDefaultRequestPolicyCreators(credentials, options, this.userAgentInfo.value); + this._requestPolicyFactories = options.requestPolicyFactories || createDefaultRequestPolicyFactories(credentials, options, this.userAgentInfo.value); } /** @@ -164,9 +164,9 @@ export class ServiceClient { } let httpPipeline: RequestPolicy = this._httpClient; - if (this._requestPolicyCreators && this._requestPolicyCreators.length > 0) { - for (let i = this._requestPolicyCreators.length - 1; i >= 0; --i) { - httpPipeline = this._requestPolicyCreators[i](httpPipeline, this._requestPolicyOptions); + if (this._requestPolicyFactories && this._requestPolicyFactories.length > 0) { + for (let i = this._requestPolicyFactories.length - 1; i >= 0; --i) { + httpPipeline = this._requestPolicyFactories[i].create(httpPipeline, this._requestPolicyOptions); } } return httpPipeline.sendRequest(httpRequest); @@ -350,32 +350,32 @@ export function serializeRequestBody(serviceClient: ServiceClient, httpRequest: } } -function createDefaultRequestPolicyCreators(credentials: ServiceClientCredentials | undefined, options: ServiceClientOptions, userAgentInfo: string[]): RequestPolicyCreator[] { - const defaultRequestPolicyCreators: RequestPolicyCreator[] = []; +function createDefaultRequestPolicyFactories(credentials: ServiceClientCredentials | undefined, options: ServiceClientOptions, userAgentInfo: string[]): RequestPolicyFactory[] { + const factories: RequestPolicyFactory[] = []; if (options.generateClientRequestIdHeader) { - defaultRequestPolicyCreators.push(generateClientRequestIdPolicy(options.clientRequestIdHeaderName)); + factories.push(generateClientRequestIdPolicy(options.clientRequestIdHeaderName)); } if (credentials) { - defaultRequestPolicyCreators.push(signingPolicy(credentials)); + factories.push(signingPolicy(credentials)); } if (utils.isNode) { - defaultRequestPolicyCreators.push(msRestUserAgentPolicy(userAgentInfo)); + factories.push(msRestUserAgentPolicy(userAgentInfo)); } - defaultRequestPolicyCreators.push(redirectPolicy()); - defaultRequestPolicyCreators.push(rpRegistrationPolicy(options.rpRegistrationRetryTimeout)); + factories.push(redirectPolicy()); + factories.push(rpRegistrationPolicy(options.rpRegistrationRetryTimeout)); if (!options.noRetryPolicy) { - defaultRequestPolicyCreators.push(exponentialRetryPolicy()); - defaultRequestPolicyCreators.push(systemErrorRetryPolicy()); + factories.push(exponentialRetryPolicy()); + factories.push(systemErrorRetryPolicy()); } - defaultRequestPolicyCreators.push(deserializationPolicy()); + factories.push(deserializationPolicy()); - return defaultRequestPolicyCreators; + return factories; } export type PropertyParent = { [propertyName: string]: any }; diff --git a/package.json b/package.json index f1570138917e..68b9f27142a6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "email": "azsdkteam@microsoft.com", "url": "https://github.com/Azure/ms-rest-js" }, - "version": "0.17.0", + "version": "0.18.0", "description": "Isomorphic client Runtime for Typescript/node.js/browser javascript client libraries generated using AutoRest", "tags": [ "isomorphic", diff --git a/samples/node-sample.ts b/samples/node-sample.ts index 25b77a07dc43..51c3fc71d833 100644 --- a/samples/node-sample.ts +++ b/samples/node-sample.ts @@ -2,7 +2,7 @@ import * as msRest from "../lib/msRest"; const clientOptions: msRest.ServiceClientOptions = { - requestPolicyCreators: [msRest.logPolicy()] + requestPolicyFactories: [msRest.logPolicy()] }; const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"] || "subscriptionId"; diff --git a/test/shared/policies/deserializationPolicyTests.ts b/test/shared/policies/deserializationPolicyTests.ts index 18468b53f9ae..736a0485e601 100644 --- a/test/shared/policies/deserializationPolicyTests.ts +++ b/test/shared/policies/deserializationPolicyTests.ts @@ -41,7 +41,7 @@ describe("deserializationPolicy", () => { }) }; - const policy = deserializationPolicy()(mockClient, new RequestPolicyOptions()); + const policy = deserializationPolicy().create(mockClient, new RequestPolicyOptions()); const response = await policy.sendRequest(request); assert.deepStrictEqual(response.parsedBody, [123, 456, 789]); }); @@ -57,7 +57,7 @@ describe("deserializationPolicy", () => { }) }; - const policy = deserializationPolicy()(mockClient, new RequestPolicyOptions()); + const policy = deserializationPolicy().create(mockClient, new RequestPolicyOptions()); const response = await policy.sendRequest(request); assert.deepStrictEqual(response.parsedBody, [123, 456, 789]); }); @@ -73,7 +73,7 @@ describe("deserializationPolicy", () => { }) }; - const policy = deserializationPolicy()(mockClient, new RequestPolicyOptions()); + const policy = deserializationPolicy().create(mockClient, new RequestPolicyOptions()); const response = await policy.sendRequest(request); assert.deepStrictEqual(response.parsedBody, [123, 456, 789]); }); @@ -89,7 +89,7 @@ describe("deserializationPolicy", () => { }) }; - const policy = deserializationPolicy()(mockClient, new RequestPolicyOptions()); + const policy = deserializationPolicy().create(mockClient, new RequestPolicyOptions()); const response = await policy.sendRequest(request); assert.deepStrictEqual(response.parsedBody, [123, 456, 789]); }); diff --git a/test/shared/serviceClientTests.ts b/test/shared/serviceClientTests.ts index 49089691cf2e..2d75c25ce944 100644 --- a/test/shared/serviceClientTests.ts +++ b/test/shared/serviceClientTests.ts @@ -25,7 +25,7 @@ describe("ServiceClient", function () { return Promise.resolve({} as HttpOperationResponse); } }, - requestPolicyCreators: [] + requestPolicyFactories: [] }); await client.sendOperationRequest( @@ -83,7 +83,7 @@ describe("ServiceClient", function () { return Promise.resolve({} as HttpOperationResponse); }, }, - requestPolicyCreators: [], + requestPolicyFactories: [], }); await client.sendOperationRequest( @@ -133,7 +133,7 @@ describe("ServiceClient", function () { const client1 = new ServiceClient(undefined, { httpClient, - requestPolicyCreators: [] + requestPolicyFactories: [] }); await client1.sendOperationRequest( {}, @@ -148,7 +148,7 @@ describe("ServiceClient", function () { const client2 = new ServiceClient(undefined, { httpClient, - requestPolicyCreators: [], + requestPolicyFactories: [], withCredentials: true }); await client2.sendOperationRequest(