Files
Relay/RelayClient/ServerAPI.cs

75 lines
2.5 KiB
C#

using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using RelayShared.Rtc;
namespace RelayClient;
public class ServerAPI
{
private static readonly HttpClient client = new()
{
BaseAddress = new Uri("http://localhost:5000/")
};
public static void setupClient()
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
}
public static async Task PostOfferAsync(RtcOffer offer)
{
var response = await client.PostAsJsonAsync("api/rtc/offer", offer);
response.EnsureSuccessStatusCode();
}
public static async Task<List<string>> GetParticipantsForChannelAsync(string channelId)
{
var response = await client.GetAsync($"api/rtc/participants/{channelId}");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<List<string>>(json) ?? [];
}
public static async Task<RtcSessionDescription?> GetOfferForChannelAsync(string channelId, string fromUsername, string targetUsername)
{
var response = await client.GetAsync($"api/rtc/offer/{channelId}/{fromUsername}/{targetUsername}");
if (!response.IsSuccessStatusCode)
return null;
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<RtcSessionDescription>(json);
}
public static async Task PostAnswerAsync(RtcAnswer answer)
{
var response = await client.PostAsJsonAsync("api/rtc/answer", answer);
response.EnsureSuccessStatusCode();
}
public static async Task<RtcSessionDescription?> GetAnswerForChannelAsync(string channelId, string fromUsername, string targetUsername)
{
var response = await client.GetAsync($"api/rtc/answer/{channelId}/{fromUsername}/{targetUsername}");
if (!response.IsSuccessStatusCode)
return null;
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<RtcSessionDescription>(json);
}
public static async Task PostIceCandidateAsync(DBIceCandidate candidate)
{
var response = await client.PostAsJsonAsync("api/rtc/candidate", candidate);
response.EnsureSuccessStatusCode();
}
public static async Task PostLeaveAsync(RtcLeaveRequest leave)
{
var response = await client.PostAsJsonAsync("api/rtc/leave", leave);
response.EnsureSuccessStatusCode();
}
}