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,12 +1,11 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Geekbot.net.Lib.Extensions;
|
using Geekbot.net.Lib.Extensions;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Integrations.UbranDictionary
|
namespace Geekbot.net.Commands.Integrations.UbranDictionary
|
||||||
{
|
{
|
||||||
|
@ -25,15 +24,7 @@ namespace Geekbot.net.Commands.Integrations.UbranDictionary
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var client = new HttpClient
|
var definitions = await HttpAbstractions.Get<UrbanResponseDto>(new Uri($"https://api.urbandictionary.com/v0/define?term={word}"));
|
||||||
{
|
|
||||||
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);
|
|
||||||
if (definitions.List.Count == 0)
|
if (definitions.List.Count == 0)
|
||||||
{
|
{
|
||||||
await ReplyAsync("That word hasn't been defined...");
|
await ReplyAsync("That word hasn't been defined...");
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Randomness.Cat
|
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"));
|
||||||
try
|
var eb = new EmbedBuilder
|
||||||
{
|
{
|
||||||
using var client = new HttpClient
|
ImageUrl = response.File
|
||||||
{
|
};
|
||||||
BaseAddress = new Uri("https://aws.random.cat")
|
await ReplyAsync("", false, eb.Build());
|
||||||
};
|
|
||||||
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)");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Randomness.Chuck
|
namespace Geekbot.net.Commands.Randomness.Chuck
|
||||||
{
|
{
|
||||||
|
@ -25,15 +24,8 @@ namespace Geekbot.net.Commands.Randomness.Chuck
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var client = new HttpClient();
|
var response = await HttpAbstractions.Get<ChuckNorrisJokeResponseDto>(new Uri("https://api.chucknorris.io/jokes/random"));
|
||||||
client.DefaultRequestHeaders.Accept.Clear();
|
await ReplyAsync(response.Value);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
catch (HttpRequestException)
|
catch (HttpRequestException)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Randomness.Dad
|
namespace Geekbot.net.Commands.Randomness.Dad
|
||||||
{
|
{
|
||||||
|
@ -23,22 +21,8 @@ namespace Geekbot.net.Commands.Randomness.Dad
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
var response = await HttpAbstractions.Get<DadJokeResponseDto>(new Uri("https://icanhazdadjoke.com/"));
|
||||||
{
|
await ReplyAsync(response.Joke);
|
||||||
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...");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Randomness.Dog
|
namespace Geekbot.net.Commands.Randomness.Dog
|
||||||
{
|
{
|
||||||
|
@ -23,27 +22,12 @@ namespace Geekbot.net.Commands.Randomness.Dog
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
var response = await HttpAbstractions.Get<DogResponseDto>(new Uri("http://random.dog/woof.json"));
|
||||||
|
var eb = new EmbedBuilder
|
||||||
{
|
{
|
||||||
using var client = new HttpClient
|
ImageUrl = response.Url
|
||||||
{
|
};
|
||||||
BaseAddress = new Uri("http://random.dog")
|
await ReplyAsync("", false, eb.Build());
|
||||||
};
|
|
||||||
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}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Geekbot.net.Commands.Randomness.Cat;
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Geekbot.net.Lib.Extensions;
|
using Geekbot.net.Lib.Extensions;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Randomness.Greetings
|
namespace Geekbot.net.Commands.Randomness.Greetings
|
||||||
{
|
{
|
||||||
|
@ -26,7 +24,7 @@ namespace Geekbot.net.Commands.Randomness.Greetings
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var greeting = await GetRandomGreeting();
|
var greeting = await HttpAbstractions.Get<GreetingBaseDto>(new Uri("https://api.greetings.dev/v1/greeting"));
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
var eb = new EmbedBuilder();
|
||||||
eb.Title = greeting.Primary.Text;
|
eb.Title = greeting.Primary.Text;
|
||||||
|
@ -49,18 +47,5 @@ namespace Geekbot.net.Commands.Randomness.Greetings
|
||||||
await _errorHandler.HandleCommandException(e, Context);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Net.Http.Headers;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Randomness.Kanye
|
namespace Geekbot.net.Commands.Randomness.Kanye
|
||||||
{
|
{
|
||||||
|
@ -23,22 +21,8 @@ namespace Geekbot.net.Commands.Randomness.Kanye
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
try
|
var response = await HttpAbstractions.Get<KanyeResponseDto>(new Uri("https://api.kanye.rest/"));
|
||||||
{
|
await ReplyAsync(response.Quote);
|
||||||
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...");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using Geekbot.net.Lib;
|
||||||
using Geekbot.net.Lib.ErrorHandling;
|
using Geekbot.net.Lib.ErrorHandling;
|
||||||
using Newtonsoft.Json;
|
|
||||||
|
|
||||||
namespace Geekbot.net.Commands.Utils.Changelog
|
namespace Geekbot.net.Commands.Utils.Changelog
|
||||||
{
|
{
|
||||||
|
@ -29,17 +28,14 @@ namespace Geekbot.net.Commands.Utils.Changelog
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var client = new HttpClient
|
var commits = await HttpAbstractions.Get<List<CommitDto>>(
|
||||||
{
|
new Uri("https://api.github.com/repos/pizzaandcoffee/geekbot.net/commits"),
|
||||||
BaseAddress = new Uri("https://api.github.com")
|
new Dictionary<string, string>()
|
||||||
};
|
{
|
||||||
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent",
|
{ "User-Agent", "http://developer.github.com/v3/#user-agent-required" }
|
||||||
"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<List<CommitDto>>(stringResponse);
|
|
||||||
var eb = new EmbedBuilder();
|
var eb = new EmbedBuilder();
|
||||||
eb.WithColor(new Color(143, 165, 102));
|
eb.WithColor(new Color(143, 165, 102));
|
||||||
eb.WithAuthor(new EmbedAuthorBuilder
|
eb.WithAuthor(new EmbedAuthorBuilder
|
||||||
|
|
41
Geekbot.net/Lib/HttpAbstractions.cs
Normal file
41
Geekbot.net/Lib/HttpAbstractions.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue