Created
February 19, 2025 13:20
-
-
Save mahelbir/ff02ebce8970eb132c5ae914e04af264 to your computer and use it in GitHub Desktop.
.NET Http Client Library
This file contains hidden or 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.Text; | |
using Newtonsoft.Json; | |
namespace Common; | |
public class MClient | |
{ | |
private readonly HttpClient _httpClient; | |
private readonly List<Task<HttpResponseMessage>> _requests; | |
private List<MClientResponse> _responses; | |
public MClient(TimeSpan timeout = default) | |
{ | |
timeout = timeout == default ? TimeSpan.FromSeconds(30) : timeout; | |
_requests = []; | |
_responses = []; | |
_httpClient = new HttpClient(); | |
_httpClient.Timeout = timeout; | |
} | |
public void Get(string url, Dictionary<string, string>? queryParams = null, Dictionary<string, string>? headers = null) | |
{ | |
var requestMessage = new HttpRequestMessage(HttpMethod.Get, url); | |
if (headers != null) | |
{ | |
foreach (var header in headers) | |
{ | |
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value); | |
} | |
} | |
var requestTask = _httpClient.SendAsync(requestMessage); | |
_requests.Add(requestTask); | |
} | |
public void PostRaw(string url, StringContent? stringContent, Dictionary<string, string>? headers = null) | |
{ | |
var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) | |
{ | |
Content = stringContent | |
}; | |
if (headers != null) | |
{ | |
foreach (var header in headers) | |
{ | |
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value); | |
} | |
} | |
var requestTask = _httpClient.SendAsync(requestMessage); | |
_requests.Add(requestTask); | |
} | |
public void PostData<TRequest>(string url, TRequest? body, Dictionary<string, string>? headers = null) | |
{ | |
FormUrlEncodedContent content; | |
if (body != null) | |
{ | |
content = new FormUrlEncodedContent( | |
body.GetType().GetProperties().ToDictionary( | |
prop => prop.Name, | |
prop => prop.GetValue(body)?.ToString() ?? string.Empty | |
) | |
); | |
} | |
else | |
{ | |
content = new FormUrlEncodedContent(new Dictionary<string, string>()); | |
} | |
var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) | |
{ | |
Content = content | |
}; | |
if (headers != null) | |
{ | |
foreach (var header in headers) | |
{ | |
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value); | |
} | |
} | |
var requestTask = _httpClient.SendAsync(requestMessage); | |
_requests.Add(requestTask); | |
} | |
public void PostJson<TRequest>(string url, TRequest? body, Dictionary<string, string>? headers = null) | |
{ | |
var jsonContent = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); | |
var requestMessage = new HttpRequestMessage(HttpMethod.Post, url) | |
{ | |
Content = jsonContent | |
}; | |
if (headers != null) | |
{ | |
foreach (var header in headers) | |
{ | |
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value); | |
} | |
} | |
var requestTask = _httpClient.SendAsync(requestMessage); | |
_requests.Add(requestTask); | |
} | |
public async Task<List<MClientResponse>> ExecuteAsync() | |
{ | |
_responses = new List<MClientResponse>(); | |
var tasks = _requests.Select(async request => | |
{ | |
try | |
{ | |
var response = await request; | |
var responseBody = await response.Content.ReadAsStringAsync(); | |
_responses.Add(new MClientResponse | |
{ | |
Code = (int)response.StatusCode, | |
Body = responseBody, | |
Headers = response.Headers.ToDictionary(h => h.Key, h => string.Join(", ", h.Value)) | |
}); | |
} | |
catch (Exception ex) | |
{ | |
_responses.Add(new MClientResponse | |
{ | |
Code = 0, | |
Body = ex.Message, | |
Headers = new Dictionary<string, string>() | |
}); | |
} | |
}); | |
await Task.WhenAll(tasks); | |
return _responses; | |
} | |
} | |
public class MClientResponse | |
{ | |
public int Code { get; set; } | |
public string Body { get; set; } | |
public Dictionary<string, string> Headers { get; set; } | |
public TResponse Json<TResponse>() | |
{ | |
if (string.IsNullOrWhiteSpace(Body)) | |
{ | |
throw new JsonException("Body is unavailable"); | |
} | |
var result = JsonConvert.DeserializeObject<TResponse>(Body); | |
if (result == null) | |
{ | |
throw new JsonException("Json deserialization failed"); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment