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 @@ -528,6 +528,12 @@
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientCheck" files="com.azure.communication.administration.CommunicationIdentityClient.java"/>
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientCheck" files="com.azure.communication.administration.CommunicationIdentityAsyncClient.java"/>

<!-- Checkstyle suppressions for the auto generated Phone Number Administration Implementation -->
<suppress checks="[a-zA-Z0-9]*" files="com.azure.communication.administration.implementation.PhoneNumberAdministrationsImpl.java"/>
<!-- TODO: Fix with https://github.com/Azure/azure-sdk-for-java/issues#6716 Method name should follow a common vocabulary. -->
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientCheck" files="com.azure.communication.administration.PhoneNumberClient.java"/>
<suppress checks="com.azure.tools.checkstyle.checks.ServiceClientCheck" files="com.azure.communication.administration.PhoneNumberAsyncClient.java"/>

<!-- Checkstyle suppressions for azure-spring AADB2C package name-->
<suppress checks="PackageName" files="com.microsoft.azure.spring.autoconfigure.*|com.azure.spring.*"/>

Expand Down
29 changes: 29 additions & 0 deletions sdk/communication/azure-communication-administration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
<version>1.4.2</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version><!-- {x-version-update;org.mockito:mockito-core;external_dependency} -->
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -128,4 +134,27 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>is.jdk.11</id>
<activation>
<jdk>[9,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version> <!-- {x-version-update;org.apache.maven.plugins:maven-surefire-plugin;external_dependency} -->
<configuration>
<argLine>
--add-opens com.azure.communication.administration/com.azure.communication.administration=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.communication.administration;

import com.azure.communication.administration.implementation.PhoneNumberAdminClientImpl;
import com.azure.communication.administration.implementation.PhoneNumberAdminClientImplBuilder;
import com.azure.communication.common.CommunicationClientCredential;
import com.azure.communication.common.HmacAuthenticationPolicy;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.CookiePolicy;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
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.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Builder for creating clients of Communication Service phone number configuration
*/
@ServiceClientBuilder(serviceClients = {PhoneNumberClient.class, PhoneNumberAsyncClient.class})
public final class PhoneNumberClientBuilder {
private static final Map<String, String> PROPERTIES =
CoreUtils.getProperties("azure-communication-administration.properties");
private static final String SDK_NAME = "name";
private static final String SDK_VERSION = "version";

private final ClientLogger logger = new ClientLogger(PhoneNumberClientBuilder.class);

private PhoneNumberServiceVersion version;
private String endpoint;
private HttpPipeline pipeline;
private HttpClient httpClient;
private HttpLogOptions httpLogOptions;
private CommunicationClientCredential credential;
private Configuration configuration;
private final List<HttpPipelinePolicy> additionalPolicies = new ArrayList<>();

/**
* Set endpoint of the service
*
* @param endpoint url of the service
* @return The updated {@link PhoneNumberClientBuilder} object.
* @throws NullPointerException If {@code endpoint} is {@code null}.
*/
public PhoneNumberClientBuilder endpoint(String endpoint) {
this.endpoint = Objects.requireNonNull(endpoint, "'endpoint' cannot be null.");
return this;
}

/**
* Sets the HTTP pipeline to use for the service client
* <p>
* If {@code pipeline} is set, all other settings aside from
* {@link PhoneNumberClientBuilder#endpoint(String) endpoint} are ignored.
*
* @param pipeline HttpPipeline to use
* @return The updated {@link PhoneNumberClientBuilder} object.
*/
public PhoneNumberClientBuilder pipeline(HttpPipeline pipeline) {
this.pipeline = Objects.requireNonNull(pipeline, "'pipeline' cannot be null.");
Copy link
Member

Choose a reason for hiding this comment

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

This can be null in scenarios where the user initially set the pipeline but then decided to clear it out. These validations can be postponed until the build method is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be addressed in separate PR.

return this;
}

/**
* Set HttpClient to use
*
* @param httpClient HttpClient to use
* @return The updated {@link PhoneNumberClientBuilder} object.
* @throws NullPointerException If {@code httpClient} is {@code null}.
*/
public PhoneNumberClientBuilder httpClient(HttpClient httpClient) {
this.httpClient = Objects.requireNonNull(httpClient, "'httpClient' cannot be null.");
return this;
}

/**
* Sets the logging configuration for HTTP requests and responses.
*
* <p> If logLevel is not provided, default value of {@link HttpLogDetailLevel#NONE} is set.</p>
*
* @param httpLogOptions The logging configuration to use when sending and receiving HTTP requests/responses.
* @return the updated {@link PhoneNumberClientBuilder} object.
*/
public PhoneNumberClientBuilder httpLogOptions(HttpLogOptions httpLogOptions) {
this.httpLogOptions = httpLogOptions;
return this;
}

/**
* Set CommunicationClientCredential for authorization
*
* @param credential valid CommunicationClientCredential object
* @return The updated {@link PhoneNumberClientBuilder} object.
* @throws NullPointerException If {@code credential} is {@code null}.
*/
public PhoneNumberClientBuilder credential(CommunicationClientCredential credential) {
this.credential = Objects.requireNonNull(credential, "'credential' cannot be null.");
return this;
}

/**
* Sets the configuration object used to retrieve environment configuration values during building of the client.
*
* @param configuration Configuration store used to retrieve environment configurations.
* @return The updated {@link PhoneNumberClientBuilder} object.
*/
public PhoneNumberClientBuilder configuration(Configuration configuration) {
this.configuration = configuration;
return this;
}

/**
* Adds a policy to the set of existing policies that are executed after required policies.
*
* @param policy The retry policy for service requests.
* @return The updated {@link PhoneNumberClientBuilder} object.
* @throws NullPointerException If {@code policy} is {@code null}.
*/
public PhoneNumberClientBuilder addPolicy(HttpPipelinePolicy policy) {
this.additionalPolicies.add(Objects.requireNonNull(policy, "'policy' cannot be null."));
return this;
}

/**
* Sets the {@link PhoneNumberServiceVersion} that is used when making API requests.
* <p>
* If a service version is not provided, the service version that will be used will be the latest known service
* version based on the version of the client library being used. If no service version is specified, updating to a
* newer version the client library will have the result of potentially moving to a newer service version.
*
* @param version {@link PhoneNumberServiceVersion} of the service to be used when making requests.
* @return The updated {@link PhoneNumberClientBuilder} object.
*/
public PhoneNumberClientBuilder serviceVersion(PhoneNumberServiceVersion version) {
this.version = version;
return this;
}

/**
* Create synchronous client applying CommunicationClientCredentialPolicy,
* UserAgentPolicy, RetryPolicy, and CookiePolicy.
* Additional HttpPolicies specified by additionalPolicies will be applied after them
*
* @return {@link PhoneNumberClient} instance
*/
public PhoneNumberClient buildClient() {
return new PhoneNumberClient(this.buildAsyncClient());
}

/**
* Create asynchronous client applying CommunicationClientCredentialPolicy,
* UserAgentPolicy, RetryPolicy, and CookiePolicy.
* Additional HttpPolicies specified by additionalPolicies will be applied after them
*
* @return {@link PhoneNumberAsyncClient} instance
*/
public PhoneNumberAsyncClient buildAsyncClient() {
this.validateRequiredFields();

if (this.version != null) {
logger.info("Build client for service version" + this.version.getVersion());
}

return this.createPhoneNumberAsyncClient(this.createPhoneNumberAdminClient());
}

PhoneNumberAsyncClient createPhoneNumberAsyncClient(PhoneNumberAdminClientImpl phoneNumberAdminClient) {
return new PhoneNumberAsyncClient(phoneNumberAdminClient);
}

HmacAuthenticationPolicy createAuthenticationPolicy(CommunicationClientCredential communicationClientCredential) {
return new HmacAuthenticationPolicy(communicationClientCredential);
}

UserAgentPolicy createUserAgentPolicy(
String applicationId, String sdkName, String sdkVersion, Configuration configuration) {
return new UserAgentPolicy(applicationId, sdkName, sdkVersion, configuration);
}

RetryPolicy createRetryPolicy() {
return new RetryPolicy();
}

CookiePolicy createCookiePolicy() {
return new CookiePolicy();
}

HttpLoggingPolicy createHttpLoggingPolicy(HttpLogOptions httpLogOptions) {
return new HttpLoggingPolicy(httpLogOptions);
}

HttpLogOptions createDefaultHttpLogOptions() {
return new HttpLogOptions();
}

private void validateRequiredFields() {
Objects.requireNonNull(this.endpoint);

if (this.pipeline == null) {
Objects.requireNonNull(this.credential);
Objects.requireNonNull(this.httpClient);
}
}

private PhoneNumberAdminClientImpl createPhoneNumberAdminClient() {
PhoneNumberAdminClientImplBuilder clientBuilder = new PhoneNumberAdminClientImplBuilder();
return clientBuilder
.endpoint(this.endpoint)
.pipeline(this.createHttpPipeline())
.buildClient();
}

private HttpPipeline createHttpPipeline() {
if (this.pipeline != null) {
return this.pipeline;
}

List<HttpPipelinePolicy> policyList = new ArrayList<>();

// Add required policies
policyList.add(this.createAuthenticationPolicy(this.credential));
policyList.add(this.createUserAgentPolicy(
this.getHttpLogOptions().getApplicationId(),
PROPERTIES.get(SDK_NAME),
PROPERTIES.get(SDK_VERSION),
this.configuration
));
policyList.add(this.createRetryPolicy());
policyList.add(this.createCookiePolicy());

// Add additional policies
if (this.additionalPolicies.size() > 0) {
policyList.addAll(this.additionalPolicies);
}

// Add logging policy
policyList.add(this.createHttpLoggingPolicy(this.getHttpLogOptions()));

return new HttpPipelineBuilder()
.policies(policyList.toArray(new HttpPipelinePolicy[0]))
.httpClient(this.httpClient)
.build();
}

private HttpLogOptions getHttpLogOptions() {
if (this.httpLogOptions == null) {
this.httpLogOptions = this.createDefaultHttpLogOptions();
}

return this.httpLogOptions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.communication.administration;

import com.azure.core.util.ServiceVersion;

/**
* The versions of Phone Number Admin Service supported by this client library.
*/
public enum PhoneNumberServiceVersion implements ServiceVersion {
V2020_07_20_PREVIEW_1("2020-07-20-preview1");

private final String version;

PhoneNumberServiceVersion(String version) {

this.version = version;
}

/**
* {@inheritDoc}
*/
@Override
public String getVersion() {

return this.version;
}

/**
* Gets the latest service version supported by this client library
*
* @return the latest {@link PhoneNumberServiceVersion}
*/
public static PhoneNumberServiceVersion getLatest() {

return V2020_07_20_PREVIEW_1;
}
}
Loading