| | | 1 | | using System.Net.Http.Headers; |
| | | 2 | | using System.Net.Http.Json; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using RaidLoop.Client.Configuration; |
| | | 5 | | using RaidLoop.Core.Contracts; |
| | | 6 | | |
| | | 7 | | namespace RaidLoop.Client.Services; |
| | | 8 | | |
| | | 9 | | public sealed class GameActionApiClient : IGameActionApiClient |
| | | 10 | | { |
| | 1 | 11 | | private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); |
| | | 12 | | |
| | | 13 | | private readonly HttpClient _httpClient; |
| | | 14 | | private readonly ISupabaseSessionProvider _sessionProvider; |
| | | 15 | | private readonly string _publishableKey; |
| | | 16 | | |
| | 2 | 17 | | public GameActionApiClient( |
| | 2 | 18 | | HttpClient httpClient, |
| | 2 | 19 | | ISupabaseSessionProvider sessionProvider, |
| | 2 | 20 | | SupabaseOptions options) |
| | | 21 | | { |
| | 2 | 22 | | _httpClient = httpClient; |
| | 2 | 23 | | _sessionProvider = sessionProvider; |
| | 2 | 24 | | _publishableKey = options.PublishableKey; |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<GameActionResult> SendAsync(string action, object payload, CancellationToken cancellationToken = d |
| | | 28 | | { |
| | 2 | 29 | | using var request = new HttpRequestMessage(HttpMethod.Post, "game-action") |
| | 2 | 30 | | { |
| | 2 | 31 | | Content = JsonContent.Create(new GameActionRequest(action, JsonSerializer.SerializeToElement(payload)), opti |
| | 2 | 32 | | }; |
| | | 33 | | |
| | 2 | 34 | | await AuthorizeAsync(request); |
| | | 35 | | |
| | 2 | 36 | | using var response = await _httpClient.SendAsync(request, cancellationToken); |
| | 2 | 37 | | response.EnsureSuccessStatusCode(); |
| | | 38 | | |
| | 2 | 39 | | var json = await response.Content.ReadAsStringAsync(cancellationToken); |
| | 2 | 40 | | if (string.IsNullOrWhiteSpace(json)) |
| | | 41 | | { |
| | 0 | 42 | | throw new InvalidOperationException("Game action returned no payload."); |
| | | 43 | | } |
| | | 44 | | |
| | 2 | 45 | | using var document = JsonDocument.Parse(json); |
| | 2 | 46 | | var root = document.RootElement; |
| | 2 | 47 | | if (root.ValueKind != JsonValueKind.Object |
| | 2 | 48 | | || (!root.TryGetProperty("eventType", out _) |
| | 2 | 49 | | && !root.TryGetProperty("event", out _) |
| | 2 | 50 | | && !root.TryGetProperty("projections", out _))) |
| | | 51 | | { |
| | 1 | 52 | | throw new InvalidOperationException("Game action returned legacy snapshot payload."); |
| | | 53 | | } |
| | | 54 | | |
| | 1 | 55 | | var result = JsonSerializer.Deserialize<GameActionResult>(json, JsonOptions); |
| | 1 | 56 | | return result ?? throw new InvalidOperationException("Game action returned no payload."); |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | private async Task AuthorizeAsync(HttpRequestMessage request) |
| | | 60 | | { |
| | 2 | 61 | | var accessToken = await _sessionProvider.GetAccessTokenAsync(); |
| | 2 | 62 | | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); |
| | 2 | 63 | | request.Headers.Add("apikey", _publishableKey); |
| | 2 | 64 | | } |
| | | 65 | | } |