-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Azure Communication Service - Phone Number Administration SDK #15497
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
Changes from all commits
4b5ee61
7b0eea4
5a92026
a97ccae
749ae4b
4770006
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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."); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.")); | ||
waynemo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.