using EntrustSettle.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace EntrustSettle.Common.Http.HttpPolly { public class HttpPollyHelper : IHttpPollyHelper { private readonly IHttpClientFactory _clientFactory; public HttpPollyHelper(IHttpClientFactory httpClientFactory) { _clientFactory = httpClientFactory; } public async Task PostAsync(HttpEnum httpEnum, string url, R request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task PostAsync(HttpEnum httpEnum, string url, string request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(request, Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task PostAsync(HttpEnum httpEnum, string url, object request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var json = JsonConvert.SerializeObject(request); var stringContent = new StringContent(json, Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task PostAsync(HttpEnum httpEnum, string url, R request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return await response.Content.ReadAsStringAsync(); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task PostAsync(HttpEnum httpEnum, string url, string request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(request, Encoding.UTF8, "application/json"); var response = await client.PostAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return await response.Content.ReadAsStringAsync(); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task GetAsync(HttpEnum httpEnum, string url, Dictionary queryParams = null, Dictionary headers = null) { try { if (queryParams != null) { url += "?"; foreach (KeyValuePair param in queryParams) { url += (param.Key + "=" + System.Web.HttpUtility.UrlEncode(param.Value, Encoding.UTF8) + "&"); } url = url.TrimEnd('&'); } var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var response = await client.GetAsync(url); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task GetAsync(HttpEnum httpEnum, string url, Dictionary queryParams = null, Dictionary headers = null) { try { if (queryParams != null) { url += "?"; foreach (KeyValuePair param in queryParams) { url += (param.Key + "=" + System.Web.HttpUtility.UrlEncode(param.Value, Encoding.UTF8) + "&"); } url = url.TrimEnd('&'); } var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var response = await client.GetAsync(url); if (response.StatusCode == System.Net.HttpStatusCode.OK) { return await response.Content.ReadAsStringAsync(); ; } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task PutAsync(HttpEnum httpEnum, string url, R request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"); var response = await client.PutAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task PutAsync(HttpEnum httpEnum, string url, string request, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var stringContent = new StringContent(request, Encoding.UTF8, "application/json"); var response = await client.PutAsync(url, stringContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } public async Task DeleteAsync(HttpEnum httpEnum, string url, Dictionary headers = null) { try { var client = _clientFactory.CreateClient(httpEnum.ToString()); if (headers != null) { foreach (var header in headers) { if (!client.DefaultRequestHeaders.Contains(header.Key)) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } } var response = await client.DeleteAsync(url); if (response.StatusCode == System.Net.HttpStatusCode.OK) { string result = await response.Content.ReadAsStringAsync(); return JsonConvert.DeserializeObject(result); } else { throw new Exception($"Http Error StatusCode:{response.StatusCode}"); } } catch (Exception) { throw; } } } }