From aa85aeab324ad4e90d3f2f0e87426ebc06447ea6 Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Sun, 2 Mar 2025 15:54:49 -0800 Subject: [PATCH] add exception handling to ``ApiWebService`` improve ``BanCommand`` API response handling --- src/Core/ApiWebService.cs | 28 +++++++++++++++++++++------ src/Management/Commands/BanCommand.cs | 4 +--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Core/ApiWebService.cs b/src/Core/ApiWebService.cs index 5612a38..602ce9b 100644 --- a/src/Core/ApiWebService.cs +++ b/src/Core/ApiWebService.cs @@ -1,5 +1,6 @@ using System; using System.Net.Http.Json; +using sodoffmmo.CommandHandlers; namespace sodoffmmo.Core; @@ -15,12 +16,16 @@ public class ApiWebService ); httpClient.Timeout = new TimeSpan(0, 0, 3); - var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/CheckForVikingBan", content).Result; - if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadFromJsonAsync().Result; - else return null; + try + { + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/CheckForVikingBan", content).Result; + Log("Moderation/CheckForVikingBan"); + if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadFromJsonAsync().Result; + else return null; + } catch (Exception e) { LogError(e.Message); return null; } } - public HttpResponseMessage BanUser(Client client, string userId, string banType, string days) + public string? BanUser(Client client, string userId, string banType, string days) { HttpClient httpClient = new(); var content = new FormUrlEncodedContent( @@ -32,7 +37,18 @@ public class ApiWebService } ); httpClient.Timeout = new TimeSpan(0, 0, 3); - var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/AddBanToVikingByGuid", content).Result; - return response; + + try + { + var response = httpClient.PostAsync($"{Configuration.ServerConfiguration.ApiUrl}/Moderation/AddBanToVikingByGuid", content).Result; + Log("Moderation/AddBanToVikingByGuid"); + + if (response.StatusCode == System.Net.HttpStatusCode.OK && response.Content != null) return response.Content.ReadAsStringAsync().Result; + else return null; + } catch (Exception e) { LogError(e.Message); return null; } } + + private void Log(string endpoint) => Console.WriteLine($"Sent API Request To {Configuration.ServerConfiguration.ApiUrl}/{endpoint}"); + + private void LogError(string message) => Console.WriteLine($"An Error Has Occured When Sending An API Request - {message}"); } diff --git a/src/Management/Commands/BanCommand.cs b/src/Management/Commands/BanCommand.cs index 86802c7..6f8e57a 100644 --- a/src/Management/Commands/BanCommand.cs +++ b/src/Management/Commands/BanCommand.cs @@ -19,10 +19,8 @@ class BanCommand : IManagementCommand // send an http request to the api set in appsettings ApiWebService apiWebService = new(); var response = apiWebService.BanUser(client, clientToBan, arguments[1], arguments[2]); - var responseString = response.Content.ReadAsStringAsync().Result; - if (response.StatusCode != System.Net.HttpStatusCode.OK && responseString != null) { client.Send(Utils.BuildServerSideMessage(responseString, "Server")); return; } - else if (responseString != null) { client.Send(Utils.BuildServerSideMessage("User Banned Successfully", "Server")); return; } + if (response != null) { client.Send(Utils.BuildServerSideMessage("User Banned Successfully", "Server")); return; } else { client.Send(Utils.BuildServerSideMessage("Empty Response", "Server")); } } }