-
-
Save NDiiong/8f22f773d6ff7ce49fa4646937b51aae 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
using System.Collections.Generic; | |
using Newtonsoft.Json; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json.Converters; | |
namespace ArchDev | |
{ | |
public static class HttpClientExtensions | |
{ | |
private static JsonSerializerSettings JsonSerializerSettings() | |
{ | |
return new JsonSerializerSettings | |
{ | |
Converters = new List<JsonConverter> { new StringEnumConverter() } | |
}; | |
} | |
public static async Task<T> GetAsync<T>(this HttpClient httpClient, string url) | |
{ | |
var response = await httpClient.GetAsync(url); | |
if (response.IsSuccessStatusCode) | |
{ | |
var result = await response.Content.ReadAsStringAsync(); | |
var data = JsonConvert.DeserializeObject<T>(result, JsonSerializerSettings()); | |
return data; | |
} | |
return default; | |
} | |
public static async Task<T> PostAsync<T>(this HttpClient httpClient, string url, object request) | |
{ | |
var serialised = JsonConvert.SerializeObject(request, JsonSerializerSettings()); | |
var content = new StringContent(serialised, Encoding.UTF8, "application/json"); | |
var response = await httpClient.PostAsync(url,content); | |
if (response.IsSuccessStatusCode) | |
{ | |
var result = await response.Content.ReadAsStringAsync(); | |
var data = JsonConvert.DeserializeObject<T>(result, JsonSerializerSettings()); | |
return data; | |
} | |
return default; | |
} | |
public static async Task<T> PostFormAsync<T>(this HttpClient httpClient, string url, MultipartFormRequest request) | |
{ | |
var multipartFormDataContent = new MultipartFormDataContent(); | |
if (request.File != null) | |
{ | |
var streamContent = new StreamContent(request.File.OpenReadStream()); | |
var fileContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync()); | |
multipartFormDataContent.Add(fileContent, "\"file\"", request.File.FileName); | |
} | |
var serialised = JsonConvert.SerializeObject(request.Body, JsonSerializerSettings()); | |
var stringContent = new StringContent(serialised,Encoding.UTF8, "application/json"); | |
multipartFormDataContent.Add(stringContent, "\"body\""); | |
var response = await httpClient.PostAsync(url, multipartFormDataContent); | |
if (response.IsSuccessStatusCode) | |
{ | |
var result = await response.Content.ReadAsStringAsync(); | |
var data = JsonConvert.DeserializeObject<T>(result, JsonSerializerSettings()); | |
return data; | |
} | |
return default; | |
} | |
public static async Task<bool> PatchAsync(this HttpClient httpClient, string url, object request) | |
{ | |
var serialised = JsonConvert.SerializeObject(request, JsonSerializerSettings()); | |
var content = new StringContent(serialised, Encoding.UTF8, "application/json"); | |
var response = await httpClient.PatchAsync(url, content); | |
return response.IsSuccessStatusCode; | |
} | |
public static async Task<T> PutAsync<T>(this HttpClient httpClient, string url, object request) | |
{ | |
var serialised = JsonConvert.SerializeObject(request, JsonSerializerSettings()); | |
var content = new StringContent(serialised, Encoding.UTF8, "application/json"); | |
var response = await httpClient.PutAsync(url, content); | |
if (response.IsSuccessStatusCode) | |
{ | |
var result = await response.Content.ReadAsStringAsync(); | |
var data = JsonConvert.DeserializeObject<T>(result, JsonSerializerSettings()); | |
return data; | |
} | |
return default; | |
} | |
} | |
} |
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
using Microsoft.AspNetCore.Http; | |
namespace ArchDev | |
{ | |
public class MultipartFormRequest | |
{ | |
public IFormFile File { get; set; } | |
public object Body { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment