use of com.azure.android.core.http.policy.RetryPolicy 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);
}
Aggregations