From 86d3c4cdc8045d13fc25ee66bb75f35aaf023647 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Sun, 11 May 2025 01:17:55 +0500 Subject: [PATCH] use enum for compat api status codes --- Clients/CompatApiClient/ApiConfig.cs | 14 +++++++------- Clients/CompatApiClient/CompatApiStatus.cs | 11 +++++++++++ Clients/CompatApiClient/POCOs/CompatResult.cs | 2 +- CompatBot/Commands/CompatList.cs | 2 +- .../EventHandlers/IsTheGamePlayableHandler.cs | 2 +- CompatBot/EventHandlers/ProductCodeLookup.cs | 4 ++-- 6 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 Clients/CompatApiClient/CompatApiStatus.cs diff --git a/Clients/CompatApiClient/ApiConfig.cs b/Clients/CompatApiClient/ApiConfig.cs index ccb47d04..b1d68bfb 100644 --- a/Clients/CompatApiClient/ApiConfig.cs +++ b/Clients/CompatApiClient/ApiConfig.cs @@ -7,7 +7,7 @@ using NLog; namespace CompatApiClient; -using ReturnCodeType = Dictionary; +using ReturnCodeType = Dictionary; public static class ApiConfig { @@ -22,12 +22,12 @@ public static class ApiConfig public static readonly ReturnCodeType ReturnCodes = new() { - [0] = (true, false, true, "Results successfully retrieved."), - [1] = (false, false, true, "No results."), - [2] = (true, false, true, "No match was found, displaying results for: ***{0}***."), - [-1] = (false, true, false, "{0}: Internal error occurred, please contact Ani and Nicba1010"), - [-2] = (false, true, false, "{0}: API is undergoing maintenance, please try again later."), - [-3] = (false, false, false, "Illegal characters found, please try again with a different search term."), + [CompatApiStatus.Success] = (true, false, true, "Results successfully retrieved."), + [CompatApiStatus.NoResults] = (false, false, true, "No results."), + [CompatApiStatus.NoExactMatch] = (true, false, true, "No match was found, displaying results for: ***{0}***."), + [CompatApiStatus.InternalError] = (false, true, false, "{0}: Internal error occurred, please contact Ani and Nicba1010"), + [CompatApiStatus.Maintenance] = (false, true, false, "{0}: API is undergoing maintenance, please try again later."), + [CompatApiStatus.IllegalQuery] = (false, false, false, "Illegal characters found, please try again with a different search term."), }; public static readonly List ResultAmount = [25, 50, 100, 200]; diff --git a/Clients/CompatApiClient/CompatApiStatus.cs b/Clients/CompatApiClient/CompatApiStatus.cs new file mode 100644 index 00000000..5a1e9517 --- /dev/null +++ b/Clients/CompatApiClient/CompatApiStatus.cs @@ -0,0 +1,11 @@ +namespace CompatApiClient; + +public enum CompatApiStatus: short +{ + IllegalQuery = -3, + Maintenance = -2, + InternalError = -1, + Success = 0, + NoResults = 1, + NoExactMatch = 2, +} \ No newline at end of file diff --git a/Clients/CompatApiClient/POCOs/CompatResult.cs b/Clients/CompatApiClient/POCOs/CompatResult.cs index 54a890b4..d4f65977 100644 --- a/Clients/CompatApiClient/POCOs/CompatResult.cs +++ b/Clients/CompatApiClient/POCOs/CompatResult.cs @@ -7,7 +7,7 @@ namespace CompatApiClient.POCOs; public class CompatResult { - public int ReturnCode; + public CompatApiStatus ReturnCode; public string SearchTerm; public Dictionary Results; diff --git a/CompatBot/Commands/CompatList.cs b/CompatBot/Commands/CompatList.cs index 8a9d23a5..0c12768a 100644 --- a/CompatBot/Commands/CompatList.cs +++ b/CompatBot/Commands/CompatList.cs @@ -146,7 +146,7 @@ internal static partial class CompatList var result = new CompatResult { RequestBuilder = requestBuilder, - ReturnCode = 0, + ReturnCode = CompatApiStatus.Success, SearchTerm = requestBuilder.Search, Results = matches.ToDictionary(i => i.thumb.ProductCode, i => new TitleInfo { diff --git a/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs b/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs index 44939a65..f4c0aa4b 100644 --- a/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs +++ b/CompatBot/EventHandlers/IsTheGamePlayableHandler.cs @@ -106,7 +106,7 @@ internal static partial class IsTheGamePlayableHandler var status = await searchCompatListTask.ConfigureAwait(false); status = status?.Append(localList); if (status is null - || status.ReturnCode != 0 && status.ReturnCode != 2 + || status.ReturnCode is not CompatApiStatus.Success and not CompatApiStatus.NoExactMatch || status.Results.Count is 0) return (null, null); diff --git a/CompatBot/EventHandlers/ProductCodeLookup.cs b/CompatBot/EventHandlers/ProductCodeLookup.cs index 33f089a3..7cbcfdb9 100644 --- a/CompatBot/EventHandlers/ProductCodeLookup.cs +++ b/CompatBot/EventHandlers/ProductCodeLookup.cs @@ -127,13 +127,13 @@ internal static partial class ProductCodeLookup var localList = await CompatList.GetLocalCompatResultAsync(requestBuilder).ConfigureAwait(false); var status = await searchCompatListTask.ConfigureAwait(false); result = status?.Append(localList); - if (result?.ReturnCode == -2) + if (result?.ReturnCode is CompatApiStatus.Maintenance) return ( await TitleInfo.Maintenance.AsEmbedAsync(code).ConfigureAwait(false), result ); - if (result?.ReturnCode == -1) + if (result?.ReturnCode is CompatApiStatus.InternalError) return ( await TitleInfo.CommunicationError.AsEmbedAsync(code).ConfigureAwait(false), result