Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generic JSON HttpClient
using System.Threading.Tasks;
namespace BrunoMartinsPro.Http
{
public interface IJsonHttpClient
{
/// <summary>
/// Add Request Headers
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void AddDefaultRequestHeaders(string name, string value);
/// <summary>
/// Add Authorization Header to Default Request Headers
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void AddAuthorizationHeader(string name, string value);
/// <summary>
/// Performs a GET Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task<T> GetAsync<T>(string url);
/// <summary>
/// Performs a GET Request
/// </summary>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task GetAsync(string url);
/// <summary>
/// Performs a POST Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task<T> PostAsync<T>(T content, string url);
/// <summary>
/// Performs a POST Request
/// </summary>
/// <typeparam name="T">From</typeparam>
/// <typeparam name="T1">To</typeparam>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task<T1> PostAsync<T, T1>(T content, string url);
/// <summary>
/// Performs a PUT Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task<T> PutAsync<T>(int id, T content, string url);
/// <summary>
/// Performs a PUT Request
/// </summary>
/// <typeparam name="T">From</typeparam>
/// <typeparam name="T1">To</typeparam>
/// <param name="id"></param>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task<T1> PutAsync<T, T1>(int id, T content, string url);
/// <summary>
/// Performs a DELETE Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
Task<T> DeleteAsync<T>(int id, string url);
}
}
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace BrunoMartinsPro.Http
{
public class JsonHttpClient : IJsonHttpClient
{
private HttpClient Client { get; set; }
public JsonHttpClient()
{
Client = new HttpClient();
//Set Json Default
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
/// <summary>
/// Add Request Headers
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
public void AddDefaultRequestHeaders(string name, string value)
{
Client.DefaultRequestHeaders.Add(name, value);
}
/// <summary>
/// Add Authorization Header to Default Request Headers
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
public void AddAuthorizationHeader(string name, string value)
{
Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(name, value);
}
/// <summary>
/// Performs a GET Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task<T> GetAsync<T>(string url)
{
HttpResponseMessage response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await DeserializeObject<T>(response);
}
/// <summary>
/// Performs a GET Request
/// </summary>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task GetAsync(string url)
{
HttpResponseMessage response = await Client.GetAsync(url);
response.EnsureSuccessStatusCode();
}
/// <summary>
/// Performs a POST Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task<T> PostAsync<T>(T content, string url)
{
//Serialize Object
StringContent jsonContent = SerializeObject(content);
//Execute POST request
HttpResponseMessage response = await Client.PostAsync(url, jsonContent);
//Return response
return await DeserializeObject<T>(response);
}
/// <summary>
/// Performs a POST Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task<T1> PostAsync<T, T1>(T content, string url)
{
//Serialize Object
StringContent jsonContent = SerializeObject(content);
//Execute POST request
HttpResponseMessage response = await Client.PostAsync(url, jsonContent);
//Return response
return await DeserializeObject<T1>(response);
}
/// <summary>
/// Performs a PUT Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task<T> PutAsync<T>(int id, T content, string url)
{
//Serialize Object
StringContent jsonContent = SerializeObject(content);
//Execute PUT request
HttpResponseMessage response = await Client.PutAsync(url + $"/{id}", jsonContent);
//Return response
return await DeserializeObject<T>(response);
}
/// <summary>
/// Performs a PUT Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T1"></typeparam>
/// <param name="id"></param>
/// <param name="content"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task<T1> PutAsync<T, T1>(int id, T content, string url)
{
//Serialize Object
StringContent jsonContent = SerializeObject(content);
//Execute PUT request
HttpResponseMessage response = await Client.PutAsync(url + $"/{id}", jsonContent);
//Return response
return await DeserializeObject<T1>(response);
}
/// <summary>
/// Performs a DELETE Request
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <param name="url">Complete url</param>
/// <returns></returns>
public async Task<T> DeleteAsync<T>(int id, string url)
{
//Execute DELETE request
HttpResponseMessage response = await Client.DeleteAsync(url + $"/{id}");
//Return response
return await DeserializeObject<T>(response);
}
/// <summary>
/// Serialize an object to Json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <returns></returns>
private StringContent SerializeObject<T>(T content)
{
//Serialize Object
string jsonObject = JsonConvert.SerializeObject(content);
//Create Json UTF8 String Content
return new StringContent(jsonObject, Encoding.UTF8, "application/json");
}
/// <summary>
/// Deserialize object from request response
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="response"></param>
/// <returns></returns>
private async Task<T> DeserializeObject<T>(HttpResponseMessage response)
{
//Throw exception if request is not successfull
response.EnsureSuccessStatusCode();
//Read body
string responseBody = await response.Content.ReadAsStringAsync();
//Deserialize Body to object
return JsonConvert.DeserializeObject<T>(responseBody);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment