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,12 +1,11 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Integrations.UbranDictionary
{
@ -25,15 +24,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary
{
try
{
using var client = new HttpClient
{
BaseAddress = new Uri("https://api.urbandictionary.com")
};
var response = await client.GetAsync($"/v0/define?term={word}");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var definitions = JsonConvert.DeserializeObject<UrbanResponseDto>(stringResponse);
var definitions = await HttpAbstractions.Get<UrbanResponseDto>(new Uri($"https://api.urbandictionary.com/v0/define?term={word}"));
if (definitions.List.Count == 0)
{
await ReplyAsync("That word hasn't been defined...");

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)
{

View file

@ -1,10 +1,9 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Chuck
{
@ -25,15 +24,8 @@ namespace Geekbot.net.Commands.Randomness.Chuck
{
try
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = await client.GetAsync("https://api.chucknorris.io/jokes/random");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<ChuckNorrisJokeResponseDto>(stringResponse);
await ReplyAsync(data.Value);
var response = await HttpAbstractions.Get<ChuckNorrisJokeResponseDto>(new Uri("https://api.chucknorris.io/jokes/random"));
await ReplyAsync(response.Value);
}
catch (HttpRequestException)
{

View file

@ -1,10 +1,8 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Dad
{
@ -23,22 +21,8 @@ namespace Geekbot.net.Commands.Randomness.Dad
{
try
{
try
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = await client.GetAsync("https://icanhazdadjoke.com/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<DadJokeResponseDto>(stringResponse);
await ReplyAsync(data.Joke);
}
catch (HttpRequestException)
{
await ReplyAsync("Api down...");
}
var response = await HttpAbstractions.Get<DadJokeResponseDto>(new Uri("https://icanhazdadjoke.com/"));
await ReplyAsync(response.Joke);
}
catch (Exception e)
{

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.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)
{

View file

@ -1,12 +1,10 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Commands.Randomness.Cat;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Greetings
{
@ -26,7 +24,7 @@ namespace Geekbot.net.Commands.Randomness.Greetings
{
try
{
var greeting = await GetRandomGreeting();
var greeting = await HttpAbstractions.Get<GreetingBaseDto>(new Uri("https://api.greetings.dev/v1/greeting"));
var eb = new EmbedBuilder();
eb.Title = greeting.Primary.Text;
@ -49,18 +47,5 @@ namespace Geekbot.net.Commands.Randomness.Greetings
await _errorHandler.HandleCommandException(e, Context);
}
}
private async Task<GreetingBaseDto> GetRandomGreeting()
{
using var client = new HttpClient
{
BaseAddress = new Uri("https://api.greetings.dev")
};
var response = await client.GetAsync("/v1/greeting");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<GreetingBaseDto>(stringResponse);
}
}
}

View file

@ -1,10 +1,8 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Kanye
{
@ -23,22 +21,8 @@ namespace Geekbot.net.Commands.Randomness.Kanye
{
try
{
try
{
using var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));
var response = await client.GetAsync("https://api.kanye.rest/");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<KanyeResponseDto>(stringResponse);
await ReplyAsync(data.Quote);
}
catch (HttpRequestException)
{
await ReplyAsync("Api down...");
}
var response = await HttpAbstractions.Get<KanyeResponseDto>(new Uri("https://api.kanye.rest/"));
await ReplyAsync(response.Quote);
}
catch (Exception e)
{

View file

@ -1,14 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Utils.Changelog
{
@ -29,17 +28,14 @@ namespace Geekbot.net.Commands.Utils.Changelog
{
try
{
using var client = new HttpClient
{
BaseAddress = new Uri("https://api.github.com")
};
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent",
"http://developer.github.com/v3/#user-agent-required");
var response = await client.GetAsync("/repos/pizzaandcoffee/geekbot.net/commits");
response.EnsureSuccessStatusCode();
var commits = await HttpAbstractions.Get<List<CommitDto>>(
new Uri("https://api.github.com/repos/pizzaandcoffee/geekbot.net/commits"),
new Dictionary<string, string>()
{
{ "User-Agent", "http://developer.github.com/v3/#user-agent-required" }
}
);
var stringResponse = await response.Content.ReadAsStringAsync();
var commits = JsonConvert.DeserializeObject<List<CommitDto>>(stringResponse);
var eb = new EmbedBuilder();
eb.WithColor(new Color(143, 165, 102));
eb.WithAuthor(new EmbedAuthorBuilder

View file

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Geekbot.net.Lib
{
public class HttpAbstractions
{
public static async Task<T> Get<T>(Uri location, Dictionary<string, string> additionalHeaders = null)
{
using var client = new HttpClient
{
BaseAddress = location,
DefaultRequestHeaders =
{
Accept =
{
MediaTypeWithQualityHeaderValue.Parse("application/json")
}
}
};
if (additionalHeaders != null)
{
foreach (var (name, val) in additionalHeaders)
{
client.DefaultRequestHeaders.TryAddWithoutValidation(name, val);
}
}
var response = await client.GetAsync(location.PathAndQuery);
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(stringResponse);
}
}
}