DeukBot4/DeukBot4/APIHandlers/AnimalFactsApi.cs

27 lines
886 B
C#
Raw Permalink Normal View History

2018-05-08 22:41:56 +00:00
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace DeukBot4.APIHandlers
{
2018-11-05 14:21:36 +00:00
public static class AnimalFactsApi
2018-05-08 22:41:56 +00:00
{
2018-11-05 14:21:36 +00:00
private const string URL = "https://api.levi506.net/";
2018-05-08 22:41:56 +00:00
2018-11-05 14:21:36 +00:00
public static async Task<string> GetRandomCatFact(string animal)
2018-05-08 22:41:56 +00:00
{
var client = new HttpClient {BaseAddress = new Uri(URL)};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
2018-11-05 14:21:36 +00:00
var response = await client.GetAsync("/v1/fact/animal/" + animal); // Blocking call!
2018-05-08 22:41:56 +00:00
if (!response.IsSuccessStatusCode)
return null;
var res = await response.Content.ReadAsStringAsync();
var json = JObject.Parse(res);
2018-06-03 18:39:39 +00:00
return json["fact"].Value<string>();
2018-05-08 22:41:56 +00:00
}
}
}