using System.Net; using System.Net.Http; using Microsoft.Extensions.Configuration; using NLog; using Torch.API.Managers; namespace LuckPerms.Torch.Discord.Managers; public class LinkSteamDiscordClientManager(IConfiguration configuration) : IManager { private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); public string ApiUrl => _client.BaseAddress!.ToString(); private readonly HttpClient _client = new() { BaseAddress = new(configuration.GetValue("backendUrl") ?? throw new ArgumentNullException(nameof(configuration), "backendUrl is null")), DefaultRequestHeaders = { { "X-API-Key", configuration.GetValue("apiKey") ?? throw new ArgumentNullException(nameof(configuration), "apiKey is null") } } }; public void Attach() { } public void Detach() { _client.Dispose(); } /// /// Checks if a Steam ID is linked. /// /// The Steam ID to check. /// /// A boolean value indicating whether the Steam ID is linked. /// /// Thrown when an HTTP request to the check API fails. public async Task IsSteamIdLinkedAsync(ulong steamId) { using var response = await _client.GetAsync($"steam/check/{steamId}"); if (response.IsSuccessStatusCode) return true; if (response.StatusCode is HttpStatusCode.NotFound) return false; Log.Warn("Failed to check steam id {0}\n{1}", steamId, response); response.EnsureSuccessStatusCode(); return false; // should never happen } /// /// Retrieves the Steam ID associated with the given Discord ID asynchronously. /// /// The Discord ID to lookup the associated Steam ID for. /// The associated Steam ID if found, otherwise null. /// Thrown when an HTTP request to the lookup API fails. public async Task LookupSteamIdAsync(ulong discordId) { using var response = await _client.GetAsync($"lookup/discord/{discordId}"); if (response.IsSuccessStatusCode) return ulong.Parse(await response.Content.ReadAsStringAsync()); if (response.StatusCode is HttpStatusCode.NotFound) return null; Log.Warn("Failed to lookup discord id {0}\n{1}", discordId, response); response.EnsureSuccessStatusCode(); return null; // should never happen } /// /// Retrieves the Discord ID associated with a given Steam ID asynchronously. /// /// The Steam ID to look up. /// The associated Discord ID, or null if no association exists. /// Thrown when an HTTP request to the lookup API fails. public async Task LookupDiscordIdAsync(ulong steamId) { using var response = await _client.GetAsync($"lookup/steam/{steamId}"); if (response.IsSuccessStatusCode) return ulong.Parse(await response.Content.ReadAsStringAsync()); if (response.StatusCode is HttpStatusCode.NotFound) return null; Log.Warn("Failed to lookup steam id {0}\n{1}", steamId, response); response.EnsureSuccessStatusCode(); return null; // should never happen } }