Created
May 3, 2018 09:57
-
-
Save bazwilliams/0bb07745e6633453acd087227e3236be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Integration.Tests | |
{ | |
using System; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
public static class HttpClientExtensions | |
{ | |
public static void Header(this HttpClient client, string header, string value) | |
{ | |
client.DefaultRequestHeaders.Add(header, value); | |
} | |
public static void Accept(this HttpClient client, string accept) | |
{ | |
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); | |
} | |
public static Task<HttpResponseMessage> Get(this HttpClient client, string uri, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
return client.GetAsync(uri); | |
} | |
public static Task<HttpResponseMessage> Post(this HttpClient client, string uri, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
return client.PostAsync(uri, null); | |
} | |
public static Task<HttpResponseMessage> Put(this HttpClient client, string uri, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
return client.PutAsync(uri, null); | |
} | |
public static Task<HttpResponseMessage> Patch(this HttpClient client, string uri, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
var request = new HttpRequestMessage(new HttpMethod("PATCH"), uri); | |
return client.SendAsync(request); | |
} | |
public static Task<HttpResponseMessage> Delete(this HttpClient client, string uri, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
return client.DeleteAsync(uri); | |
} | |
public static Task<HttpResponseMessage> Put<T>(this HttpClient client, string uri, T payload, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
var content = new StringContent(JsonConvert.SerializeObject(payload)); | |
return client.PutAsync(uri, content); | |
} | |
public static Task<HttpResponseMessage> Post<T>(this HttpClient client, string uri, T payload, Action<HttpClient> configurationAction) | |
{ | |
configurationAction(client); | |
var content = new StringContent(JsonConvert.SerializeObject(payload)); | |
return client.PostAsync(uri, content); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment