Add basic Lodestone linkup
This commit is contained in:
parent
8e53d012c1
commit
6d25611c4e
14
ChaosBot/Lodestone/Character.cs
Normal file
14
ChaosBot/Lodestone/Character.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
21
ChaosBot/Lodestone/PaginationWrapper.cs
Normal file
21
ChaosBot/Lodestone/PaginationWrapper.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
56
ChaosBot/Services/LodestoneHttpProxy.cs
Normal file
56
ChaosBot/Services/LodestoneHttpProxy.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user