using SharedClasses.SharedModels; using System.Net.Http; using Microsoft.AspNetCore.Mvc; using static System.Net.Mime.MediaTypeNames; using System.Text.Json; using System.Text; namespace DiscordBot.Services { public class HttpService { private readonly IHttpClientFactory _httpClientFactory; private readonly JsonSerializerOptions _serializerOptions; public HttpService(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; _serializerOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; } public async Task DoesUserExist(ulong userId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/DoesUserExist/{userId}"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); } return false; } public async Task> IsSignUpAllowed(int raidId, ulong userId, bool ignoreRole = false) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsSignUpAllowed/{raidId}/{userId}/{ignoreRole}"); if (!httpResponseMessage.IsSuccessStatusCode) { ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync(_serializerOptions) ?? new ProblemDetails(); string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail; return new Tuple(false, errorMessage); } return new Tuple(true, string.Empty); } public async Task> IsExternalSignUpAllowed(int raidId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsExternalSignUpAllowed/{raidId}"); if (!httpResponseMessage.IsSuccessStatusCode) { ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync(_serializerOptions) ?? new ProblemDetails(); string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail; return new Tuple(false, errorMessage); } return new Tuple(true, string.Empty); } public async Task IsUserSignedUp(int raidId, ulong userId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsUserSignedUp/{raidId}/{userId}"); if (httpResponseMessage.IsSuccessStatusCode) { return await httpResponseMessage.Content.ReadAsStringAsync(); } return string.Empty; } public async Task> GetRoles(int raidId, ulong userId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetRoles/{raidId}/{userId}"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync>(contentStream, _serializerOptions); } return new List(); } public async Task SignUp(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/SignUp"); } public async Task SignUpMaybe(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/SignUpMaybe"); } public async Task SignUpBackup(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/SignUpBackup"); } public async Task SignUpFlex(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/SignUpFlex"); } public async Task SignOff(ApiSignUp signUp) { await SendSignUp(signUp, "DiscordBot/SignOff"); } public async Task ChangeToSignUp(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToSignUp"); } public async Task ChangeToMaybe(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToMaybe"); } public async Task ChangeToBackup(ApiSignUp signUp) { return await SendSignUp(signUp, "DiscordBot/ChangeSignUpTypeToBackup"); } private async Task SendSignUp(ApiSignUp signUp, string requestUri) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var raidItemJson = new StringContent( JsonSerializer.Serialize(signUp), Encoding.UTF8, Application.Json); var httpResponseMessage = await httpClient.PostAsync(requestUri, raidItemJson); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); } return false; } public async Task> CreateAccount(ApiRaid.Role.User user) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var raidItemJson = new StringContent( JsonSerializer.Serialize(user), Encoding.UTF8, Application.Json); var httpResponseMessage = await httpClient.PostAsync("DiscordBot/CreateAccount", raidItemJson); if (!httpResponseMessage.IsSuccessStatusCode) { ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync(_serializerOptions) ?? new ProblemDetails(); string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail; return new Tuple(false, errorMessage); } return new Tuple(true, string.Empty); } public async Task GetRaid(int raidId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetRaid/{raidId}"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); } return new ApiRaid(); } public async Task> GetUserRenameServers() { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetUserRenameServers"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync>(contentStream, _serializerOptions); } return new List(); } public async Task GetUser(ulong userId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetUser/{userId}"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); } return new ApiRaid.Role.User(); } public async Task> GetSignUpAccounts(ulong userId, int raidId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/GetSignUpAccounts/{userId}/{raidId}"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync>(contentStream, _serializerOptions); } return new List(); } public async Task> IsSlashCommandAllowed(ulong userId, string command) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/IsSlashCommandAllowed/{userId}/{command}"); if (!httpResponseMessage.IsSuccessStatusCode) { ProblemDetails problemDetails = await httpResponseMessage.Content.ReadFromJsonAsync(_serializerOptions) ?? new ProblemDetails(); string errorMessage = string.IsNullOrEmpty(problemDetails.Detail) ? string.Empty : problemDetails.Detail; return new Tuple(false, errorMessage); } return new Tuple(true, string.Empty); } public async Task ReminderOptOut(ulong userId) { var httpClient = _httpClientFactory.CreateClient(Constants.HTTP_CLIENT_NAME); var httpResponseMessage = await httpClient.GetAsync($"DiscordBot/ReminderOptOut/{userId}"); if (httpResponseMessage.IsSuccessStatusCode) { using var contentStream = await httpResponseMessage.Content.ReadAsStreamAsync(); return await JsonSerializer.DeserializeAsync(contentStream, _serializerOptions); } return false; } } }