diff --git a/RawgNET/RawgAccessManager.cs b/RawgNET/RawgAccessManager.cs index ec5168d..784cc0c 100644 --- a/RawgNET/RawgAccessManager.cs +++ b/RawgNET/RawgAccessManager.cs @@ -16,22 +16,22 @@ internal class RawgAccessManager internal static Game RawgRequest(string name, string rawgkey, bool getAchievements) { Game? GameReturnValue = null; - Task QueryTryGame = Task.Run(() => QueryGame(name, rawgkey)); - QueryTryGame.Wait(); - if (object.Equals(QueryTryGame.Result.BackgroundImage, null)) + Task GameQueryResult = Task.Run(() => QueryGame(name, rawgkey)); + GameQueryResult.Wait(); + if (object.Equals(GameQueryResult.Result.BackgroundImage, null)) { - Task FallbackQueryTry = Task.Run(() => QueryFallback(name, rawgkey)); - FallbackQueryTry.Wait(); - if (FallbackQueryTry.Result.Redirect) + Task GameFallbackquery = Task.Run(() => QueryFallback(name, rawgkey)); + GameFallbackquery.Wait(); + if (GameFallbackquery.Result.Redirect) { - Task SecondQueryTryGame = Task.Run(() => QueryGame(FallbackQueryTry.Result.Slug, rawgkey)); - QueryTryGame.Wait(); - QueryTryGame = SecondQueryTryGame; + Task GameRequeryResult = Task.Run(() => QueryGame(GameFallbackquery.Result.Slug, rawgkey)); + GameQueryResult.Wait(); + GameQueryResult = GameRequeryResult; } } - if (!object.Equals(QueryTryGame.Result.BackgroundImage, null)) + if (!object.Equals(GameQueryResult.Result.BackgroundImage, null)) { - GameReturnValue = QueryTryGame.Result; + GameReturnValue = GameQueryResult.Result; if (object.Equals(GameReturnValue.Metacritic, null)) { GameReturnValue.Metacritic = 0; @@ -43,7 +43,7 @@ internal static Game RawgRequest(string name, string rawgkey, bool getAchievemen if (getAchievements) { Task gameAchievementQuery = Task.Run(() => QueryAchievements(GameReturnValue.Slug, rawgkey)); - QueryTryGame.Wait(); + GameQueryResult.Wait(); if (gameAchievementQuery.Result.Count > 0) { @@ -61,6 +61,41 @@ internal static Game RawgRequest(string name, string rawgkey, bool getAchievemen return GameReturnValue; } + /// + /// A method, to see if a game exists in the first place + /// + /// Name of the game we'd like to query + /// Your API-Key + /// + internal static bool RawgRequestGameExists(string name, string rawgkey) + { + Task GameQueryResult = Task.Run(() => QueryGame(name, rawgkey)); + GameQueryResult.Wait(); + + if (object.Equals(GameQueryResult.Result.BackgroundImage, null)) + { + Task GameFallbackquery = Task.Run(() => QueryFallback(name, rawgkey)); + GameFallbackquery.Wait(); + + if (GameFallbackquery.Result.Redirect) + { + Task GameRequeryResult = Task.Run(() => QueryGame(GameFallbackquery.Result.Slug, rawgkey)); + GameQueryResult.Wait(); + GameQueryResult = GameRequeryResult; + + if (object.Equals(GameQueryResult.Result.BackgroundImage, null)) + { + return false; + } + } + else + { + return false; + } + } + return true; + } + /// /// Recursive method, to get every achievement of a game /// diff --git a/RawgNET/RawgClient.cs b/RawgNET/RawgClient.cs index f4bd9e2..0377c76 100644 --- a/RawgNET/RawgClient.cs +++ b/RawgNET/RawgClient.cs @@ -15,32 +15,46 @@ public void Dispose() } /// - /// The publicy available method that is called + /// Method to query if a game exists, where key and name are checked first /// /// Name of the game we'd like to query - /// If we want to query for the achievements (takes a second longer) - /// - public Game GetGameData(string gamename, bool getAchievements) + /// A boolean, if the game exists or not + public bool IsGameExisting(string gamename) { - Game game; - if (!object.Equals(options.APIKEY, null) && options.APIKEY.Length > 10) + if (object.Equals(options.APIKEY, null) || options.APIKEY.Length < 30 || options.APIKEY.Contains(' ')) { - if (!object.Equals(gamename, null) && gamename.Length > 1) - { - game = RawgAccessManager.RawgRequest(gamename, options.APIKEY, getAchievements); - } - else + if (object.Equals(gamename, null) || gamename.Length < 1) { - NullReferenceException nullReferenceException = new("The name of the game is empty or in the wrong format!"); - throw nullReferenceException; + NullReferenceException ErrorGame = new("The name of the game is empty or in the wrong format!"); + throw ErrorGame; } + + NullReferenceException ErrorKey = new("ApiKey is empty or in the wrong format!"); + throw ErrorKey; } - else + return RawgAccessManager.RawgRequestGameExists(gamename, options.APIKEY); + } + + /// + /// Method to query a game, where key and name are checked first + /// + /// Name of the game we'd like to query + /// If we want to query for the achievements (takes a second longer) + /// Returns a game object, which contains the data + public Game GetGameData(string gamename, bool getAchievements) + { + if (object.Equals(options.APIKEY, null) || options.APIKEY.Length < 30 || options.APIKEY.Contains(' ')) { - NullReferenceException nullReferenceException = new("ApiKey is empty or in the wrong format!"); - throw nullReferenceException; + if (object.Equals(gamename, null) || gamename.Length < 1) + { + NullReferenceException ErrorGame = new("The name of the game is empty or in the wrong format!"); + throw ErrorGame; + } + + NullReferenceException ErrorKey = new("ApiKey is empty or in the wrong format!"); + throw ErrorKey; } - return game; + return RawgAccessManager.RawgRequest(gamename, options.APIKEY, getAchievements); } } } \ No newline at end of file diff --git a/RawgNetDemo/Program.cs b/RawgNetDemo/Program.cs index 2b12956..bd666cb 100644 --- a/RawgNetDemo/Program.cs +++ b/RawgNetDemo/Program.cs @@ -11,15 +11,22 @@ private static void Main(string[] args) string query = "gtav"; Console.WriteLine($"Querying for: {query}"); - Game game = client.GetGameData(query, true); - if (!object.Equals(game, null)) + if (client.IsGameExisting(query)) { - Console.WriteLine($"Output for: {game.Name} | {game.NameOriginal}\n"); + Game game = client.GetGameData(query, true); + if (!object.Equals(game, null)) + { + Console.WriteLine($"Output for: {game.Name} | {game.NameOriginal}\n"); + } + Console.WriteLine($"Achievements {Environment.NewLine}--------------"); + foreach (Result item in game.Achievements) + { + Console.WriteLine($"------ {Environment.NewLine} Name: {item.Name} {Environment.NewLine} Description: {item.Description} {Environment.NewLine} Image: {item.Image} {Environment.NewLine}"); + } } - Console.WriteLine($"Achievements {Environment.NewLine}--------------"); - foreach (Result item in game.Achievements) + else { - Console.WriteLine($"------ {Environment.NewLine} Name: {item.Name} {Environment.NewLine} Description: {item.Description} {Environment.NewLine} Image: {item.Image} {Environment.NewLine}"); + Console.WriteLine("Game does not exist"); } } }