using System; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; using UWPHook.Properties; using System.Diagnostics; namespace UWPHook.SteamGridDb { class SteamGridDbApi { private const string BASE_URL = "https://www.steamgriddb.com/api/v2/"; private HttpClient httpClient; private Settings settings; /// /// Constructor /// /// An SteamGridDB api key retrieved from https://www.steamgriddb.com/profile/preferences public SteamGridDbApi(string apiKey) { settings = Settings.Default; httpClient = new HttpClient(); httpClient.BaseAddress = new Uri(BASE_URL); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); } /// /// Search SteamGridDB for a game /// /// Name of the game /// Array of games corresponding to the provided name public async Task SearchGame(string gameName) { string path = $"search/autocomplete/{gameName}"; GameResponse[] games = null; HttpResponseMessage response = await httpClient.GetAsync(path); if (response.IsSuccessStatusCode) { var parsedResponse = await response.Content.ReadAsAsync>(); games = parsedResponse.Data; } else if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { Debug.WriteLine("ERROR RESPONSE: " + response.ToString()); settings.SteamGridDbApiKey = String.Empty; settings.Save(); throw new TaskCanceledException("Warning: SteamGrid API Key Invalid. Please generate a new key and add it to settings."); } return games; } /// /// Method responsible for transforming user selected settings /// into a suitable parameter list for SteamGridDB requests /// /// Comma separated list of resolutions, see https://www.steamgriddb.com/api/v2#tag/GRIDS /// A String with the formatted parameters public string BuildParameters(string dimensions) { String result = String.Empty; var style = settings.SteamGridDB_Style[settings.SelectedSteamGridDB_Style]; var type = settings.SteamGridDB_Type[settings.SelectedSteamGridDB_Type]; var nsfw = settings.SteamGridDB_nfsw[settings.SelectedSteamGridDB_nfsw]; var humor = settings.SteamGridDB_Humor[settings.SelectedSteamGridDB_Humor]; if (!String.IsNullOrEmpty(dimensions)) result += $"dimensions={dimensions}&"; if (type != "any") result += $"types={type}&"; if (style != "any") result += $"styles={style}&"; if (nsfw != "any") result += $"nsfw={nsfw}&"; if (humor != "any") result += $"humor={humor}&"; return result; } /// /// Performs a request on a given url /// /// The url to perform the request /// An array of ImageResponse with their urls public async Task getResponse(string url) { HttpResponseMessage response = await httpClient.GetAsync(url); ImageResponse[] images = null; if (response.IsSuccessStatusCode) { var parsedResponse = await response.Content.ReadAsAsync>(); images = parsedResponse.Data; } return images; } public async Task GetGameGrids(int gameId, string dimensions = null) { string path = $"grids/game/{gameId}?{BuildParameters(dimensions)}"; return await getResponse(path); } public async Task GetGameHeroes(int gameId, string dimensions = null) { string path = $"heroes/game/{gameId}?{BuildParameters(dimensions)}"; return await getResponse(path); } public async Task GetGameLogos(int gameId, string dimensions = null) { string path = $"logos/game/{gameId}?{BuildParameters(dimensions)}"; return await getResponse(path); } private class ResponseWrapper { public bool Success { get; set; } public T[] Data { get; set; } } } }