Skip to content

Instantly share code, notes, and snippets.

@ntxinh
Last active July 30, 2020 06:05
Show Gist options
  • Save ntxinh/c5b57c2b2d45b09011659bf175f89617 to your computer and use it in GitHub Desktop.
Save ntxinh/c5b57c2b2d45b09011659bf175f89617 to your computer and use it in GitHub Desktop.
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
{
public HttpClientService()
{
}
public async Task<T> GetAsync<T>(HttpClient httpClient, string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null)
{
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;
}
}
public async Task<Stream> GetStreamAsync(HttpClient httpClient, string url, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null)
{
try
{
// TODO: Handle queryParams, headers
return await httpClient.GetStreamAsync(url);
}
catch (Exception ex)
{
throw ex;
}
}
public async Task<T> PostAsJsonAsync<T>(HttpClient httpClient, string url, object data, Dictionary<string, string> queryParams = null, Dictionary<string, string> headers = null)
{
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;
}
}
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
{
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;
}
}
#region Private Method
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
}
}
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);
}
}
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(_httpClientA, "/api/hello-world");
return Ok("Hello World");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment