| | | 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 ProfileApiClient : IProfileApiClient |
| | | 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 | | |
| | 3 | 17 | | public ProfileApiClient( |
| | 3 | 18 | | HttpClient httpClient, |
| | 3 | 19 | | ISupabaseSessionProvider sessionProvider, |
| | 3 | 20 | | SupabaseOptions options) |
| | | 21 | | { |
| | 3 | 22 | | _httpClient = httpClient; |
| | 3 | 23 | | _sessionProvider = sessionProvider; |
| | 3 | 24 | | _publishableKey = options.PublishableKey; |
| | 3 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<AuthBootstrapResponse> BootstrapAsync(CancellationToken cancellationToken = default) |
| | | 28 | | { |
| | 1 | 29 | | using var request = new HttpRequestMessage(HttpMethod.Post, "profile-bootstrap") |
| | 1 | 30 | | { |
| | 1 | 31 | | Content = JsonContent.Create(new { }, options: JsonOptions) |
| | 1 | 32 | | }; |
| | | 33 | | |
| | 1 | 34 | | await AuthorizeAsync(request); |
| | | 35 | | |
| | 1 | 36 | | using var response = await _httpClient.SendAsync(request, cancellationToken); |
| | 1 | 37 | | response.EnsureSuccessStatusCode(); |
| | | 38 | | |
| | 1 | 39 | | var payload = await response.Content.ReadFromJsonAsync<AuthBootstrapResponse>(JsonOptions, cancellationToken); |
| | 1 | 40 | | return payload ?? throw new InvalidOperationException("Profile bootstrap returned no payload."); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | private async Task AuthorizeAsync(HttpRequestMessage request) |
| | | 44 | | { |
| | 1 | 45 | | var accessToken = await _sessionProvider.GetAccessTokenAsync(); |
| | 1 | 46 | | request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); |
| | 1 | 47 | | request.Headers.Add("apikey", _publishableKey); |
| | 1 | 48 | | } |
| | | 49 | | } |