From ba0d116f3e4e70bdc1a10b9bd8e19e603bf9b774 Mon Sep 17 00:00:00 2001 From: runebaas Date: Wed, 15 Jul 2020 02:52:13 +0200 Subject: [PATCH] Add an abstraction for http calls --- .../UbranDictionary/UrbanDictionary.cs | 13 +----- Geekbot.net/Commands/Randomness/Cat/Cat.cs | 29 +++---------- .../Randomness/Chuck/ChuckNorrisJokes.cs | 14 ++----- .../Commands/Randomness/Dad/DadJokes.cs | 22 ++-------- Geekbot.net/Commands/Randomness/Dog/Dog.cs | 28 +++---------- .../Randomness/Greetings/Greetings.cs | 19 +-------- .../Commands/Randomness/Kanye/Kanye.cs | 22 ++-------- .../Commands/Utils/Changelog/Changelog.cs | 22 ++++------ Geekbot.net/Lib/HttpAbstractions.cs | 41 +++++++++++++++++++ 9 files changed, 75 insertions(+), 135 deletions(-) create mode 100644 Geekbot.net/Lib/HttpAbstractions.cs diff --git a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs index 1f33573..882caa5 100644 --- a/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs +++ b/Geekbot.net/Commands/Integrations/UbranDictionary/UrbanDictionary.cs @@ -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(stringResponse); + var definitions = await HttpAbstractions.Get(new Uri($"https://api.urbandictionary.com/v0/define?term={word}")); if (definitions.List.Count == 0) { await ReplyAsync("That word hasn't been defined..."); diff --git a/Geekbot.net/Commands/Randomness/Cat/Cat.cs b/Geekbot.net/Commands/Randomness/Cat/Cat.cs index 3ac0764..7cd7b19 100644 --- a/Geekbot.net/Commands/Randomness/Cat/Cat.cs +++ b/Geekbot.net/Commands/Randomness/Cat/Cat.cs @@ -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(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(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) { diff --git a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs index 5c28a9a..95dcfd4 100644 --- a/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs +++ b/Geekbot.net/Commands/Randomness/Chuck/ChuckNorrisJokes.cs @@ -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(stringResponse); - await ReplyAsync(data.Value); + var response = await HttpAbstractions.Get(new Uri("https://api.chucknorris.io/jokes/random")); + await ReplyAsync(response.Value); } catch (HttpRequestException) { diff --git a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs index bcedd96..7aabff3 100644 --- a/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs +++ b/Geekbot.net/Commands/Randomness/Dad/DadJokes.cs @@ -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(stringResponse); - await ReplyAsync(data.Joke); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var response = await HttpAbstractions.Get(new Uri("https://icanhazdadjoke.com/")); + await ReplyAsync(response.Joke); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Randomness/Dog/Dog.cs b/Geekbot.net/Commands/Randomness/Dog/Dog.cs index d7404c6..b3d603b 100644 --- a/Geekbot.net/Commands/Randomness/Dog/Dog.cs +++ b/Geekbot.net/Commands/Randomness/Dog/Dog.cs @@ -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(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(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) { diff --git a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs index c47bf2a..85e9152 100644 --- a/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs +++ b/Geekbot.net/Commands/Randomness/Greetings/Greetings.cs @@ -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(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 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(stringResponse); - } } } \ No newline at end of file diff --git a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs index 36f1d1a..2aca31d 100644 --- a/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs +++ b/Geekbot.net/Commands/Randomness/Kanye/Kanye.cs @@ -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(stringResponse); - await ReplyAsync(data.Quote); - } - catch (HttpRequestException) - { - await ReplyAsync("Api down..."); - } + var response = await HttpAbstractions.Get(new Uri("https://api.kanye.rest/")); + await ReplyAsync(response.Quote); } catch (Exception e) { diff --git a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs index 02981df..335ee3f 100644 --- a/Geekbot.net/Commands/Utils/Changelog/Changelog.cs +++ b/Geekbot.net/Commands/Utils/Changelog/Changelog.cs @@ -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 stringResponse = await response.Content.ReadAsStringAsync(); - var commits = JsonConvert.DeserializeObject>(stringResponse); + var commits = await HttpAbstractions.Get>( + new Uri("https://api.github.com/repos/pizzaandcoffee/geekbot.net/commits"), + new Dictionary() + { + { "User-Agent", "http://developer.github.com/v3/#user-agent-required" } + } + ); + var eb = new EmbedBuilder(); eb.WithColor(new Color(143, 165, 102)); eb.WithAuthor(new EmbedAuthorBuilder diff --git a/Geekbot.net/Lib/HttpAbstractions.cs b/Geekbot.net/Lib/HttpAbstractions.cs new file mode 100644 index 0000000..cf609f6 --- /dev/null +++ b/Geekbot.net/Lib/HttpAbstractions.cs @@ -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 Get(Uri location, Dictionary 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(stringResponse); + } + } +} \ No newline at end of file