29 lines
901 B
C#
29 lines
901 B
C#
using System;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace DeukBot4.APIHandlers
|
|
{
|
|
public static class CatFactsApi
|
|
{
|
|
private const string URL = "http://api.levi506.net/";
|
|
|
|
public static async Task<string> GetRandomCatFact()
|
|
{
|
|
var client = new HttpClient {BaseAddress = new Uri(URL)};
|
|
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
var response = await client.GetAsync("fact/animal/cat"); // Blocking call!
|
|
if (!response.IsSuccessStatusCode)
|
|
return null;
|
|
|
|
var res = await response.Content.ReadAsStringAsync();
|
|
var json = JObject.Parse(res);
|
|
return json["fact"].Value<string>();
|
|
|
|
}
|
|
}
|
|
} |