Add basic Lodestone linkup

This commit is contained in:
Daniel_I_Am 2020-06-04 20:00:43 +02:00
parent 8e53d012c1
commit 6d25611c4e
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 91 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,21 @@
using System.Collections.Generic;
namespace ChaosBot.Lodestone
{
public class PaginationWrapper<T>
{
public PageinationEntries Pagination;
public List<T> 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;
}
}

View File

@ -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<T>(string endpoint, Dictionary<string, string> parameters)
{
// TODO: implement some sort of local caching
return LodestoneHttpConnection.fetch<T>(endpoint, parameters).GetAwaiter().GetResult();
}
}
static class LodestoneHttpConnection
{
private static readonly Logger _logger = Program._logger;
static HttpClient client = new HttpClient();
public static async Task<T> fetch<T>(string endpoint, Dictionary<string, string> 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<T>(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;
}
}
}
}