Skip to content

Commit

Permalink
Migrate to stream style serialization - Azure communication rooms (#4…
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity authored Aug 24, 2024
1 parent 2e2577d commit dcdba22
Show file tree
Hide file tree
Showing 27 changed files with 1,240 additions and 975 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,14 @@
"new": "class com.azure.resourcemanager.sql.models.UserIdentity",
"justification": "Customer unlikely to subclass this class."
},
{
"ignore": true,
"code": "java.annotation.removed",
"old": "method com.azure.communication.rooms.models.ParticipantRole com.azure.communication.rooms.models.ParticipantRole::fromString(java.lang.String)",
"new": "method com.azure.communication.rooms.models.ParticipantRole com.azure.communication.rooms.models.ParticipantRole::fromString(java.lang.String)",
"annotation": "@com.fasterxml.jackson.annotation.JsonCreator",
"justification": "Migration to azure-json"
},
{
"ignore": true,
"code": "java.method.removed",
Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/azure-communication-rooms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<src.dir>src/main</src.dir>
<test.dir>src/test</test.dir>
<jacoco.min.linecoverage>0.60</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.60</jacoco.min.branchcoverage>
<jacoco.min.branchcoverage>0.54</jacoco.min.branchcoverage>
<jacoco.skip>false</jacoco.skip>
<!-- Configures the Java 9+ run to perform the required module exports,
opens, and reads that are necessary for testing but shouldn't be part of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,47 @@

package com.azure.communication.rooms;

import com.azure.communication.rooms.implementation.AzureCommunicationRoomServiceImpl;

import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.rooms.implementation.RoomsImpl;
import com.azure.communication.rooms.implementation.AzureCommunicationRoomServiceImpl;
import com.azure.communication.rooms.implementation.ParticipantsImpl;
import com.azure.communication.rooms.implementation.converters.ParticipantRoleConverter;
import com.azure.communication.rooms.implementation.RoomsImpl;
import com.azure.communication.rooms.implementation.converters.RoomModelConverter;
import com.azure.communication.rooms.implementation.converters.RoomParticipantConverter;
import com.azure.communication.rooms.implementation.models.RoomModel;
import com.azure.communication.rooms.implementation.models.ParticipantProperties;
import com.azure.communication.rooms.implementation.models.RoomModel;
import com.azure.communication.rooms.models.AddOrUpdateParticipantsResult;
import com.azure.communication.rooms.models.CommunicationRoom;
import com.azure.communication.rooms.models.CreateRoomOptions;
import com.azure.communication.rooms.models.RemoveParticipantsResult;
import com.azure.communication.rooms.models.RoomParticipant;
import com.azure.communication.rooms.models.UpdateRoomOptions;
import com.azure.communication.rooms.models.AddOrUpdateParticipantsResult;
import com.azure.communication.rooms.implementation.models.UpdateParticipantsRequest;
import com.azure.communication.rooms.implementation.models.UpdateRoomRequest;
import com.azure.communication.rooms.implementation.models.CreateRoomRequest;
import com.azure.core.annotation.ReturnType;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.paging.PageRetriever;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.OffsetDateTime;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import static com.azure.communication.rooms.implementation.Transforms.convertRoomIdentifiersToMapForRemove;
import static com.azure.communication.rooms.implementation.Transforms.convertRoomParticipantsToMapForAddOrUpdate;
import static com.azure.communication.rooms.implementation.Transforms.getCommunicationRoomFromResponse;
import static com.azure.communication.rooms.implementation.Transforms.getUpdateRequest;
import static com.azure.communication.rooms.implementation.Transforms.toCreateRoomRequest;
import static com.azure.communication.rooms.implementation.Transforms.toUpdateRoomRequest;
import static com.azure.core.util.FluxUtil.monoError;
import static com.azure.core.util.FluxUtil.pagedFluxError;

Expand Down Expand Up @@ -339,19 +336,14 @@ Mono<AddOrUpdateParticipantsResult> addOrUpdateParticipants(String roomId, Itera

Map<String, ParticipantProperties> participantMap = convertRoomParticipantsToMapForAddOrUpdate(participants);

ObjectMapper mapper = new ObjectMapper();

String updateRequest = mapper.writeValueAsString(new UpdateParticipantsRequest().setParticipants(participantMap));


String updateRequest = getUpdateRequest(participantMap);
return this.participantsClient
.updateAsync(roomId, updateRequest, context)
.flatMap((response) -> {
return Mono.just(new AddOrUpdateParticipantsResult());
});
.updateAsync(roomId, updateRequest, context)
.flatMap((response) -> {
return Mono.just(new AddOrUpdateParticipantsResult());
});

} catch (JsonProcessingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
return Mono.error(new IllegalArgumentException("Failed to process JSON input", ex));
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand Down Expand Up @@ -380,18 +372,14 @@ Mono<Response<AddOrUpdateParticipantsResult>> addOrUpdateParticipantsWithRespons

Map<String, ParticipantProperties> participantMap = convertRoomParticipantsToMapForAddOrUpdate(participants);

ObjectMapper mapper = new ObjectMapper();

String updateRequest = mapper.writeValueAsString(new UpdateParticipantsRequest().setParticipants(participantMap));

String updateRequest = getUpdateRequest(participantMap);


return this.participantsClient
.updateWithResponseAsync(roomId, updateRequest, context)
.map(result -> new SimpleResponse<AddOrUpdateParticipantsResult>(
result.getRequest(), result.getStatusCode(), result.getHeaders(), null));
} catch (JsonProcessingException ex) {
ex.printStackTrace();
.updateWithResponseAsync(roomId, updateRequest, context)
.map(result -> new SimpleResponse<AddOrUpdateParticipantsResult>(
result.getRequest(), result.getStatusCode(), result.getHeaders(), null));
} catch (IOException ex) {
return Mono.error(new IllegalArgumentException("Failed to process JSON input", ex));
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand Down Expand Up @@ -421,17 +409,13 @@ Mono<RemoveParticipantsResult> removeParticipants(String roomId,
Map<String, ParticipantProperties> participantMap = convertRoomIdentifiersToMapForRemove(
participantsIdentifiers);

ObjectMapper mapper = new ObjectMapper();

String updateRequest = mapper.writeValueAsString(new UpdateParticipantsRequest().setParticipants(participantMap));

String updateRequest = getUpdateRequest(participantMap);
return this.participantsClient
.updateAsync(roomId, updateRequest, context)
.flatMap((response) -> {
return Mono.just(new RemoveParticipantsResult());
});
} catch (JsonProcessingException ex) {
ex.printStackTrace();
.updateAsync(roomId, updateRequest, context)
.flatMap((response) -> {
return Mono.just(new RemoveParticipantsResult());
});
} catch (IOException ex) {
return Mono.error(new IllegalArgumentException("Failed to process JSON input", ex));
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand Down Expand Up @@ -461,16 +445,12 @@ Mono<Response<RemoveParticipantsResult>> removeParticipantsWithResponse(String r
Map<String, ParticipantProperties> participantMap = convertRoomIdentifiersToMapForRemove(
participantsIdentifiers);

ObjectMapper mapper = new ObjectMapper();

String updateRequest = mapper.writeValueAsString(new UpdateParticipantsRequest().setParticipants(participantMap));

String updateRequest = getUpdateRequest(participantMap);
return this.participantsClient
.updateWithResponseAsync(roomId, updateRequest, context)
.map(result -> new SimpleResponse<RemoveParticipantsResult>(
result.getRequest(), result.getStatusCode(), result.getHeaders(), null));
} catch (JsonProcessingException ex) {
ex.printStackTrace();
.updateWithResponseAsync(roomId, updateRequest, context)
.map(result -> new SimpleResponse<RemoveParticipantsResult>(
result.getRequest(), result.getStatusCode(), result.getHeaders(), null));
} catch (IOException ex) {
return Mono.error(new IllegalArgumentException("Failed to process JSON input", ex));
} catch (RuntimeException ex) {
return monoError(logger, ex);
Expand Down Expand Up @@ -525,105 +505,4 @@ private <T1, T2> PagedFlux<T1> pagedFluxConvert(PagedFlux<T2> originalPagedFlux,

return PagedFlux.create(provider);
}

private CommunicationRoom getCommunicationRoomFromResponse(RoomModel room) {
return new CommunicationRoom(
room.getId(),
room.getValidFrom(),
room.getValidUntil(),
room.getCreatedAt(),
room.isPstnDialOutEnabled());
}

/**
* Translate to create room request.
*
* @return The create room request.
*/
private CreateRoomRequest toCreateRoomRequest(OffsetDateTime validFrom, OffsetDateTime validUntil,
Boolean isPstnDialOutEnabled, Iterable<RoomParticipant> participants) {
CreateRoomRequest createRoomRequest = new CreateRoomRequest();
if (validFrom != null) {
createRoomRequest.setValidFrom(validFrom);
}

if (validUntil != null) {
createRoomRequest.setValidUntil(validUntil);
}

createRoomRequest.setPstnDialOutEnabled(isPstnDialOutEnabled);

Map<String, ParticipantProperties> roomParticipants = new HashMap<>();

if (participants != null) {
roomParticipants = convertRoomParticipantsToMapForAddOrUpdate(participants);
}

if (participants != null) {
createRoomRequest.setParticipants(roomParticipants);
}

return createRoomRequest;
}

/**
* Translate to update room request.
*
* @return The update room request.
*/
private UpdateRoomRequest toUpdateRoomRequest(OffsetDateTime validFrom, OffsetDateTime validUntil, Boolean isPstnDialOutEnabled) {
UpdateRoomRequest updateRoomRequest = new UpdateRoomRequest();

if (validFrom != null) {
updateRoomRequest.setValidFrom(validFrom);
}

if (validUntil != null) {
updateRoomRequest.setValidUntil(validUntil);
}

if (isPstnDialOutEnabled != null) {
updateRoomRequest.setPstnDialOutEnabled(isPstnDialOutEnabled);
}

return updateRoomRequest;
}

/**
* Translate to map for add or update participants.
*
* @return Map of participants.
*/
private Map<String, ParticipantProperties> convertRoomParticipantsToMapForAddOrUpdate(
Iterable<RoomParticipant> participants) {
Map<String, ParticipantProperties> participantMap = new HashMap<>();

if (participants != null) {
for (RoomParticipant participant : participants) {
participantMap.put(participant.getCommunicationIdentifier().getRawId(),
new ParticipantProperties().setRole(ParticipantRoleConverter.convert(participant.getRole())));
}
}

return participantMap;
}

/**
* Translate to map for remove participants.
*
* @return Map of participants.
*/
private Map<String, ParticipantProperties> convertRoomIdentifiersToMapForRemove(
Iterable<CommunicationIdentifier> identifiers) {
Map<String, ParticipantProperties> participantMap = new HashMap<>();

if (identifiers != null) {
for (CommunicationIdentifier identifier : identifiers) {
participantMap.put(identifier.getRawId(), null);
}
}

return participantMap;
}

}
Loading

0 comments on commit dcdba22

Please sign in to comment.