Skip to content

Commit

Permalink
Migrate from @azure/core-http to @azure/core-rest-pipeline (#1040)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremymeng authored Nov 28, 2022
1 parent 2f626c2 commit 358fbce
Show file tree
Hide file tree
Showing 9 changed files with 500 additions and 608 deletions.
4 changes: 2 additions & 2 deletions Bootstrap/Default.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import azureCore = require("@azure/core-http");
import * as azureCoreAuth from "@azure/core-auth";

import * as types from "../applicationinsights";
import * as Helpers from "./Helpers";
Expand Down Expand Up @@ -45,7 +45,7 @@ export function setStatusLogger(statusLogger: StatusLogger) {
* Try to setup and start this app insights instance if attach is enabled.
* @param aadTokenCredential Optional AAD credential
*/
export function setupAndStart(aadTokenCredential?: azureCore.TokenCredential): typeof types | null {
export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential): typeof types | null {
// If app already contains SDK, skip agent attach
if (!forceStart && Helpers.sdkAlreadyExists(_logger)) {
_statusLogger.logStatus({
Expand Down
4 changes: 2 additions & 2 deletions Declarations/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import http = require("http");
import https = require("https");
import azureCore = require("@azure/core-http");
import * as azureCoreAuth from "@azure/core-auth";
import { DistributedTracingModes } from "../applicationinsights";
import { IDisabledExtendedMetrics } from "../AutoCollection/NativePerformance";

Expand Down Expand Up @@ -216,5 +216,5 @@ export interface IConfig extends IBaseConfig {
/** An https.Agent to use for SDK HTTPS traffic (Optional, Default undefined) */
httpsAgent: https.Agent;
/** AAD TokenCredential to use to authenticate the app */
aadTokenCredential?: azureCore.TokenCredential;
aadTokenCredential?: azureCoreAuth.TokenCredential;
}
28 changes: 14 additions & 14 deletions Library/AuthorizationHandler.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import http = require("http");
import https = require("https");
import azureCore = require("@azure/core-http");
import azureCoreAuth = require("@azure/core-auth");
import azureCore = require("@azure/core-rest-pipeline");

const applicationInsightsResource = "https://monitor.azure.com//.default";


function emptySendRequest(_request: azureCore.PipelineRequest): Promise<azureCore.PipelineResponse> {
return null;
}

class AuthorizationHandler {

private _azureTokenPolicy: azureCore.RequestPolicy;
private _azureTokenPolicy: azureCore.PipelinePolicy;

constructor(credential: azureCore.TokenCredential) {
constructor(credential: azureCoreAuth.TokenCredential) {
let scopes: string[] = [applicationInsightsResource];
let emptyPolicy: azureCore.RequestPolicy = {
sendRequest(httpRequest: azureCore.WebResourceLike): Promise<azureCore.HttpOperationResponse> {
return null;
}
};
this._azureTokenPolicy = azureCore.bearerTokenAuthenticationPolicy(credential, scopes).create(emptyPolicy, new azureCore.RequestPolicyOptions());
this._azureTokenPolicy = azureCore.bearerTokenAuthenticationPolicy({credential, scopes});
}

/**
* Applies the Bearer token to the request through the Authorization header.
*/
public async addAuthorizationHeader(requestOptions: http.RequestOptions | https.RequestOptions): Promise<void> {
let authHeaderName = azureCore.Constants.HeaderConstants.AUTHORIZATION;
let webResource = new azureCore.WebResource("https://");
await this._azureTokenPolicy.sendRequest(webResource);
let authHeaderName = "authorization";
let webResource = azureCore.createPipelineRequest({ url: "https://" });
await this._azureTokenPolicy.sendRequest(webResource, emptySendRequest);
console.log("#### webResource", webResource);
requestOptions.headers[authHeaderName] = webResource.headers.get(authHeaderName);
}

}

export = AuthorizationHandler;
export = AuthorizationHandler;
4 changes: 2 additions & 2 deletions Library/Config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import azureCore = require("@azure/core-http");
import azureCoreAuth = require("@azure/core-auth");

import CorrelationIdManager = require("./CorrelationIdManager");
import ConnectionStringParser = require("./ConnectionStringParser");
Expand Down Expand Up @@ -33,7 +33,7 @@ class Config implements IConfig {
public httpAgent: http.Agent;
public httpsAgent: https.Agent;
public ignoreLegacyHeaders: boolean;
public aadTokenCredential?: azureCore.TokenCredential;
public aadTokenCredential?: azureCoreAuth.TokenCredential;
public enableAutoCollectConsole: boolean;
public enableAutoCollectExceptions: boolean;
public enableAutoCollectPerformance: boolean;
Expand Down
3 changes: 1 addition & 2 deletions Library/TelemetryClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import url = require("url");
import os = require("os");
import azureCore = require("@azure/core-http");

import Config = require("./Config");
import AuthorizationHandler = require("./AuthorizationHandler");
Expand Down Expand Up @@ -273,4 +272,4 @@ class TelemetryClient {
}
}

export = TelemetryClient;
export = TelemetryClient;
6 changes: 3 additions & 3 deletions Tests/Library/AuthorizationHandler.tests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import assert = require("assert");
import https = require("https");
import sinon = require("sinon");
import azureCore = require("@azure/core-http");
import azureCoreAuth = require("@azure/core-auth");

import AuthorizationHandler = require("../../Library/AuthorizationHandler");
import Config = require("../../Library/Config");
import Util = require("../../Library/Util");

class TestTokenCredential implements azureCore.TokenCredential {
class TestTokenCredential implements azureCoreAuth.TokenCredential {
private _expiresOn: Date;
private _numberOfRefreshs = 0;

Expand Down Expand Up @@ -71,4 +71,4 @@ describe("Library/AuthorizationHandler", () => {
assert.equal(options.headers["authorization"], "Bearer testToken2");
});
});
});
});
1 change: 0 additions & 1 deletion Tests/Library/Client.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import https = require("https");
import sinon = require("sinon");
import Sinon = require("sinon");
import eventEmitter = require('events');
import azureCore = require("@azure/core-http");

import AutoCollecPreAggregatedMetrics = require("../../AutoCollection/PreAggregatedMetrics");
import Client = require("../../Library/NodeClient");
Expand Down
Loading

0 comments on commit 358fbce

Please sign in to comment.