Add an abstraction for http calls
This commit is contained in:
parent
0589b8e91b
commit
ba0d116f3e
9 changed files with 75 additions and 135 deletions
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…
Add table
Add a link
Reference in a new issue