Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Communication] - Phone Numbers - Address API View comments #19325

Merged
merged 4 commits into from
Feb 23, 2021
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 @@ -20,6 +20,7 @@
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.util.ClientOptions;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;
Expand All @@ -37,7 +38,7 @@ public final class CommunicationIdentityClientBuilder {
private static final String SDK_NAME = "name";
private static final String SDK_VERSION = "version";

private static final String COMMUNICATION_IDENTITY_PROPERTIES =
private static final String COMMUNICATION_IDENTITY_PROPERTIES =
"azure-communication-identity.properties";

private final ClientLogger logger = new ClientLogger(CommunicationIdentityClientBuilder.class);
Expand All @@ -47,8 +48,9 @@ public final class CommunicationIdentityClientBuilder {
private HttpClient httpClient;
private HttpLogOptions httpLogOptions = new HttpLogOptions();
private HttpPipeline pipeline;
private Configuration configuration;
private final Map<String, String> properties = CoreUtils.getProperties(COMMUNICATION_IDENTITY_PROPERTIES);
private Configuration configuration;
private ClientOptions clientOptions;
private final Map<String, String> properties = CoreUtils.getProperties(COMMUNICATION_IDENTITY_PROPERTIES);
private final List<HttpPipelinePolicy> customPolicies = new ArrayList<HttpPipelinePolicy>();

/**
Expand Down Expand Up @@ -139,6 +141,18 @@ public CommunicationIdentityClientBuilder addPolicy(HttpPipelinePolicy customPol
return this;
}

/**
* Sets the client options for all the requests made through the client.
*
* @param clientOptions {@link ClientOptions}.
* @return The updated {@link CommunicationIdentityClientBuilder} object.
* @throws NullPointerException If {@code clientOptions} is {@code null}.
*/
public CommunicationIdentityClientBuilder clientOptions(ClientOptions clientOptions) {
this.clientOptions = Objects.requireNonNull(clientOptions, "'clientOptions' cannot be null.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clientOptions have applicationId which should get into UserAgent here.

policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, configuration));

This is an old PR shows how UserAgent should be populated with applicationId

https://github.com/Azure/azure-sdk-for-java/pull/16428/files#diff-57d5c797a106560d37148554ddcceb69f85c68f35b2a257823fbcc69b24a28aaR171

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Addressed

return this;
}

/**
* Sets the configuration object used to retrieve environment configuration values during building of the client.
*
Expand Down Expand Up @@ -247,14 +261,25 @@ private HttpPipeline createHttpPipeline(HttpClient httpClient,
return new HttpPipelineBuilder()
.policies(policies.toArray(new HttpPipelinePolicy[0]))
.httpClient(httpClient)
.clientOptions(clientOptions)
.build();
}

private void applyRequiredPolicies(List<HttpPipelinePolicy> policies) {
String clientName = properties.getOrDefault(SDK_NAME, "UnknownName");
String clientVersion = properties.getOrDefault(SDK_VERSION, "UnknownVersion");

policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, configuration));
ClientOptions buildClientOptions = (clientOptions == null) ? new ClientOptions() : clientOptions;
HttpLogOptions buildLogOptions = (httpLogOptions == null) ? new HttpLogOptions() : httpLogOptions;

String applicationId = null;
if (!CoreUtils.isNullOrEmpty(buildClientOptions.getApplicationId())) {
applicationId = buildClientOptions.getApplicationId();
} else if (!CoreUtils.isNullOrEmpty(buildLogOptions.getApplicationId())) {
applicationId = buildLogOptions.getApplicationId();
}

policies.add(new UserAgentPolicy(applicationId, clientName, clientVersion, configuration));
policies.add(new RetryPolicy());
policies.add(new CookiePolicy());
policies.add(new HttpLoggingPolicy(httpLogOptions));
Expand Down
35 changes: 15 additions & 20 deletions sdk/communication/azure-communication-phonenumbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PhoneNumbersClient phoneNumberClient = new PhoneNumbersClientBuilder()
.httpClient(httpClient)
.buildClient();
```
Alternatively, you can provide the entire connection string using the connectionString() function of the PhoneNumberClientBuilder instead of providing the endpoint and access key.
Alternatively, you can provide the entire connection string using the connectionString() function of the PhoneNumberClientBuilder instead of providing the endpoint and access key.

### Phone Number Types overview

Expand Down Expand Up @@ -103,20 +103,15 @@ The Phone Number Client supports a variety of long running operations that allow
### Search for Available Phone Numbers
Search for available phone numbers by providing the area code, assignment type, phone number capabilities, phone number type, and quantity. The result of the search can then be used to purchase the numbers. Note that for the toll-free phone number type, providing the area code is optional.

<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L98-L114-->
<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L98-L109-->
```java
PhoneNumberSearchRequest searchRequest = new PhoneNumberSearchRequest();
searchRequest
.setAreaCode("800") // Area code is optional for toll free numbers
.setAssignmentType(PhoneNumberAssignmentType.PERSON)
.setCapabilities(new PhoneNumberCapabilities()
.setCalling(PhoneNumberCapabilityValue.INBOUND)
.setSms(PhoneNumberCapabilityValue.INBOUND_OUTBOUND))
.setPhoneNumberType(PhoneNumberType.GEOGRAPHIC)
.setQuantity(1); // Quantity is optional, default is 1
PhoneNumberCapabilities capabilities = new PhoneNumberCapabilities()
.setCalling(PhoneNumberCapabilityType.INBOUND)
.setSms(PhoneNumberCapabilityType.INBOUND_OUTBOUND);
PhoneNumberSearchOptions searchOptions = new PhoneNumberSearchOptions().setAreaCode("800").setQuantity(1);

PhoneNumberSearchResult searchResult = phoneNumberClient
.beginSearchAvailablePhoneNumbers("US", searchRequest, Context.NONE)
.beginSearchAvailablePhoneNumbers("US", PhoneNumberType.TOLL_FREE, PhoneNumberAssignmentType.PERSON, capabilities, searchOptions, Context.NONE)
.getFinalResult();

System.out.println("Searched phone numbers: " + searchResult.getPhoneNumbers());
Expand All @@ -127,36 +122,36 @@ System.out.println("Phone number costs:" + searchResult.getCost().getAmount());
### Purchase Phone Numbers
The result of searching for phone numbers is a `PhoneNumberSearchResult`. This can be used to get the numbers' details and purchase numbers by passing in the `searchId` to the purchase number API.

<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L116-L118 -->
<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L111-L113 -->
```java
PollResponse<PhoneNumberOperation> purchaseResponse =
PollResponse<PhoneNumberOperation> purchaseResponse =
phoneNumberClient.beginPurchasePhoneNumbers(searchResult.getSearchId(), Context.NONE).waitForCompletion();
System.out.println("Purchase phone numbers is complete: " + purchaseResponse.getStatus());
```

### Release Phone Number
Releases an acquired phone number.

<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L126-L128 -->
<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L121-L123 -->
```java
PollResponse<PhoneNumberOperation> releaseResponse =
PollResponse<PhoneNumberOperation> releaseResponse =
phoneNumberClient.beginReleasePhoneNumber("+18001234567", Context.NONE).waitForCompletion();
System.out.println("Release phone number is complete: " + releaseResponse.getStatus());
```

### Updating Phone Number Capabilities
Updates Phone Number Capabilities for Calling and SMS to one of:
Updates Phone Number Capabilities for Calling and SMS to one of:
- `PhoneNumberCapabilityValue.NONE`
- `PhoneNumberCapabilityValue.INBOUND`
- `PhoneNumberCapabilityValue.OUTBOUND`
- `PhoneNumberCapabilityValue.INBOUND_OUTBOUND`

<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L138-L145 -->
<!-- embedme ./src/samples/java/com/azure/communication/phonenumbers/ReadmeSamples.java#L133-L140 -->
```java
PhoneNumberCapabilitiesRequest capabilitiesRequest = new PhoneNumberCapabilitiesRequest();
capabilitiesRequest
.setCalling(PhoneNumberCapabilityValue.INBOUND)
.setSms(PhoneNumberCapabilityValue.INBOUND_OUTBOUND);
.setCalling(PhoneNumberCapabilityType.INBOUND)
.setSms(PhoneNumberCapabilityType.INBOUND_OUTBOUND);
AcquiredPhoneNumber phoneNumber = phoneNumberClient.beginUpdatePhoneNumberCapabilities("+18001234567", capabilitiesRequest, Context.NONE).getFinalResult();

System.out.println("Phone Number Calling capabilities: " + phoneNumber.getCapabilities().getCalling()); //Phone Number Calling capabilities: inbound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import com.azure.communication.phonenumbers.implementation.PhoneNumbersImpl;
import com.azure.communication.phonenumbers.implementation.models.PhoneNumbersPurchasePhoneNumbersResponse;
import com.azure.communication.phonenumbers.implementation.models.PhoneNumberPurchaseRequest;
import com.azure.communication.phonenumbers.implementation.models.PhoneNumberSearchRequest;
import com.azure.communication.phonenumbers.implementation.models.PhoneNumbersSearchAvailablePhoneNumbersResponse;
import com.azure.communication.phonenumbers.implementation.models.PhoneNumbersReleasePhoneNumberResponse;
import com.azure.communication.phonenumbers.implementation.models.PhoneNumbersUpdateCapabilitiesResponse;
import com.azure.communication.phonenumbers.models.AcquiredPhoneNumber;
import com.azure.communication.phonenumbers.models.PhoneNumberAssignmentType;
import com.azure.communication.phonenumbers.models.PhoneNumberCapabilities;
import com.azure.communication.phonenumbers.models.PhoneNumberCapabilitiesRequest;
import com.azure.communication.phonenumbers.models.PhoneNumberOperation;
import com.azure.communication.phonenumbers.models.PhoneNumberOperationStatus;
import com.azure.communication.phonenumbers.models.PhoneNumberSearchRequest;
import com.azure.communication.phonenumbers.models.PhoneNumberSearchOptions;
import com.azure.communication.phonenumbers.models.PhoneNumberSearchResult;
import com.azure.communication.phonenumbers.models.PhoneNumberType;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
Expand All @@ -38,7 +42,7 @@
import static com.azure.core.util.FluxUtil.withContext;

/**
* Asynchronous client for Communication service phone number operations
* Asynchronous client for Communication service phone number operations.
*/
@ServiceClient(builder = PhoneNumbersClientBuilder.class, isAsync = true)
public final class PhoneNumbersAsyncClient {
Expand Down Expand Up @@ -98,22 +102,42 @@ public PagedFlux<AcquiredPhoneNumber> listPhoneNumbers() {
* Starts the search for available phone numbers to purchase.
*
* @param countryCode The ISO 3166-2 country code.
* @param searchRequest {@link PhoneNumberSearchRequest} specifying the search request
* until it gets a result from the server
* @return A {@link PollerFlux} object with the reservation result
* @param phoneNumberType {@link PhoneNumberType} The phone number type.
* @param assignmentType {@link PhoneNumberAssignmentType} The phone number assignment type.
* @param capabilities {@link PhoneNumberCapabilities} The phone number capabilities.
* @param searchOptions The phone number search options.
* @return A {@link PollerFlux} object with the reservation result.
* @throws NullPointerException if {@code countryCode} or {@code searchRequest} is null.
*/
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
public PollerFlux<PhoneNumberOperation, PhoneNumberSearchResult> beginSearchAvailablePhoneNumbers(
String countryCode, PhoneNumberSearchRequest searchRequest) {
return beginSearchAvailablePhoneNumbers(countryCode, searchRequest, null);
String countryCode, PhoneNumberType phoneNumberType, PhoneNumberAssignmentType assignmentType,
PhoneNumberCapabilities capabilities, PhoneNumberSearchOptions searchOptions) {
return beginSearchAvailablePhoneNumbers(countryCode, phoneNumberType, assignmentType, capabilities, searchOptions, null);
}

PollerFlux<PhoneNumberOperation, PhoneNumberSearchResult> beginSearchAvailablePhoneNumbers(
String countryCode, PhoneNumberSearchRequest searchRequest, Context context) {
String countryCode, PhoneNumberType phoneNumberType, PhoneNumberAssignmentType assignmentType,
PhoneNumberCapabilities capabilities, PhoneNumberSearchOptions searchOptions, Context context) {
try {
Objects.requireNonNull(countryCode, "'countryCode' cannot be null.");
Objects.requireNonNull(searchRequest, "'searchRequest' cannot be null.");
Objects.requireNonNull(phoneNumberType, "'phoneNumberType' cannot be null.");
Objects.requireNonNull(assignmentType, "'assignmentType' cannot be null.");
Objects.requireNonNull(capabilities, "'capabilities' cannot be null.");

String areaCode = null;
Integer quantity = null;
if (searchOptions != null) {
areaCode = searchOptions.getAreaCode();
quantity = searchOptions.getQuantity();
}
PhoneNumberSearchRequest searchRequest = new PhoneNumberSearchRequest();
searchRequest
.setPhoneNumberType(phoneNumberType)
.setAssignmentType(assignmentType)
.setCapabilities(capabilities)
.setAreaCode(areaCode)
.setQuantity(quantity);

return new PollerFlux<>(defaultPollInterval,
searchAvailableNumbersInitOperation(countryCode, searchRequest, context),
Expand Down Expand Up @@ -145,7 +169,7 @@ PollerFlux<PhoneNumberOperation, PhoneNumberSearchResult> beginSearchAvailablePh

private Function<PollingContext<PhoneNumberOperation>, Mono<PollResponse<PhoneNumberOperation>>>
pollOperation() {
return (pollingContext) -> {
return (pollingContext) -> {
return client.getOperationAsync(pollingContext.getData("operationId"))
.flatMap(operation -> {
if (operation.getStatus().toString().equalsIgnoreCase(PhoneNumberOperationStatus.SUCCEEDED.toString())) {
Expand Down Expand Up @@ -179,7 +203,7 @@ PollerFlux<PhoneNumberOperation, PhoneNumberSearchResult> beginSearchAvailablePh
return Mono.empty();
};
}

private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberSearchResult>>
searchAvailableNumbersFetchFinalResultOperation() {
return (pollingContext) -> {
Expand All @@ -190,7 +214,7 @@ PollerFlux<PhoneNumberOperation, PhoneNumberSearchResult> beginSearchAvailablePh
/**
* Starts the purchase of the phone number(s) in the search result associated with a given id.
*
* @param searchId ID of the search
* @param searchId ID of the search.
* @return A {@link PollerFlux} object.
* @throws NullPointerException if {@code searchId} is null.
*/
Expand All @@ -212,7 +236,7 @@ PollerFlux<PhoneNumberOperation, Void> beginPurchasePhoneNumbers(String searchId
}
}

private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberOperation>>
private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberOperation>>
purchaseNumbersInitOperation(String searchId, Context context) {
return (pollingContext) -> {
return withContext(contextValue -> {
Expand All @@ -231,7 +255,7 @@ PollerFlux<PhoneNumberOperation, Void> beginPurchasePhoneNumbers(String searchId
/**
* Begins release of an acquired phone number.
*
* This function returns a Long Running Operation poller that allows you to wait indefinitely until the
* This function returns a Long Running Operation poller that allows you to wait indefinitely until the
* operation is complete.
* @param phoneNumber The phone number id in E.164 format. The leading plus can be either + or encoded
* as %2B.
Expand All @@ -256,7 +280,7 @@ PollerFlux<PhoneNumberOperation, Void> beginReleasePhoneNumber(String phoneNumbe
}
}

private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberOperation>>
private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberOperation>>
releaseNumberInitOperation(String phoneNumber, Context context) {
return (pollingContext) -> {
return withContext(contextValue -> {
Expand All @@ -281,12 +305,12 @@ PollerFlux<PhoneNumberOperation, Void> beginReleasePhoneNumber(String phoneNumbe
* @throws NullPointerException if {@code phoneNumber} or {@code capabilitiesUpdateRequest} is null.
*/
@ServiceMethod(returns = ReturnType.LONG_RUNNING_OPERATION)
public PollerFlux<PhoneNumberOperation, AcquiredPhoneNumber>
public PollerFlux<PhoneNumberOperation, AcquiredPhoneNumber>
beginUpdatePhoneNumberCapabilities(String phoneNumber, PhoneNumberCapabilitiesRequest capabilitiesUpdateRequest) {
return beginUpdatePhoneNumberCapabilities(phoneNumber, capabilitiesUpdateRequest, null);
}

PollerFlux<PhoneNumberOperation, AcquiredPhoneNumber>
PollerFlux<PhoneNumberOperation, AcquiredPhoneNumber>
beginUpdatePhoneNumberCapabilities(String phoneNumber, PhoneNumberCapabilitiesRequest capabilitiesUpdateRequest, Context context) {
try {
Objects.requireNonNull(phoneNumber, "'phoneNumber' cannot be null.");
Expand All @@ -302,7 +326,7 @@ PollerFlux<PhoneNumberOperation, Void> beginReleasePhoneNumber(String phoneNumbe
}
}

private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberOperation>>
private Function<PollingContext<PhoneNumberOperation>, Mono<PhoneNumberOperation>>
updateNumberCapabilitiesInitOperation(String phoneNumber, PhoneNumberCapabilitiesRequest capabilitiesUpdateRequest, Context context) {
return (pollingContext) -> {
return withContext(contextValue -> {
Expand Down
Loading