Search in sources :

Example 1 with HttpPipelinePolicy

use of com.azure.android.core.http.HttpPipelinePolicy in project azure-sdk-for-android by Azure.

the class ChatThreadClientBuilder method createHttpPipeline.

private HttpPipeline createHttpPipeline(HttpClient httpClient, HttpPipelinePolicy authorizationPolicy, List<HttpPipelinePolicy> additionalPolicies) {
    List<HttpPipelinePolicy> policies = new ArrayList<HttpPipelinePolicy>();
    applyRequiredPolicies(policies, authorizationPolicy);
    if (additionalPolicies != null && additionalPolicies.size() > 0) {
        policies.addAll(additionalPolicies);
    }
    return new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
}
Also used : HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) ArrayList(java.util.ArrayList)

Example 2 with HttpPipelinePolicy

use of com.azure.android.core.http.HttpPipelinePolicy in project azure-sdk-for-android by Azure.

the class ChatThreadClientBuilder method createInternalClient.

private AzureCommunicationChatServiceImpl createInternalClient() {
    if (endpoint == null) {
        throw logger.logExceptionAsError(new NullPointerException("'endpoint' is required."));
    }
    HttpPipeline pipeline;
    if (this.httpPipeline != null) {
        pipeline = this.httpPipeline;
    } else {
        if (this.communicationTokenCredential == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationTokenCredential is required."));
        }
        HttpPipelinePolicy authorizationPolicy = chain -> {
            final CompletableFuture<CommunicationAccessToken> tokenFuture = this.communicationTokenCredential.getToken();
            final CommunicationAccessToken token;
            try {
                token = tokenFuture.get();
            } catch (ExecutionException e) {
                chain.completedError(e);
                return;
            } catch (InterruptedException e) {
                chain.completedError(e);
                return;
            }
            HttpRequest httpRequest = chain.getRequest();
            httpRequest.getHeaders().put("Authorization", "Bearer " + token.getToken());
            chain.processNextPolicy(httpRequest);
        };
        pipeline = createHttpPipeline(this.httpClient, authorizationPolicy, this.customPolicies);
    }
    AzureCommunicationChatServiceImplBuilder clientBuilder = new AzureCommunicationChatServiceImplBuilder().apiVersion((this.serviceVersion == null) ? ChatServiceVersion.getLatest().getVersion() : this.serviceVersion.getVersion()).endpoint(this.endpoint).pipeline(pipeline);
    return clientBuilder.buildClient();
}
Also used : AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) AzureCommunicationChatServiceImpl(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImpl) VERSION_NAME(com.azure.android.communication.chat.BuildConfig.VERSION_NAME) RetryPolicy(com.azure.android.core.http.policy.RetryPolicy) ServiceClientBuilder(com.azure.android.core.rest.annotation.ServiceClientBuilder) UserAgentPolicy(com.azure.android.core.http.policy.UserAgentPolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) ClientLogger(com.azure.android.core.logging.ClientLogger) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) LIBRARY_PACKAGE_NAME(com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy) List(java.util.List) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipeline(com.azure.android.core.http.HttpPipeline) CookiePolicy(com.azure.android.core.http.policy.CookiePolicy) CommunicationTokenCredential(com.azure.android.communication.common.CommunicationTokenCredential) HttpClient(com.azure.android.core.http.HttpClient) HttpRequest(com.azure.android.core.http.HttpRequest) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) HttpRequest(com.azure.android.core.http.HttpRequest) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) HttpPipeline(com.azure.android.core.http.HttpPipeline) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with HttpPipelinePolicy

use of com.azure.android.core.http.HttpPipelinePolicy in project azure-sdk-for-android by Azure.

the class AzureCommunicationChatServiceImplBuilder method createHttpPipeline.

private HttpPipeline createHttpPipeline() {
    if (httpLogOptions == null) {
        httpLogOptions = new HttpLogOptions();
    }
    List<HttpPipelinePolicy> policies = new ArrayList<>();
    String clientName = properties.get(SDK_NAME);
    if (clientName == null) {
        clientName = "UnknownName";
    }
    String clientVersion = properties.get(SDK_VERSION);
    if (clientVersion == null) {
        clientVersion = "UnknownVersion";
    }
    policies.add(new UserAgentPolicy(null, clientName, clientVersion));
    policies.add(retryPolicy == null ? RetryPolicy.withExponentialBackoff() : retryPolicy);
    policies.add(new CookiePolicy());
    policies.addAll(this.pipelinePolicies);
    policies.add(new HttpLoggingPolicy(httpLogOptions));
    HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
    return httpPipeline;
}
Also used : HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) UserAgentPolicy(com.azure.android.core.http.policy.UserAgentPolicy) HttpPipeline(com.azure.android.core.http.HttpPipeline) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) ArrayList(java.util.ArrayList) CookiePolicy(com.azure.android.core.http.policy.CookiePolicy) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy)

Example 4 with HttpPipelinePolicy

use of com.azure.android.core.http.HttpPipelinePolicy in project azure-sdk-for-android by Azure.

the class ChatClientBuilder method buildAsyncClient.

/**
 * Create asynchronous client applying CommunicationTokenCredential, UserAgentPolicy,
 * RetryPolicy, and CookiePolicy.
 * Additional HttpPolicies specified by additionalPolicies will be applied after them
 *
 * @throws NullPointerException if endpoint or CommunicationTokenCredential is not set.
 * @return A {@link ChatAsyncClient} instance.
 */
public ChatAsyncClient buildAsyncClient() {
    if (this.endpoint == null) {
        throw logger.logExceptionAsError(new NullPointerException("Endpoint is required."));
    }
    HttpPipeline pipeline;
    if (this.httpPipeline != null) {
        pipeline = this.httpPipeline;
    } else {
        if (this.communicationTokenCredential == null) {
            throw logger.logExceptionAsError(new NullPointerException("CommunicationTokenCredential is required."));
        }
        HttpPipelinePolicy authorizationPolicy = chain -> {
            final CompletableFuture<CommunicationAccessToken> tokenFuture = this.communicationTokenCredential.getToken();
            final CommunicationAccessToken token;
            try {
                token = tokenFuture.get();
            } catch (ExecutionException e) {
                chain.completedError(e);
                return;
            } catch (InterruptedException e) {
                chain.completedError(e);
                return;
            }
            HttpRequest httpRequest = chain.getRequest();
            httpRequest.getHeaders().put("Authorization", "Bearer " + token.getToken());
            chain.processNextPolicy(httpRequest);
        };
        pipeline = createHttpPipeline(this.httpClient, authorizationPolicy, this.customPolicies);
    }
    AzureCommunicationChatServiceImplBuilder clientBuilder = new AzureCommunicationChatServiceImplBuilder().apiVersion((this.serviceVersion == null) ? ChatServiceVersion.getLatest().getVersion() : this.serviceVersion.getVersion()).endpoint(this.endpoint).pipeline(pipeline);
    return new ChatAsyncClient(clientBuilder.buildClient(), this.communicationTokenCredential);
}
Also used : AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) VERSION_NAME(com.azure.android.communication.chat.BuildConfig.VERSION_NAME) RetryPolicy(com.azure.android.core.http.policy.RetryPolicy) ServiceClientBuilder(com.azure.android.core.rest.annotation.ServiceClientBuilder) UserAgentPolicy(com.azure.android.core.http.policy.UserAgentPolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) ClientLogger(com.azure.android.core.logging.ClientLogger) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) LIBRARY_PACKAGE_NAME(com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) HttpLoggingPolicy(com.azure.android.core.http.policy.HttpLoggingPolicy) List(java.util.List) HttpLogOptions(com.azure.android.core.http.policy.HttpLogOptions) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipeline(com.azure.android.core.http.HttpPipeline) CookiePolicy(com.azure.android.core.http.policy.CookiePolicy) CommunicationTokenCredential(com.azure.android.communication.common.CommunicationTokenCredential) HttpClient(com.azure.android.core.http.HttpClient) HttpRequest(com.azure.android.core.http.HttpRequest) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) CommunicationAccessToken(com.azure.android.communication.common.CommunicationAccessToken) HttpRequest(com.azure.android.core.http.HttpRequest) HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) CompletableFuture(java9.util.concurrent.CompletableFuture) AzureCommunicationChatServiceImplBuilder(com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder) HttpPipeline(com.azure.android.core.http.HttpPipeline) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with HttpPipelinePolicy

use of com.azure.android.core.http.HttpPipelinePolicy in project azure-sdk-for-android by Azure.

the class ChatClientBuilder method createHttpPipeline.

private HttpPipeline createHttpPipeline(HttpClient httpClient, HttpPipelinePolicy authorizationPolicy, List<HttpPipelinePolicy> additionalPolicies) {
    List<HttpPipelinePolicy> policies = new ArrayList<HttpPipelinePolicy>();
    applyRequiredPolicies(policies, authorizationPolicy);
    if (additionalPolicies != null && additionalPolicies.size() > 0) {
        policies.addAll(additionalPolicies);
    }
    return new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(httpClient).build();
}
Also used : HttpPipelinePolicy(com.azure.android.core.http.HttpPipelinePolicy) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) ArrayList(java.util.ArrayList)

Aggregations

HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)5 HttpPipelinePolicy (com.azure.android.core.http.HttpPipelinePolicy)5 ArrayList (java.util.ArrayList)5 HttpPipeline (com.azure.android.core.http.HttpPipeline)3 CookiePolicy (com.azure.android.core.http.policy.CookiePolicy)3 HttpLogOptions (com.azure.android.core.http.policy.HttpLogOptions)3 HttpLoggingPolicy (com.azure.android.core.http.policy.HttpLoggingPolicy)3 UserAgentPolicy (com.azure.android.core.http.policy.UserAgentPolicy)3 LIBRARY_PACKAGE_NAME (com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME)2 VERSION_NAME (com.azure.android.communication.chat.BuildConfig.VERSION_NAME)2 AzureCommunicationChatServiceImplBuilder (com.azure.android.communication.chat.implementation.AzureCommunicationChatServiceImplBuilder)2 CommunicationAccessToken (com.azure.android.communication.common.CommunicationAccessToken)2 CommunicationTokenCredential (com.azure.android.communication.common.CommunicationTokenCredential)2 HttpClient (com.azure.android.core.http.HttpClient)2 HttpRequest (com.azure.android.core.http.HttpRequest)2 RetryPolicy (com.azure.android.core.http.policy.RetryPolicy)2 ClientLogger (com.azure.android.core.logging.ClientLogger)2 ServiceClientBuilder (com.azure.android.core.rest.annotation.ServiceClientBuilder)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2