Skip to content

Commit

Permalink
PR review fix (#22138)
Browse files Browse the repository at this point in the history
* PR review fix

* PR review fix

* pr review fix
  • Loading branch information
arifsaikat-microsoft authored Jun 8, 2021
1 parent 4459456 commit 15b62ae
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 186 deletions.
11 changes: 2 additions & 9 deletions sdk/communication/azure-communication-callingserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,12 @@ Based on if the Contoso app join a call or not, APIs can be divided into two cat
Once you initialized a `CallClient` class, you can do the following chat operations:
<!-- embedme src/samples/java/com/azure/communication/callingserver/ReadmeSamples.java#L31-L40 -->
```java
// You can find your endpoint and access key from your resource in the Azure Portal
String endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com";

// Your connectionString retrieved from your Azure Communication Service
String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>";

// Initialize the call client
final CallClientBuilder builder = new CallClientBuilder();
builder.endpoint(endpoint)
.connectionString(connectionString);
builder.connectionString(connectionString);
CallClient callClient = builder.buildClient();
```

Expand Down Expand Up @@ -117,15 +113,12 @@ callClient.deleteCall(callId);
Create a ConverationClient:
<!-- embedme src/samples/java/com/azure/communication/callingserver/ConversationClientReadmeSamples.java#L27-L36 -->
```java
String endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com";

// Your connectionString retrieved from your Azure Communication Service
String connectionString = "https://<resource-name>.communication.azure.com/;<access-key>";

// Initialize the call client
final ConversationClientBuilder builder = new ConversationClientBuilder();
builder.endpoint(endpoint)
.connectionString(connectionString);
builder.connectionString(connectionString);
ConversationClient conversationClient = builder.buildClient();
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.azure.communication.callingserver.implementation.converters.CommunicationIdentifierConverter;
import com.azure.communication.callingserver.implementation.converters.AddParticipantConverter;
import com.azure.communication.callingserver.implementation.converters.PlayAudioConverter;
import com.azure.communication.callingserver.implementation.converters.ServerCallingErrorConverter;
import com.azure.communication.callingserver.implementation.models.CommunicationErrorException;
import com.azure.communication.callingserver.implementation.models.PhoneNumberIdentifierModel;
import com.azure.communication.callingserver.implementation.models.PlayAudioRequest;
import com.azure.communication.callingserver.models.CallModality;
Expand All @@ -29,6 +31,8 @@
import com.azure.communication.callingserver.models.CreateCallResponse;
import com.azure.communication.callingserver.models.PlayAudioOptions;
import com.azure.communication.callingserver.models.PlayAudioResponse;
import com.azure.communication.callingserver.models.ServerCallingError;
import com.azure.communication.callingserver.models.ServerCallingErrorException;
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
Expand Down Expand Up @@ -68,7 +72,8 @@ public Mono<CreateCallResponse> createCall(CommunicationIdentifier source,
Objects.requireNonNull(targets, "'targets' cannot be null.");
Objects.requireNonNull(createCallOptions, "'createCallOptions' cannot be null.");
CreateCallRequestInternal request = createCreateCallRequest(source, targets, createCallOptions);
return this.callClient.createCallAsync(request);
return this.callClient.createCallAsync(request)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand Down Expand Up @@ -100,7 +105,8 @@ Mono<Response<CreateCallResponse>> createCallWithResponse(CommunicationIdentifie
CreateCallRequestInternal request = createCreateCallRequest(source, targets, createCallOptions);
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.createCallWithResponseAsync(request, contextValue);
return this.callClient.createCallWithResponseAsync(request, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand All @@ -127,8 +133,12 @@ public Mono<PlayAudioResponse> playAudio(String callId,
String audioFileId,
String callbackUri,
String operationContext) {
PlayAudioRequest playAudioRequest =
PlayAudioConverter.convert(audioFileUri, loop, audioFileId, callbackUri, operationContext);
PlayAudioRequest playAudioRequest = new PlayAudioRequest();
playAudioRequest.setAudioFileUri(audioFileUri);
playAudioRequest.setLoop(loop);
playAudioRequest.setAudioFileId(audioFileId);
playAudioRequest.setOperationContext(operationContext);
playAudioRequest.setCallbackUri(callbackUri);
return playAudio(callId, playAudioRequest);
}

Expand All @@ -155,7 +165,8 @@ Mono<PlayAudioResponse> playAudio(String callId, PlayAudioRequest playAudioReque
try {
Objects.requireNonNull(callId, "'callId' cannot be null.");
Objects.requireNonNull(playAudioRequest.getAudioFileUri(), "'audioFileUri' cannot be null.");
return this.callClient.playAudioAsync(callId, playAudioRequest);
return this.callClient.playAudioAsync(callId, playAudioRequest)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand All @@ -181,8 +192,12 @@ public Mono<Response<PlayAudioResponse>> playAudioWithResponse(String callId,
String audioFileId,
String callbackUri,
String operationContext) {
PlayAudioRequest playAudioRequest =
PlayAudioConverter.convert(audioFileUri, loop, audioFileId, callbackUri, operationContext);
PlayAudioRequest playAudioRequest = new PlayAudioRequest();
playAudioRequest.setAudioFileUri(audioFileUri);
playAudioRequest.setLoop(loop);
playAudioRequest.setAudioFileId(audioFileId);
playAudioRequest.setOperationContext(operationContext);
playAudioRequest.setCallbackUri(callbackUri);
return playAudioWithResponse(callId, playAudioRequest, Context.NONE);
}

Expand Down Expand Up @@ -212,7 +227,8 @@ Mono<Response<PlayAudioResponse>> playAudioWithResponse(String callId,
Objects.requireNonNull(playAudioRequest.getAudioFileUri(), "'audioFileUri' cannot be null.");
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.playAudioWithResponseAsync(callId, playAudioRequest, contextValue);
return this.callClient.playAudioWithResponseAsync(callId, playAudioRequest, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand All @@ -229,7 +245,8 @@ Mono<Response<PlayAudioResponse>> playAudioWithResponse(String callId,
public Mono<Void> hangupCall(String callId) {
try {
Objects.requireNonNull(callId, "'callId' cannot be null.");
return this.callClient.hangupCallAsync(callId);
return this.callClient.hangupCallAsync(callId)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand All @@ -251,7 +268,8 @@ Mono<Response<Void>> hangupCallWithResponse(String callId, Context context) {
Objects.requireNonNull(callId, "'callId' cannot be null.");
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.hangupCallWithResponseAsync(callId, contextValue);
return this.callClient.hangupCallWithResponseAsync(callId, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand All @@ -268,7 +286,8 @@ Mono<Response<Void>> hangupCallWithResponse(String callId, Context context) {
public Mono<Void> deleteCall(String callId) {
try {
Objects.requireNonNull(callId, "'callId' cannot be null.");
return this.callClient.deleteCallAsync(callId);
return this.callClient.deleteCallAsync(callId)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand All @@ -290,7 +309,8 @@ Mono<Response<Void>> deleteCallWithResponse(String callId, Context context) {
Objects.requireNonNull(callId, "'callId' cannot be null.");
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.deleteCallWithResponseAsync(callId, contextValue);
return this.callClient.deleteCallWithResponseAsync(callId, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand All @@ -310,7 +330,8 @@ public Mono<CancelAllMediaOperationsResponse> cancelAllMediaOperations(String ca
Objects.requireNonNull(callId, "'callId' cannot be null.");
CancelAllMediaOperationsRequest request = new CancelAllMediaOperationsRequest();
request.setOperationContext(operationContext);
return this.callClient.cancelAllMediaOperationsAsync(callId, request);
return this.callClient.cancelAllMediaOperationsAsync(callId, request)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand Down Expand Up @@ -338,7 +359,8 @@ Mono<Response<CancelAllMediaOperationsResponse>> cancelAllMediaOperationsWithRes
request.setOperationContext(operationContext);
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.cancelAllMediaOperationsWithResponseAsync(callId, request, contextValue);
return this.callClient.cancelAllMediaOperationsWithResponseAsync(callId, request, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand Down Expand Up @@ -366,7 +388,8 @@ public Mono<Void> addParticipant(String callId,
alternateCallerId,
operationContext,
null);
return this.callClient.inviteParticipantsAsync(callId, request);
return this.callClient.inviteParticipantsAsync(callId, request)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand Down Expand Up @@ -404,7 +427,8 @@ Mono<Response<Void>> addParticipantWithResponse(String callId,
InviteParticipantsRequest request = AddParticipantConverter.convert(participant, alternateCallerId, operationContext, null);
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.inviteParticipantsWithResponseAsync(callId, request, contextValue);
return this.callClient.inviteParticipantsWithResponseAsync(callId, request, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand All @@ -423,7 +447,8 @@ public Mono<Void> removeParticipant(String callId, String participantId) {
try {
Objects.requireNonNull(callId, "'callId' cannot be null.");
Objects.requireNonNull(participantId, "'participantId' cannot be null.");
return this.callClient.removeParticipantAsync(callId, participantId);
return this.callClient.removeParticipantAsync(callId, participantId)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
Expand All @@ -450,7 +475,8 @@ Mono<Response<Void>> removeParticipantWithResponse(String callId, String partici
Objects.requireNonNull(participantId, "'participantId' cannot be null.");
return withContext(contextValue -> {
contextValue = context == null ? contextValue : context;
return this.callClient.removeParticipantWithResponseAsync(callId, participantId, contextValue);
return this.callClient.removeParticipantWithResponseAsync(callId, participantId, contextValue)
.onErrorMap(CommunicationErrorException.class, e -> translateException(e));
});
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand Down Expand Up @@ -493,4 +519,12 @@ private CreateCallRequestInternal createCreateCallRequest(CommunicationIdentifie

return request;
}

private ServerCallingErrorException translateException(CommunicationErrorException exception) {
ServerCallingError error = null;
if (exception.getValue() != null) {
error = ServerCallingErrorConverter.convert(exception.getValue());
}
return new ServerCallingErrorException(exception.getMessage(), exception.getResponse(), error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ public PlayAudioResponse playAudio(String callId,
String audioFileId,
String callbackUri,
String operationContext) {
PlayAudioRequest playAudioRequest =
PlayAudioConverter.convert(audioFileUri, loop, audioFileId, callbackUri, operationContext);
PlayAudioRequest playAudioRequest = new PlayAudioRequest();
playAudioRequest.setAudioFileUri(audioFileUri);
playAudioRequest.setLoop(loop);
playAudioRequest.setAudioFileId(audioFileId);
playAudioRequest.setOperationContext(operationContext);
playAudioRequest.setCallbackUri(callbackUri);
return callAsyncClient.playAudio(callId, playAudioRequest).block();
}

Expand All @@ -107,8 +111,12 @@ public Response<PlayAudioResponse> playAudioWithResponse(String callId,
String callbackUri,
String operationContext,
Context context) {
PlayAudioRequest playAudioRequest =
PlayAudioConverter.convert(audioFileUri, loop, audioFileId, callbackUri, operationContext);
PlayAudioRequest playAudioRequest = new PlayAudioRequest();
playAudioRequest.setAudioFileUri(audioFileUri);
playAudioRequest.setLoop(loop);
playAudioRequest.setAudioFileId(audioFileId);
playAudioRequest.setOperationContext(operationContext);
playAudioRequest.setCallbackUri(callbackUri);
return callAsyncClient.playAudioWithResponse(callId, playAudioRequest, context).block();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class CallClientBuilder {
private static final String APP_CONFIG_PROPERTIES = "azure-communication-callingserver.properties";

private final ClientLogger logger = new ClientLogger(CallClientBuilder.class);
private String connectionString;
private String endpoint;
private AzureKeyCredential azureKeyCredential;
private TokenCredential tokenCredential;
Expand Down Expand Up @@ -102,18 +103,14 @@ public CallClientBuilder credential(AzureKeyCredential keyCredential) {
}

/**
* Set endpoint and credential to use
* Set connectionString to use
*
* @param connectionString connection string for setting endpoint and
* initalizing AzureKeyCredential
* @param connectionString connection string to set endpoint and initialize AzureKeyCredential
* @return CallClientBuilder
*/
public CallClientBuilder connectionString(String connectionString) {
Objects.requireNonNull(connectionString, "'connectionString' cannot be null.");
CommunicationConnectionString connectionStringObject = new CommunicationConnectionString(connectionString);
String endpoint = connectionStringObject.getEndpoint();
String accessKey = connectionStringObject.getAccessKey();
this.endpoint(endpoint).credential(new AzureKeyCredential(accessKey));
this.connectionString = connectionString;
return this;
}

Expand Down Expand Up @@ -220,10 +217,43 @@ public CallClient buildClient() {
}

private AzureCommunicationCallingServerServiceImpl createServiceImpl() {
boolean isConnectionStringSet = connectionString != null && !connectionString.trim().isEmpty();
boolean isEndpointSet = endpoint != null && !endpoint.trim().isEmpty();
boolean isAzureKeyCredentialSet = azureKeyCredential != null;
boolean isTokenCredentialSet = tokenCredential != null;

if (isConnectionStringSet && isEndpointSet) {
throw logger.logExceptionAsError(new IllegalArgumentException(
"Both 'connectionString' and 'endpoint' are set. Just one may be used."));
}

if (isConnectionStringSet && isAzureKeyCredentialSet) {
throw logger.logExceptionAsError(new IllegalArgumentException(
"Both 'connectionString' and 'keyCredential' are set. Just one may be used."));
}

if (isConnectionStringSet && isTokenCredentialSet) {
throw logger.logExceptionAsError(new IllegalArgumentException(
"Both 'connectionString' and 'tokenCredential' are set. Just one may be used."));
}

if (isAzureKeyCredentialSet && isTokenCredentialSet) {
throw logger.logExceptionAsError(new IllegalArgumentException(
"Both 'tokenCredential' and 'keyCredential' are set. Just one may be used."));
}

if (isConnectionStringSet) {
CommunicationConnectionString connectionStringObject = new CommunicationConnectionString(connectionString);
String endpoint = connectionStringObject.getEndpoint();
String accessKey = connectionStringObject.getAccessKey();
this.endpoint(endpoint).credential(new AzureKeyCredential(accessKey));
}

Objects.requireNonNull(endpoint);
if (this.pipeline == null) {
Objects.requireNonNull(httpClient);
}

HttpPipeline builderPipeline = this.pipeline;
if (this.pipeline == null) {
builderPipeline = createHttpPipeline(httpClient);
Expand Down
Loading

0 comments on commit 15b62ae

Please sign in to comment.