| | | 1 | | using System.Net.Http; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | |
| | | 4 | | namespace Livekit.Server.Sdk.Dotnet |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Client of the LiveKit Agent Dispatch service: dispatches agents to rooms explicitly, lists and deletes |
| | | 8 | | /// those dispatches. |
| | | 9 | | /// </summary> |
| | | 10 | | public class AgentDispatchServiceClient : BaseService |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Creates the client. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="host">URL of the LiveKit server, <c>http(s)://</c> or <c>ws(s)://</c>; a subpath such as <c>htt |
| | | 16 | | /// <param name="apiKey">API key; the <c>LIVEKIT_API_KEY</c> environment variable when omitted.</param> |
| | | 17 | | /// <param name="apiSecret">API secret; the <c>LIVEKIT_API_SECRET</c> environment variable when omitted.</param> |
| | | 18 | | /// <param name="client">Optional <see cref="HttpClient"/> to send the requests with. The SDK never modifies it, |
| | | 19 | | public AgentDispatchServiceClient( |
| | | 20 | | string host, |
| | | 21 | | string apiKey = null, |
| | | 22 | | string apiSecret = null, |
| | | 23 | | HttpClient client = null |
| | | 24 | | ) |
| | 15 | 25 | | : base(host, apiKey, apiSecret, client) { } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Dispatches an agent to a room. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="request">Room, agent name and optional metadata for the agent.</param> |
| | | 31 | | /// <returns>The created dispatch.</returns> |
| | | 32 | | public async Task<AgentDispatch> CreateDispatch(CreateAgentDispatchRequest request) |
| | 2 | 33 | | { |
| | 2 | 34 | | return await SendAsync( |
| | 2 | 35 | | Twirp.Routes.AgentDispatchService.CreateDispatch, |
| | 2 | 36 | | request, |
| | 2 | 37 | | AgentDispatch.Parser, |
| | 2 | 38 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 2 | 39 | | ) |
| | 2 | 40 | | .ConfigureAwait(false); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Deletes an agent dispatch. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="request">Dispatch ID and room.</param> |
| | | 47 | | /// <returns>The deleted dispatch.</returns> |
| | | 48 | | public async Task<AgentDispatch> DeleteDispatch(DeleteAgentDispatchRequest request) |
| | 2 | 49 | | { |
| | 2 | 50 | | return await SendAsync( |
| | 2 | 51 | | Twirp.Routes.AgentDispatchService.DeleteDispatch, |
| | 2 | 52 | | request, |
| | 2 | 53 | | AgentDispatch.Parser, |
| | 2 | 54 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 2 | 55 | | ) |
| | 2 | 56 | | .ConfigureAwait(false); |
| | 2 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Lists the agent dispatches of a room. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="request">Room name, and optionally a dispatch ID to match.</param> |
| | | 63 | | /// <returns>The dispatches of the room.</returns> |
| | | 64 | | public async Task<ListAgentDispatchResponse> ListDispatch(ListAgentDispatchRequest request) |
| | 5 | 65 | | { |
| | 5 | 66 | | return await SendAsync( |
| | 5 | 67 | | Twirp.Routes.AgentDispatchService.ListDispatch, |
| | 5 | 68 | | request, |
| | 5 | 69 | | ListAgentDispatchResponse.Parser, |
| | 5 | 70 | | AuthHeader(new VideoGrants { RoomAdmin = true, Room = request.Room }) |
| | 5 | 71 | | ) |
| | 5 | 72 | | .ConfigureAwait(false); |
| | 5 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets an agent dispatch by its ID, or null when it does not exist. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="dispatchId">ID of the dispatch.</param> |
| | | 79 | | /// <param name="roomName">Room the dispatch belongs to.</param> |
| | | 80 | | /// <returns>The dispatch, or null when the room has no dispatch with that ID.</returns> |
| | | 81 | | public async Task<AgentDispatch> GetDispatch(string dispatchId, string roomName) |
| | 4 | 82 | | { |
| | 4 | 83 | | var response = await ListDispatch( |
| | 4 | 84 | | new ListAgentDispatchRequest { DispatchId = dispatchId, Room = roomName } |
| | 4 | 85 | | ); |
| | 4 | 86 | | return response.AgentDispatches.Count > 0 ? response.AgentDispatches[0] : null; |
| | 4 | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |