Skip to content

Commit e5971de

Browse files
authored
[core-client] New option for adding policies (Azure#19920)
Rather than needing to expose the Pipeline directly, clients can pass a static array of policies to be added. After discussion with other architects, these additional policies are limited to specifying one of two positions to participate in the pipeline.
1 parent cb5df15 commit e5971de

File tree

8 files changed

+77
-3
lines changed

8 files changed

+77
-3
lines changed

sdk/core/core-client/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Release History
22

3-
## 1.4.1 (Unreleased)
3+
## 1.5.0 (Unreleased)
44

55
### Features Added
66

7+
- Added new `CommonClientOptions` member `additionalPolicies` to allow passing custom pipeline policies to client constructors. [PR #19920](https://github.com/Azure/azure-sdk-for-js/pull/19920)
8+
79
### Breaking Changes
810

911
### Bugs Fixed

sdk/core/core-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure/core-client",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"description": "Core library for interfacing with AutoRest generated code",
55
"sdk-type": "client",
66
"main": "dist/index.js",

sdk/core/core-client/review/core-client.api.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import { PipelineResponse } from '@azure/core-rest-pipeline';
1818
import { TokenCredential } from '@azure/core-auth';
1919
import { TransferProgressEvent } from '@azure/core-rest-pipeline';
2020

21+
// @public
22+
export interface AdditionalPolicyConfig {
23+
policy: PipelinePolicy;
24+
position: "perCall" | "perRetry";
25+
}
26+
2127
// @public
2228
export function authorizeRequestOnClaimChallenge(onChallengeOptions: AuthorizeRequestOnChallengeOptions): Promise<boolean>;
2329

@@ -41,6 +47,7 @@ export interface BaseMapper {
4147

4248
// @public
4349
export interface CommonClientOptions extends PipelineOptions {
50+
additionalPolicies?: AdditionalPolicyConfig[];
4451
allowInsecureConnection?: boolean;
4552
httpClient?: HttpClient;
4653
}

sdk/core/core-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export {
4040
SerializerOptions,
4141
RawResponseCallback,
4242
CommonClientOptions,
43+
AdditionalPolicyConfig,
4344
} from "./interfaces";
4445
export {
4546
deserializationPolicy,

sdk/core/core-client/src/interfaces.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HttpClient,
66
HttpMethods,
77
PipelineOptions,
8+
PipelinePolicy,
89
PipelineRequest,
910
PipelineResponse,
1011
TransferProgressEvent,
@@ -724,6 +725,23 @@ export interface SpanConfig {
724725
namespace: string;
725726
}
726727

728+
/**
729+
* Used to configure additional policies added to the pipeline at construction.
730+
*/
731+
export interface AdditionalPolicyConfig {
732+
/**
733+
* A policy to be added.
734+
*/
735+
policy: PipelinePolicy;
736+
/**
737+
* Determines if this policy be applied before or after retry logic.
738+
* Only use `perRetry` if you need to modify the request again
739+
* each time the operation is retried due to retryable service
740+
* issues.
741+
*/
742+
position: "perCall" | "perRetry";
743+
}
744+
727745
/**
728746
* The common set of options that high level clients are expected to expose.
729747
*/
@@ -736,4 +754,8 @@ export interface CommonClientOptions extends PipelineOptions {
736754
* Set to true if the request is sent over HTTP instead of HTTPS
737755
*/
738756
allowInsecureConnection?: boolean;
757+
/**
758+
* Additional policies to include in the HTTP pipeline.
759+
*/
760+
additionalPolicies?: AdditionalPolicyConfig[];
739761
}

sdk/core/core-client/src/serviceClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ export class ServiceClient {
9393
this._httpClient = options.httpClient || getCachedDefaultHttpClient();
9494

9595
this.pipeline = options.pipeline || createDefaultPipeline(options);
96+
if (options.additionalPolicies?.length) {
97+
for (const { policy, position } of options.additionalPolicies) {
98+
// Sign happens after Retry and is commonly needed to occur
99+
// before policies that intercept post-retry.
100+
const afterPhase = position === "perRetry" ? "Sign" : undefined;
101+
this.pipeline.addPolicy(policy, {
102+
afterPhase,
103+
});
104+
}
105+
}
96106
}
97107

98108
/**

sdk/core/core-client/test/serviceClient.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import {
1919
} from "../src";
2020
import {
2121
HttpClient,
22+
PipelinePolicy,
2223
PipelineRequest,
2324
RestError,
25+
SendRequest,
2426
createEmptyPipeline,
2527
createHttpHeaders,
2628
createPipelineRequest,
@@ -1483,6 +1485,36 @@ describe("ServiceClient", function () {
14831485
await client.sendOperationRequest<string>({ options: { top: 10 } as any }, operationSpec);
14841486
assert.equal(request!.url, "https://example.com/path?$skip=10&$top=10");
14851487
});
1488+
1489+
it("should insert policies in the correct pipeline position", async function () {
1490+
const pipeline = createEmptyPipeline();
1491+
const sendRequest = (request: PipelineRequest, next: SendRequest) => next(request);
1492+
const retryPolicy: PipelinePolicy = {
1493+
name: "retry",
1494+
sendRequest,
1495+
};
1496+
pipeline.addPolicy(retryPolicy, { phase: "Retry" });
1497+
const policy1: PipelinePolicy = {
1498+
name: "policy1",
1499+
sendRequest,
1500+
};
1501+
const policy2: PipelinePolicy = {
1502+
name: "policy2",
1503+
sendRequest,
1504+
};
1505+
1506+
const client = new ServiceClient({
1507+
pipeline,
1508+
additionalPolicies: [
1509+
{ policy: policy1, position: "perRetry" },
1510+
{ policy: policy2, position: "perCall" },
1511+
],
1512+
});
1513+
1514+
assert(client);
1515+
const policies = pipeline.getOrderedPolicies();
1516+
assert.deepStrictEqual(policies, [policy2, retryPolicy, policy1]);
1517+
});
14861518
});
14871519

14881520
async function testSendOperationRequest(
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
export const SDK_VERSION: string = "1.4.1";
4+
export const SDK_VERSION: string = "1.5.0";

0 commit comments

Comments
 (0)