Skip to content

Commit

Permalink
Migrate Template project to use CoreV2 (#19748)
Browse files Browse the repository at this point in the history
This PR migrate `sdk/template/template` from using `@azure/core-http` to use `@azure/core-client` and `@azure/core-rest-pipeline`.

Followed instructions on [this migration guide](https://github.com/Azure/autorest.typescript/wiki/%60core-http%60-dependency-migration-to-%60core-client%60-%60core-rest-pipeline%60)

More information about Azure Core V2 can be found [here](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/core/core-rest-pipeline/documentation/core2.md).
  • Loading branch information
JonathanCrd authored Feb 3, 2022
1 parent 477dfb4 commit c26ac44
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 570 deletions.
9 changes: 7 additions & 2 deletions sdk/template/template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
"constantPaths": [
{
"path": "src/generated/generatedClientContext.ts",
"prefix": "packageVersion"
"prefix": "packageDetails"
},
{
"path": "src/constants.ts",
"prefix": "SDK_VERSION"
},
{
"path": "swagger/README.md",
"prefix": "package-version"
}
]
},
Expand Down Expand Up @@ -78,7 +82,8 @@
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure/core-http": "^2.0.0",
"@azure/core-client": "^1.4.0",
"@azure/core-rest-pipeline": "^1.4.0",
"@azure/core-tracing": "1.0.0-preview.14",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions sdk/template/template/review/template.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
```ts

import { OperationOptions } from '@azure/core-http';
import { PipelineOptions } from '@azure/core-http';
import { TokenCredential } from '@azure/core-http';
import { CommonClientOptions } from '@azure/core-client';
import { OperationOptions } from '@azure/core-client';
import { TokenCredential } from '@azure/core-auth';

// @public
export class ConfigurationClient {
Expand All @@ -16,7 +16,7 @@ export class ConfigurationClient {
}

// @public
export interface ConfigurationClientOptions extends PipelineOptions {
export interface ConfigurationClientOptions extends CommonClientOptions {
}

// @public (undocumented)
Expand All @@ -38,7 +38,6 @@ export interface GetConfigurationSettingOptions extends OperationOptions {
onlyIfChanged?: boolean;
}


// (No @packageDocumentation comment for this package)

```
37 changes: 14 additions & 23 deletions sdk/template/template/src/configurationClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

import { ConfigurationSetting, GeneratedClient } from "./generated";
import {
InternalPipelineOptions,
CommonClientOptions,
OperationOptions,
PipelineOptions,
TokenCredential,
bearerTokenAuthenticationPolicy,
createPipelineFromOptions,
} from "@azure/core-http";
InternalClientPipelineOptions,
} from "@azure/core-client";
import { bearerTokenAuthenticationPolicy } from "@azure/core-rest-pipeline";
import { TokenCredential } from "@azure/core-auth";
import { TracingClient, createTracingClient } from "@azure/core-tracing";
import { SDK_VERSION } from "./constants";
import { logger } from "./logger";
Expand All @@ -36,7 +35,7 @@ export interface GetConfigurationSettingOptions extends OperationOptions {
/**
* Client options used to configure App Configuration API requests.
*/
export interface ConfigurationClientOptions extends PipelineOptions {
export interface ConfigurationClientOptions extends CommonClientOptions {
// Any custom options configured at the client level go here.
}

Expand Down Expand Up @@ -69,22 +68,14 @@ export class ConfigurationClient {
credential: TokenCredential,
options: ConfigurationClientOptions = {}
) {
// The below code helps us set a proper User-Agent header on all requests
const libInfo = `azsdk-js-template-template/${SDK_VERSION}`;
if (!options.userAgentOptions) {
options.userAgentOptions = {};
}
if (options.userAgentOptions.userAgentPrefix) {
options.userAgentOptions.userAgentPrefix = `${options.userAgentOptions.userAgentPrefix} ${libInfo}`;
} else {
options.userAgentOptions.userAgentPrefix = libInfo;
}

// The AAD scope for an API is usually the baseUri + "/.default", but it
// may be different for your service.
const authPolicy = bearerTokenAuthenticationPolicy(credential, `${endpointUrl}/.default`);
const authPolicy = bearerTokenAuthenticationPolicy({
credential,
scopes: `${endpointUrl}/.default`,
});

const internalPipelineOptions: InternalPipelineOptions = {
const internalClientPipelineOptions: InternalClientPipelineOptions = {
...options,
deserializationOptions: {
expectedContentTypes: {
Expand All @@ -102,13 +93,13 @@ export class ConfigurationClient {
logger: logger.info,
// This array contains header names we want to log that are not already
// included as safe. Unknown/unsafe headers are logged as "<REDACTED>".
allowedHeaderNames: ["x-ms-correlation-request-id"],
additionalAllowedHeaderNames: ["x-ms-correlation-request-id"],
},
},
};
const pipeline = createPipelineFromOptions(internalPipelineOptions, authPolicy);

this.client = new GeneratedClient(endpointUrl, pipeline);
this.client = new GeneratedClient(endpointUrl, internalClientPipelineOptions);
this.client.pipeline.addPolicy(authPolicy);
this.tracingClient = createTracingClient({
// The name of the resource provider requests are made against, as described in
// https://github.com/Azure/azure-sdk/blob/main/docs/tracing/distributed-tracing-conventions.yml#L11-L15
Expand Down
Loading

0 comments on commit c26ac44

Please sign in to comment.