Add an abstraction for http calls
This commit is contained in:
parent
0589b8e91b
commit
ba0d116f3e
9 changed files with 75 additions and 135 deletions
|
@ -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.Dog
|
||||
{
|
||||
|
@ -23,27 +22,12 @@ namespace Geekbot.net.Commands.Randomness.Dog
|
|||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
var response = await HttpAbstractions.Get<DogResponseDto>(new Uri("http://random.dog/woof.json"));
|
||||
var eb = new EmbedBuilder
|
||||
{
|
||||
using var client = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri("http://random.dog")
|
||||
};
|
||||
var response = await client.GetAsync("/woof.json");
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var stringResponse = await response.Content.ReadAsStringAsync();
|
||||
var dogFile = JsonConvert.DeserializeObject<DogResponseDto>(stringResponse);
|
||||
var eb = new EmbedBuilder
|
||||
{
|
||||
ImageUrl = dogFile.Url
|
||||
};
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}");
|
||||
}
|
||||
ImageUrl = response.Url
|
||||
};
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue