Search in sources :

Example 36 with HttpRequest

use of com.azure.android.core.http.HttpRequest 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 37 with HttpRequest

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

the class ChatImplClientTestBase method getChatClientBuilder.

public AzureCommunicationChatServiceImplBuilder getChatClientBuilder(HttpClient httpClient) {
    AzureCommunicationChatServiceImplBuilder builder = new AzureCommunicationChatServiceImplBuilder();
    builder.endpoint(ENDPOINT).httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
    if (interceptorManager.isPlaybackMode()) {
        builder.addPolicy(chain -> {
            HttpRequest request = chain.getRequest();
            request.getHeaders().put("Authorization", "Bearer " + generateRawToken());
            chain.processNextPolicy(request);
        });
        return builder;
    } else {
        builder.addPolicy(chain -> {
            HttpRequest request = chain.getRequest();
            request.getHeaders().put("Authorization", "Bearer " + ACCESS_KEY);
            chain.processNextPolicy(request);
        });
    }
    if (getTestMode() == TestMode.RECORD) {
        builder.addPolicy(interceptorManager.getRecordPolicy());
    }
    return builder;
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest)

Aggregations

HttpRequest (com.azure.android.core.http.HttpRequest)37 HttpResponse (com.azure.android.core.http.HttpResponse)22 HttpCallback (com.azure.android.core.http.HttpCallback)18 CountDownLatch (java.util.concurrent.CountDownLatch)18 HttpPipeline (com.azure.android.core.http.HttpPipeline)17 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)17 CancellationToken (com.azure.android.core.util.CancellationToken)15 Test (org.junit.jupiter.api.Test)13 HttpHeaders (com.azure.android.core.http.HttpHeaders)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 HttpClient (com.azure.android.core.http.HttpClient)4 UrlBuilder (com.azure.android.core.http.util.UrlBuilder)4 IOException (java.io.IOException)4 HttpHeader (com.azure.android.core.http.HttpHeader)3 ClientLogger (com.azure.android.core.logging.ClientLogger)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 List (java.util.List)3 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 LIBRARY_PACKAGE_NAME (com.azure.android.communication.chat.BuildConfig.LIBRARY_PACKAGE_NAME)2