Last active
July 30, 2020 06:05
-
-
Save ntxinh/c5b57c2b2d45b09011659bf175f89617 to your computer and use it in GitHub Desktop.
Revisions
-
ntxinh revised this gist
Jun 21, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ IHttpClientService httpClientService [HttpGet("hello-world")] public async Task<IActionResult> HelloWorld() { var response = await _httpClientService.GetAsync(_httpClientA, "/api/hello-world"); return Ok("Hello World"); } } -
ntxinh revised this gist
Jun 11, 2020 . 1 changed file with 27 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ namespace Web.Controllers { [Produces("application/json")] [Route("api/[controller]")] public class ToolController: Controller { private readonly IHttpClientFactory _httpClientFactory; private readonly IHttpClientService _httpClientService; private readonly HttpClient _httpClientA; public ToolController( IHttpClientFactory httpClientFactory, IHttpClientService httpClientService ) { _httpClientFactory = httpClientFactory; _httpClientService = httpClientService; _httpClientA = _httpClientFactory.CreateClient("A"); } } [HttpGet("hello-world")] public async Task<IActionResult> HelloWorld() { var response = await _httpClientService.GetAsync(_httpClientService, "/api/hello-world"); return Ok("Hello World"); } } -
ntxinh revised this gist
Jun 11, 2020 . 2 changed files with 11 additions and 41 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -9,19 +9,11 @@ namespace Infrastructure.Services { public class HttpClientService : IHttpClientService { public HttpClientService() { } public async Task<T> GetAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { try { @@ -39,7 +31,7 @@ private async Task<T> GetAsync<T>(HttpClient httpClient, string url, Dictionary< } } public async Task<Stream> GetStreamAsync(HttpClient httpClient, string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { try { @@ -52,7 +44,7 @@ private async Task<Stream> GetStreamAsync(HttpClient httpClient, string url, Dic } } public async Task<T> PostAsJsonAsync<T>(HttpClient httpClient, string url, object data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { try { @@ -70,7 +62,7 @@ private async Task<T> PostAsJsonAsync<T>(HttpClient httpClient, string url, obje } } public async Task<T> PostAsFormUrlEncodedAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { try { @@ -88,6 +80,7 @@ private async Task<T> PostAsFormUrlEncodedAsync<T>(HttpClient httpClient, string } } #region Private Method private HttpRequestMessage CreatePostAsJsonRequest(string url, object data, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { // TODO: Handle queryParams @@ -135,27 +128,5 @@ private HttpRequestMessage CreateGetRequest(string url, Dictionary<string, strin return request; } #endregion } } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,15 @@ using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading.Tasks; namespace Infrastructure.Services { public interface IHttpClientService { Task<T> GetAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); Task<T> PostAsJsonAsync<T>(HttpClient httpClient, string url, object data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); Task<T> PostAsFormUrlEncodedAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); Task<Stream> GetStreamAsync(HttpClient httpClient, string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); } } -
ntxinh created this gist
Jun 11, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,161 @@ using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Formatting; using System.Threading.Tasks; namespace Infrastructure.Services { public class HttpClientService : IHttpClientService { private readonly IHttpClientFactory _clientFactory; private readonly HttpClient httpClientA; private readonly HttpClient httpClientB; public HttpClientService(IHttpClientFactory clientFactory) { _clientFactory = clientFactory; httpClientA = _clientFactory.CreateClient("A"); httpClientB = _clientFactory.CreateClient("B"); } #region Wrapper private async Task<T> GetAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { try { var request = CreateGetRequest(url, queryParams, headers); var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsAsync<T>(); } return default(T); } catch (Exception ex) { throw ex; } } private async Task<Stream> GetStreamAsync(HttpClient httpClient, string url, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { try { // TODO: Handle queryParams, headers return await httpClient.GetStreamAsync(url); } catch (Exception ex) { throw ex; } } private async Task<T> PostAsJsonAsync<T>(HttpClient httpClient, string url, object data, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { try { var request = CreatePostAsJsonRequest(url, data, queryParams, headers); var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsAsync<T>(); } return default(T); } catch (Exception ex) { throw ex; } } private async Task<T> PostAsFormUrlEncodedAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> data, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { try { var request = CreatePostAsFormUrlEncodedRequest(url, data, queryParams, headers); var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsAsync<T>(); } return default(T); } catch (Exception ex) { throw ex; } } private HttpRequestMessage CreatePostAsJsonRequest(string url, object data, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { // TODO: Handle queryParams MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); HttpContent content = new ObjectContent<object>(data, jsonFormatter); var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = content, }; if (headers != null) { foreach (KeyValuePair<string, string> entry in headers) { request.Headers.Add(entry.Key, entry.Value); } } return request; } private HttpRequestMessage CreatePostAsFormUrlEncodedRequest(string url, Dictionary<string, string> data, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { // TODO: Handle queryParams var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(data), }; if (headers != null) { foreach (KeyValuePair<string, string> entry in headers) { request.Headers.Add(entry.Key, entry.Value); } } return request; } private HttpRequestMessage CreateGetRequest(string url, Dictionary<string, string> queryParams, Dictionary<string, string> headers) { // TODO: Handle queryParams var request = new HttpRequestMessage(HttpMethod.Get, url); foreach (KeyValuePair<string, string> entry in headers) { request.Headers.Add(entry.Key, entry.Value); } return request; } #endregion #region A public async Task<T> GetAsync<T>(string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { return await GetAsync<T>(httpClientA, url, queryParams, headers); } public async Task<T> PostAsJsonAsync<T>(string url, object data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { return await PostAsJsonAsync<T>(httpClientA, url, data, queryParams, headers); } public async Task<T> PostAsFormUrlEncodedAsync<T>(string url, Dictionary<string, string> data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { return await PostAsFormUrlEncodedAsync<T>(httpClientA, url, data, queryParams, headers); } public async Task<Stream> GetStreamAsync(string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null) { return await GetStreamAsync(httpClientA, url, queryParams, headers); } #endregion } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ using System.Collections.Generic; using System.IO; using System.Threading.Tasks; namespace Infrastructure.Services { public interface IHttpClientService { #region A Task<T> GetAsync<T>(string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); Task<T> PostAsJsonAsync<T>(string url, object data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); Task<T> PostAsFormUrlEncodedAsync<T>(string url, Dictionary<string, string> data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); Task<Stream> GetStreamAsync(string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null); #endregion } }