Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@azure/core-auth": "^1.3.0",
"@azure/core-client": "^1.3.0",
"@azure/core-rest-pipeline": "^1.3.0",
"@azure/core-lro": "^2.2.0",
"@azure/core-lro": "^2.2.4",
"@azure/core-paging": "^1.1.1",
"@azure/core-tracing": "1.0.0-preview.13",
"@azure/logger": "^1.0.0",
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ import {
BeginSearchAvailablePhoneNumbersOptions,
BeginUpdatePhoneNumberCapabilitiesOptions,
} from "./lroModels";
import {
createPhoneNumbersPagingPolicy,
phoneNumbersLroPolicy,
} from "./utils/customPipelinePolicies";
import { createPhoneNumbersPagingPolicy } from "./utils/customPipelinePolicies";
Comment thread
lucasrsant marked this conversation as resolved.
import { CommonClientOptions } from "@azure/core-client";

/**
Expand Down Expand Up @@ -106,10 +103,9 @@ export class PhoneNumbersClient {
const authPolicy = createCommunicationAuthPolicy(credential);
this.client.pipeline.addPolicy(authPolicy);

// These policies are temporary workarounds to address compatibility issues with Azure Core V2.
// This policy is temporary workarounds to address compatibility issues with Azure Core V2.
Comment thread
lucasrsant marked this conversation as resolved.
const phoneNumbersPagingPolicy = createPhoneNumbersPagingPolicy(url);
this.client.pipeline.addPolicy(phoneNumbersPagingPolicy);
this.client.pipeline.addPolicy(phoneNumbersLroPolicy);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,3 @@ export function createPhoneNumbersPagingPolicy(host: string): PipelinePolicy {
},
};
}

/**
* A `PipelinePolicy` that adds the `azure-asyncoperation` header to the request with the
* same value as the `operation-location` header, if it exists.
*
* This is used because the phone-numbers API uses LROs with status monitors, but
* the Core V2 LRO implementation only supports this pattern if the legacy
* `azure-asyncoperation` header is present.
*
* For more information on the LRO guidelines, refer to: https://github.com/microsoft/api-guidelines/blob/vNext/azure/Guidelines.md#long-running-operations-with-status-monitor
*/
export const phoneNumbersLroPolicy: PipelinePolicy = {
name: "phoneNumbersLroPolicy",
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
const response = await next(request);

const operationLocation = response.headers?.get("operation-location");
if (operationLocation) {
response.headers.set("azure-asyncoperation", operationLocation);
}

return response;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Configuration

```yaml
package-name: azure-communication-phone-numbers
package-name: "@azure/communication-phone-numbers"
description: Phone number configuration client
package-version: 1.2.0-beta.2
generate-metadata: false
Expand All @@ -23,7 +23,6 @@ azure-arm: false
skip-enum-validation: true
title: Phone Numbers Client
v3: true
use-core-v2: true
```

## Customizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
// Licensed under the MIT license.

import { FullOperationResponse } from "@azure/core-client";
import {
HttpHeaders,
PipelineRequest,
PipelineResponse,
createHttpHeaders,
} from "@azure/core-rest-pipeline";
import { PipelineRequest, PipelineResponse, createHttpHeaders } from "@azure/core-rest-pipeline";
import { assert } from "chai";
import {
createPhoneNumbersPagingPolicy,
phoneNumbersLroPolicy,
} from "../../src/utils/customPipelinePolicies";
import { createPhoneNumbersPagingPolicy } from "../../src/utils/customPipelinePolicies";

describe("phoneNumbersPagingPolicy", function () {
const endpoint = "https://contoso.spool.azure.local";
Expand Down Expand Up @@ -79,39 +71,3 @@ describe("phoneNumbersPagingPolicy", function () {
);
});
});

describe("phoneNumbersLroPolicy", function () {
const request: PipelineRequest = {
url: "https://contoso.spool.azure.local/availablePhoneNumbers/countries/{countryCode}/:search",
method: "POST",
headers: createHttpHeaders(),
timeout: 0,
withCredentials: false,
requestId: "any-id",
};

async function createMockResponse(headers: HttpHeaders) {
return Promise.resolve({ headers }) as unknown as PipelineResponse;
}

it("sets azure-asyncoperation header", async function () {
const headers = createHttpHeaders();
headers.set("operation-location", "/phoneNumbers/operations/search_12345");
const response = await phoneNumbersLroPolicy.sendRequest(request, (_request) =>
createMockResponse(headers)
);
assert.exists(response.headers.get("azure-asyncoperation"));
assert.equal(
response.headers.get("azure-asyncoperation"),
"/phoneNumbers/operations/search_12345"
);
});

it("does not mutate headers without operation-location", async function () {
const response = await phoneNumbersLroPolicy.sendRequest(request, (_request) =>
createMockResponse(createHttpHeaders())
);
assert.isUndefined(response.headers.get("azure-asyncoperation"));
assert.deepEqual(response.headers, createHttpHeaders());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe("PhoneNumbersClient - headers", function () {
const userAgentHeader = isNode ? "user-agent" : "x-ms-useragent";
assert.match(
request.headers.get(userAgentHeader) as string,
new RegExp(`azsdk-js-azure-communication-phone-numbers/${SDK_VERSION}`, "g")
new RegExp(`azsdk-js-communication-phone-numbers/${SDK_VERSION}`, "g")
);
});

Expand Down Expand Up @@ -112,7 +112,7 @@ describe("PhoneNumbersClient - headers", function () {
assert.match(
request.headers.get(userAgentHeader) as string,
new RegExp(
`phonenumbersclient-headers-test azsdk-js-azure-communication-phone-numbers/${SDK_VERSION}`,
`phonenumbersclient-headers-test azsdk-js-communication-phone-numbers/${SDK_VERSION}`,
"g"
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,14 @@

import { AzureKeyCredential } from "@azure/core-auth";
import { assert } from "chai";
import { SearchAvailablePhoneNumbersRequest } from "../../src/models";
import { PhoneNumbersClient } from "../../src/phoneNumbersClient";
import {
mockListPhoneNumbersHttpClient,
mockSearchHttpClient,
} from "../public/utils/mockHttpClients";
import { mockListPhoneNumbersHttpClient } from "../public/utils/mockHttpClients";

describe("PhoneNumbersClient - custom policies ", function () {
const endpoint = "https://contoso.spool.azure.local";
const accessKey = "banana";
let client: PhoneNumbersClient;

it("applies the phoneNumbersLroPolicy", async function () {
client = new PhoneNumbersClient(endpoint, new AzureKeyCredential(accessKey), {
httpClient: mockSearchHttpClient,
});

const searchRequest: SearchAvailablePhoneNumbersRequest = {
countryCode: "US",
phoneNumberType: "tollFree",
assignmentType: "application",
capabilities: {
sms: "none",
calling: "outbound",
},
};

const poller = await client.beginSearchAvailablePhoneNumbers(searchRequest, {
onResponse: (_response) => {
assert.isDefined(_response.headers.get("azure-asyncoperation"));
assert.equal(
_response.headers.get("azure-asyncopearation"),
_response.headers.get("opearation-location")
);
},
});

// It is only required to poll once to check if the initial request contains the header
await poller.poll();
});

it("applies the phoneNumbersPagingPolicy", async function () {
client = new PhoneNumbersClient(endpoint, new AzureKeyCredential(accessKey), {
httpClient: mockListPhoneNumbersHttpClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
HttpHeaders,
PipelineRequest,
PipelineResponse,
createHttpHeaders,
} from "@azure/core-rest-pipeline";
import { PurchasedPhoneNumber } from "../../../src";
import { PurchasedPhoneNumbers } from "../../../src/generated/src/models";
Expand Down Expand Up @@ -49,26 +48,6 @@ export const getPhoneNumberHttpClient: HttpClient = createMockHttpClient<Purchas
}
);

function createMockSearchResponseHeaders(): HttpHeaders {
const headers = createHttpHeaders();
headers.set(
"Operation-Location",
"/phoneNumbers/operations/search_378ddf60-81be-452a-ba4f-613198ea6c28"
);
headers.set(
"Location",
"/availablePhoneNumbers/searchResults/378ddf60-81be-452a-ba4f-613198ea6c28"
);
headers.set("operation-id", "search_378ddf60-81be-452a-ba4f-613198ea6c28");
headers.set("search-id", "378ddf60-81be-452a-ba4f-613198ea6c28");
return headers;
}
export const mockSearchHttpClient = createMockHttpClient(
202,
null,
createMockSearchResponseHeaders()
);

export const mockListPhoneNumbersHttpClient = createMockHttpClient<PurchasedPhoneNumbers>(200, {
phoneNumbers: [
{
Expand Down