|
using Newtonsoft.Json; |
|
using Newtonsoft.Json.Linq; |
|
using System; |
|
using System.Net; |
|
using System.Net.Http; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
|
|
namespace Client.Tests |
|
{ |
|
public static class HttpClientExtensions |
|
{ |
|
public static Task<(HttpStatusCode statusCode, T content)> GetAs<T>(this HttpClient client, string address) where T : class |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
return client.Get<T>(address); |
|
} |
|
|
|
private static async Task<(HttpStatusCode statusCode, TReturn content)> Get<TReturn>(this HttpClient client, string address) where TReturn : class |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
var response = await client.GetAsync(new Uri(address, UriKind.Relative)); |
|
var content = await response.Content.ReadAsStringAsync(); |
|
|
|
if (typeof(JArray).IsAssignableFrom(typeof(TReturn)) || typeof(JObject).IsAssignableFrom(typeof(TReturn))) |
|
{ |
|
return (response.StatusCode, JsonConvert.DeserializeObject(content) as TReturn); |
|
} |
|
else |
|
{ |
|
return (response.StatusCode, JsonConvert.DeserializeObject<TReturn>(content)); |
|
} |
|
} |
|
|
|
public static async Task<HttpStatusCode> Post(this HttpClient client, string address, object data) |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
using var json = data.ToJson(); |
|
var response = await client.PostAsync(new Uri(address, UriKind.Relative), json); |
|
|
|
return response.StatusCode; |
|
} |
|
|
|
public static async Task<(HttpStatusCode statusCode, TReturn content)> PostWithResult<TReturn>(this HttpClient client, string address, object data) |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
using var json = data.ToJson(); |
|
var response = await client.PostAsync(new Uri(address, UriKind.Relative), json); |
|
var body = await response.Content.ReadAsStringAsync(); |
|
var result = body.FromJson<TReturn>(); |
|
|
|
return (response.StatusCode, result); |
|
} |
|
|
|
public static async Task<HttpStatusCode> Put(this HttpClient client, string address, object data) |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
using var json = data.ToJson(); |
|
var response = await client.PutAsync(new Uri(address, UriKind.Relative), json); |
|
return response.StatusCode; |
|
} |
|
|
|
public static async Task<(HttpStatusCode statusCode, TReturn content)> PutWithResult<TReturn>(this HttpClient client, string address, object data) |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
using var json = data.ToJson(); |
|
var response = await client.PutAsync(new Uri(address, UriKind.Relative), json); |
|
var body = await response.Content.ReadAsStringAsync(); |
|
var result = body.FromJson<TReturn>(); |
|
|
|
return (response.StatusCode, result); |
|
} |
|
|
|
public static async Task<(HttpStatusCode statusCode, TReturn content)> PatchWithResult<TReturn>(this HttpClient client, string address, object data) |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
|
|
using var json = data.ToJson(); |
|
var response = await client.PatchAsync(new Uri(address, UriKind.Relative), json); |
|
var body = await response.Content.ReadAsStringAsync(); |
|
var result = body.FromJson<TReturn>(); |
|
|
|
return (response.StatusCode, result); |
|
} |
|
|
|
public static async Task<HttpStatusCode> Delete(this HttpClient client, string address) |
|
{ |
|
if (client is null) throw new ArgumentNullException(nameof(client)); |
|
var response = await client.DeleteAsync(new Uri(address, UriKind.Relative)); |
|
return response.StatusCode; |
|
} |
|
|
|
public static StringContent ToJson(this object data) |
|
{ |
|
string json = (data is string) ? (string) data : JsonConvert.SerializeObject(data); |
|
return new StringContent(json, Encoding.UTF8, "application/json"); |
|
} |
|
|
|
public static T FromJson<T>(this string json) |
|
{ |
|
return JsonConvert.DeserializeObject<T>(json); |
|
} |
|
} |
|
} |