use of com.azure.android.communication.common.CommunicationTokenCredential in project communication-services-android-calling-hero by Azure-Samples.
the class CallingContext method createTokenCredential.
// endregion
// region Private Methods
private void createTokenCredential() {
final CommunicationTokenCredential credential = new CommunicationTokenCredential(new CommunicationTokenRefreshOptions(tokenFetcher, true));
communicationUserCredentialCompletableFuture.complete(credential);
}
use of com.azure.android.communication.common.CommunicationTokenCredential 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();
}
use of com.azure.android.communication.common.CommunicationTokenCredential 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);
}
use of com.azure.android.communication.common.CommunicationTokenCredential in project azure-sdk-for-android by Azure.
the class MainActivity method createChatAsyncClient.
public void createChatAsyncClient() {
try {
chatAsyncClient = new ChatClientBuilder().endpoint(endpoint).credential(new CommunicationTokenCredential(firstUserAccessToken)).addPolicy(new UserAgentPolicy(APPLICATION_ID, SDK_NAME, sdkVersion)).httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS).addAllowedHeaderName("MS-CV")).buildAsyncClient();
Log.d(TAG, "Created ChatAsyncClient");
} catch (Exception e) {
Log.e("ChatAsyncClient creation failed", Objects.requireNonNull(e.getMessage()));
}
}
use of com.azure.android.communication.common.CommunicationTokenCredential in project azure-sdk-for-android by Azure.
the class ChatClientTestBase method getChatClientBuilder.
public ChatClientBuilder getChatClientBuilder(HttpClient httpClient) {
ChatClientBuilder builder = new ChatClientBuilder();
builder.endpoint(ENDPOINT).httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
if (interceptorManager.isPlaybackMode()) {
builder.credential(new CommunicationTokenCredential(generateRawToken()));
return builder;
} else {
builder.credential(new CommunicationTokenCredential(ACCESS_KEY));
}
if (getTestMode() == TestMode.RECORD) {
builder.addPolicy(interceptorManager.getRecordPolicy());
}
return builder;
}
Aggregations