diff --git a/ChaosBot/Lodestone/Character.cs b/ChaosBot/Lodestone/Character.cs new file mode 100644 index 0000000..8b542ca --- /dev/null +++ b/ChaosBot/Lodestone/Character.cs @@ -0,0 +1,14 @@ +namespace ChaosBot.Lodestone +{ + public class Character + { + public string Avatar; + public int FeastMatches; + public long ID; + public string Lang; + public string Name; + // public ? Rank; + // public ? RankIcon; + public string Server; + } +} \ No newline at end of file diff --git a/ChaosBot/Lodestone/PaginationWrapper.cs b/ChaosBot/Lodestone/PaginationWrapper.cs new file mode 100644 index 0000000..b8f3a81 --- /dev/null +++ b/ChaosBot/Lodestone/PaginationWrapper.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; + +namespace ChaosBot.Lodestone +{ + public class PaginationWrapper + { + public PageinationEntries Pagination; + public List Results; + } + + public class PageinationEntries + { + public int Page; + public int? PageNext; + public int? PagePrev; + public int PageTotal; + public int Results; + public int ResultsPerPage; + public int ResultsTotal; + } +} \ No newline at end of file diff --git a/ChaosBot/Services/LodestoneHttpProxy.cs b/ChaosBot/Services/LodestoneHttpProxy.cs new file mode 100644 index 0000000..21c10a3 --- /dev/null +++ b/ChaosBot/Services/LodestoneHttpProxy.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using ChaosBot.Lodestone; +using Newtonsoft.Json; +using NLog; +using NLog.Fluent; + +namespace ChaosBot.Services +{ + public static class LodestoneHttpProxy + { + public static T fetch(string endpoint, Dictionary parameters) + { + // TODO: implement some sort of local caching + return LodestoneHttpConnection.fetch(endpoint, parameters).GetAwaiter().GetResult(); + } + } + + static class LodestoneHttpConnection + { + private static readonly Logger _logger = Program._logger; + static HttpClient client = new HttpClient(); + + public static async Task fetch(string endpoint, Dictionary parameters) + { + try + { + client.BaseAddress = new Uri("https://xivapi.com/"); + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add( + new MediaTypeWithQualityHeaderValue("application/json")); + + string queryString = String.Join("&",parameters.Select(param => $"{param.Key}={param.Value}").ToArray()); + HttpResponseMessage response = await client.GetAsync($"{endpoint}?{queryString}"); + string output = null; + if (response.IsSuccessStatusCode) + { + output = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(output); + } + + throw new HttpRequestException($"HTTP Response for '{response.RequestMessage.RequestUri}' returned status code {response.StatusCode}"); + } + catch (Exception ex) + { + _logger.Error(ex, $"HttpProxy.fetch<{typeof(T)}>: Exception [{ex}] thrown, <[{ex.Message}]>."); + throw; + } + } + } +} \ No newline at end of file