Skip to content

Commit

Permalink
RequestPolicyCreator -> RequestPolicyFactory (Azure#184)
Browse files Browse the repository at this point in the history
* RequestPolicyCreator -> RequestPolicyFactory

* Bump to v0.18

* Update changelog for 0.18
  • Loading branch information
RikkiGibson authored Aug 9, 2018
1 parent 3ff6c8c commit e240203
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 68 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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().
Expand Down
2 changes: 1 addition & 1 deletion lib/msRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 6 additions & 4 deletions lib/policies/deserializationPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/exponentialRetryPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/generateClientRequestIdPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/logPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/msRestUserAgentPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>): RequestPolicyCreator {
return (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new MsRestUserAgentPolicy(nextPolicy, options, userAgentInfo);
export function msRestUserAgentPolicy(userAgentInfo: Array<string>): RequestPolicyFactory {
return {
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => {
return new MsRestUserAgentPolicy(nextPolicy, options, userAgentInfo);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/redirectPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
6 changes: 4 additions & 2 deletions lib/policies/requestPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpOperationResponse>;
Expand Down
10 changes: 6 additions & 4 deletions lib/policies/rpRegistrationPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/signingPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions lib/policies/systemErrorRetryPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
};
}

Expand Down
38 changes: 19 additions & 19 deletions lib/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion samples/node-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 4 additions & 4 deletions test/shared/policies/deserializationPolicyTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
Expand All @@ -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]);
});
Expand All @@ -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]);
});
Expand All @@ -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]);
});
Expand Down
8 changes: 4 additions & 4 deletions test/shared/serviceClientTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("ServiceClient", function () {
return Promise.resolve({} as HttpOperationResponse);
}
},
requestPolicyCreators: []
requestPolicyFactories: []
});

await client.sendOperationRequest(
Expand Down Expand Up @@ -83,7 +83,7 @@ describe("ServiceClient", function () {
return Promise.resolve({} as HttpOperationResponse);
},
},
requestPolicyCreators: [],
requestPolicyFactories: [],
});

await client.sendOperationRequest(
Expand Down Expand Up @@ -133,7 +133,7 @@ describe("ServiceClient", function () {

const client1 = new ServiceClient(undefined, {
httpClient,
requestPolicyCreators: []
requestPolicyFactories: []
});
await client1.sendOperationRequest(
{},
Expand All @@ -148,7 +148,7 @@ describe("ServiceClient", function () {

const client2 = new ServiceClient(undefined, {
httpClient,
requestPolicyCreators: [],
requestPolicyFactories: [],
withCredentials: true
});
await client2.sendOperationRequest(
Expand Down

0 comments on commit e240203

Please sign in to comment.