Skip to content

Instantly share code, notes, and snippets.

@ntxinh
Last active July 30, 2020 06:05
Show Gist options
  • Select an option

  • Save ntxinh/c5b57c2b2d45b09011659bf175f89617 to your computer and use it in GitHub Desktop.

Select an option

Save ntxinh/c5b57c2b2d45b09011659bf175f89617 to your computer and use it in GitHub Desktop.

Revisions

  1. ntxinh revised this gist Jun 21, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TestController.cs
    Original 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(_httpClientService, "/api/hello-world");
    var response = await _httpClientService.GetAsync(_httpClientA, "/api/hello-world");
    return Ok("Hello World");
    }
    }
  2. ntxinh revised this gist Jun 11, 2020. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions TestController.cs
    Original 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");
    }
    }
  3. ntxinh revised this gist Jun 11, 2020. 2 changed files with 11 additions and 41 deletions.
    41 changes: 6 additions & 35 deletions HttpClientService.cs
    Original file line number Diff line number Diff line change
    @@ -9,19 +9,11 @@ namespace Infrastructure.Services
    {
    public class HttpClientService : IHttpClientService
    {
    private readonly IHttpClientFactory _clientFactory;
    private readonly HttpClient httpClientA;
    private readonly HttpClient httpClientB;

    public HttpClientService(IHttpClientFactory clientFactory)
    public HttpClientService()
    {
    _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)
    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<
    }
    }

    private async Task<Stream> GetStreamAsync(HttpClient httpClient, string url, Dictionary<string, string> queryParams, Dictionary<string, string> headers)
    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
    }
    }

    private async Task<T> PostAsJsonAsync<T>(HttpClient httpClient, string url, object data, Dictionary<string, string> queryParams, Dictionary<string, string> headers)
    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
    }
    }

    private async Task<T> PostAsFormUrlEncodedAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> data, Dictionary<string, string> queryParams, Dictionary<string, string> headers)
    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

    #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
    }
    }
    11 changes: 5 additions & 6 deletions IHttpClientService.cs
    Original 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
    {
    #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
    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);
    }
    }
  4. ntxinh created this gist Jun 11, 2020.
    161 changes: 161 additions & 0 deletions HttpClientService.cs
    Original 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
    }
    }
    16 changes: 16 additions & 0 deletions IHttpClientService.cs
    Original 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
    }
    }