using System.Net.Http.Json; using System.Text.Json; using EnvelopeGenerator.Application.Common.Dto.Receiver; namespace EnvelopeGenerator.Server.Client.Services; public class ReceiverService(IHttpClientFactory httpClientFactory) { private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); /// /// Fetches receiver email suggestions from sender-authorized API. /// Calls GET /api/Receiver?EmailAddressSearch={term}&onlyEmailAddress=true. /// public async Task> SearchReceiverEmailsAsync(string searchTerm, CancellationToken cancel = default) { if (string.IsNullOrWhiteSpace(searchTerm)) return []; using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server"); var url = $"/api/Receiver?EmailAddressSearch={Uri.EscapeDataString(searchTerm)}&onlyEmailAddress=true"; var response = await http.GetAsync(url, cancel); if (!response.IsSuccessStatusCode) return []; var emails = await response.Content.ReadFromJsonAsync>(JsonOptions, cancel); return emails ?? []; } /// /// Fetches recent receiver suggestions for the authenticated sender. /// Calls GET /api/EnvelopeReceiver/recent-suggestions?take={take}. /// public async Task> GetRecentReceiverSuggestionsAsync(int take = 20, CancellationToken cancel = default) { using var http = httpClientFactory.CreateClient("EnvelopeGenerator.Server"); var url = $"/api/EnvelopeReceiver/recent-suggestions?take={take}"; var response = await http.GetAsync(url, cancel); if (!response.IsSuccessStatusCode) return []; var suggestions = await response.Content.ReadFromJsonAsync>(JsonOptions, cancel); return suggestions ?? []; } }