Add an abstraction for http calls

This commit is contained in:
runebaas 2020-07-15 02:52:13 +02:00
parent 0589b8e91b
commit ba0d116f3e
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
9 changed files with 75 additions and 135 deletions

View file

@ -1,10 +1,9 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Cat
{
@ -23,28 +22,12 @@ namespace Geekbot.net.Commands.Randomness.Cat
{
try
{
try
var response = await HttpAbstractions.Get<CatResponseDto>(new Uri("https://aws.random.cat/meow"));
var eb = new EmbedBuilder
{
using var client = new HttpClient
{
BaseAddress = new Uri("https://aws.random.cat")
};
var response = await client.GetAsync("/meow");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var catFile = JsonConvert.DeserializeObject<CatResponseDto>(stringResponse);
var eb = new EmbedBuilder
{
ImageUrl = catFile.File
};
await ReplyAsync("", false, eb.Build());
}
catch
{
await ReplyAsync("Seems like the dog cought the cat (error occured)");
}
ImageUrl = response.File
};
await ReplyAsync("", false, eb.Build());
}
catch (Exception e)
{