com.azure.communication.rooms.RoomsClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-communication-rooms Show documentation
Show all versions of azure-communication-rooms Show documentation
This package contains clients and data structures used to create, update, get, delete the Azure Communication Room Service.
For this release, see notes
Microsoft Azure Communication Rooms quickstart
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.communication.rooms;
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.rooms.implementation.AzureCommunicationRoomServiceImpl;
import com.azure.communication.rooms.implementation.RoomsImpl;
import com.azure.communication.rooms.implementation.ParticipantsImpl;
import com.azure.communication.rooms.implementation.converters.ParticipantRoleConverter;
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.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.util.Context;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.util.logging.ClientLogger;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;
/**
* Client for Rooms operations of Azure Communication Room Service
*
*
* Instantiating a Room Client
*
*
*
*
*
* RoomsClient roomsClient = new RoomsClientBuilder()
* .endpoint(endpoint)
* .credential(azureKeyCredential)
* .buildClient();
*
*
*
*
* @see RoomsClientBuilder
*
*/
@ServiceClient(builder = RoomsClientBuilder.class)
public final class RoomsClient {
private final RoomsImpl roomsClient;
private final ParticipantsImpl participantsClient;
private final ClientLogger logger = new ClientLogger(RoomsClient.class);
RoomsClient(AzureCommunicationRoomServiceImpl roomsServiceClient) {
roomsClient = roomsServiceClient.getRooms();
participantsClient = roomsServiceClient.getParticipants();
}
/**
* Create a new room. Input field is nullable.
*
* @param createRoomOptions the create room options.
* @return response for a successful create room request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public CommunicationRoom createRoom(CreateRoomOptions createRoomOptions) {
RoomModel roomModel = this.roomsClient
.create(toCreateRoomRequest(createRoomOptions.getValidFrom(),
createRoomOptions.getValidUntil(), createRoomOptions.isPstnDialOutEnabled(),
createRoomOptions.getParticipants()));
return getCommunicationRoomFromResponse(roomModel);
}
/**
* Create a new Room with response.
*
* @param createRoomOptions the create room options.
* @param context The context of key value pairs for http request.
* @return response for a successful create room request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response createRoomWithResponse(CreateRoomOptions createRoomOptions, Context context) {
context = context == null ? Context.NONE : context;
Response response = this.roomsClient
.createWithResponse(toCreateRoomRequest(createRoomOptions.getValidFrom(),
createRoomOptions.getValidUntil(), createRoomOptions.isPstnDialOutEnabled(),
createRoomOptions.getParticipants()), context);
return new SimpleResponse(response, getCommunicationRoomFromResponse(response.getValue()));
}
/**
* Update an existing Room.
*
* @param roomId The room Id.
* @param updateRoomOptions the update room options.
* @return response for a successful update room request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public CommunicationRoom updateRoom(String roomId, UpdateRoomOptions updateRoomOptions) {
RoomModel roomModel = this.roomsClient
.update(roomId,
toUpdateRoomRequest(updateRoomOptions.getValidFrom(), updateRoomOptions.getValidUntil(),
updateRoomOptions.isPstnDialOutEnabled()));
return getCommunicationRoomFromResponse(roomModel);
}
/**
* Update an existing Room with response.
*
* @param roomId The room Id.
* @param updateRoomOptions the update room options.
* @param context The context of key value pairs for http request.
* @return response for a successful update room request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response updateRoomWithResponse(String roomId, UpdateRoomOptions updateRoomOptions, Context context) {
context = context == null ? Context.NONE : context;
Response response = this.roomsClient
.updateWithResponse(roomId,
toUpdateRoomRequest(updateRoomOptions.getValidFrom(), updateRoomOptions.getValidUntil(),
updateRoomOptions.isPstnDialOutEnabled()), context);
return new SimpleResponse(response, getCommunicationRoomFromResponse(response.getValue()));
}
/**
* Get an existing room.
*
* @param roomId The room id.
* @return The existing room.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public CommunicationRoom getRoom(String roomId) {
RoomModel roomModel = this.roomsClient
.get(roomId);
return getCommunicationRoomFromResponse(roomModel);
}
/**
* Get an existing room with response.
*
* @param roomId The room id.
* @param context The context of key value pairs for http request.
* @return The existing room.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response getRoomWithResponse(String roomId, Context context) {
context = context == null ? Context.NONE : context;
Response response = this.roomsClient
.getWithResponse(roomId, context);
return new SimpleResponse(response, getCommunicationRoomFromResponse(response.getValue()));
}
/**
* Delete an existing room.
*
* @param roomId The room Id.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void deleteRoom(String roomId) {
this.roomsClient.delete(roomId);
}
/**
* Delete an existing room.
*
* @param roomId The room Id.
* @param context The context of key value pairs for http request.
* @return Response with status code only.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response deleteRoomWithResponse(String roomId, Context context) {
context = context == null ? Context.NONE : context;
return this.roomsClient.deleteWithResponse(roomId, context);
}
/**
* Lists all rooms.
*
* @return The existing rooms.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public PagedIterable listRooms() {
return new PagedIterable<>(
() -> this.roomsClient.listSinglePage(),
nextLink -> this.roomsClient.listNextSinglePage(nextLink))
.mapPage(f -> RoomModelConverter.convert(f));
}
/**
* Lists all rooms.
*
* @param context The context of key value pairs for http request.
* @return The existing rooms.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public PagedIterable listRooms(Context context) {
final Context serviceContext = context == null ? Context.NONE : context;
return new PagedIterable<>(
() -> this.roomsClient.listSinglePage(serviceContext),
nextLink -> this.roomsClient.listNextSinglePage(nextLink, serviceContext))
.mapPage(f -> RoomModelConverter.convert(f));
}
/**
* addOrUpdate participants to an existing Room.
*
* @param roomId The room id.
* @param participants The participants list.
* @return response for a successful addOrUpdate participants room request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public AddOrUpdateParticipantsResult addOrUpdateParticipants(String roomId, Iterable participants) {
try {
Objects.requireNonNull(participants, "'participants' cannot be null.");
Objects.requireNonNull(roomId, "'roomId' cannot be null.");
Map participantMap = convertRoomParticipantsToMapForAddOrUpdate(participants);
ObjectMapper mapper = new ObjectMapper();
String updateRequest = mapper.writeValueAsString(new UpdateParticipantsRequest().setParticipants(participantMap));
this.participantsClient.update(roomId, updateRequest);
return new AddOrUpdateParticipantsResult();
} catch (JsonProcessingException ex) {
ex.printStackTrace();
throw logger.logExceptionAsError(new IllegalArgumentException("Failed to process JSON input", ex));
}
}
/**
* addOrUpdate participants to an existing Room with response
*
* @param roomId The room id.
* @param participants The participants list.
* @param context The context of key value pairs for http request.
* @return response for a successful addOrUpdate participants room request.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response addOrUpdateParticipantsWithResponse(String roomId, Iterable participants, Context context) {
try {
context = context == null ? Context.NONE : context;
Objects.requireNonNull(participants, "'participants' cannot be null.");
Objects.requireNonNull(roomId, "'roomId' cannot be null.");
Map participantMap = convertRoomParticipantsToMapForAddOrUpdate(participants);
ObjectMapper mapper = new ObjectMapper();
String updateRequest = mapper.writeValueAsString(new UpdateParticipantsRequest().setParticipants(participantMap));
Response