Browse Source

Refactoing HttpClientApi

pull/126/head
Ramón Tomás 8 years ago
parent
commit
ddb03a0aa8
8 changed files with 87 additions and 24 deletions
  1. +4
    -6
      src/Web/WebMVC/Services/BasketService.cs
  2. +3
    -6
      src/Web/WebMVC/Services/CatalogService.cs
  3. +3
    -6
      src/Web/WebMVC/Services/OrderingService.cs
  4. +45
    -0
      src/Web/WebMVC/Services/Utilities/HttpApiClient.cs
  5. +3
    -3
      src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs
  6. +16
    -0
      src/Web/WebMVC/Services/Utilities/IHttpClient.cs
  7. +12
    -3
      src/Web/WebMVC/Startup.cs
  8. +1
    -0
      src/Web/WebMVC/appsettings.json

+ 4
- 6
src/Web/WebMVC/Services/BasketService.cs View File

@ -14,15 +14,16 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public class BasketService : IBasketService
{
private readonly IOptionsSnapshot<AppSettings> _settings;
private HttpClientWrapper _apiClient;
private IHttpClient _apiClient;
private readonly string _remoteServiceBaseUrl;
private IHttpContextAccessor _httpContextAccesor;
public BasketService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor)
public BasketService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient)
{
_settings = settings;
_remoteServiceBaseUrl = _settings.Value.BasketUrl;
_httpContextAccesor = httpContextAccesor;
_apiClient = httpClient;
}
public async Task<Basket> GetBasket(ApplicationUser user)
@ -30,7 +31,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
var context = _httpContextAccesor.HttpContext;
var token = await context.Authentication.GetTokenAsync("access_token");
var _apiClient = new HttpClientWrapper();
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id.ToString()}";
@ -51,8 +51,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
{
var context = _httpContextAccesor.HttpContext;
var token = await context.Authentication.GetTokenAsync("access_token");
_apiClient = new HttpClientWrapper();
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var basketUrl = _remoteServiceBaseUrl;
@ -119,7 +118,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
var context = _httpContextAccesor.HttpContext;
var token = await context.Authentication.GetTokenAsync("access_token");
_apiClient = new HttpClientWrapper();
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id.ToString()}";
var response = await _apiClient.DeleteAsync(basketUrl);


+ 3
- 6
src/Web/WebMVC/Services/CatalogService.cs View File

@ -13,20 +13,19 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public class CatalogService : ICatalogService
{
private readonly IOptionsSnapshot<AppSettings> _settings;
private HttpClientWrapper _apiClient;
private IHttpClient _apiClient;
private readonly string _remoteServiceBaseUrl;
public CatalogService(IOptionsSnapshot<AppSettings> settings, ILoggerFactory loggerFactory) {
public CatalogService(IOptionsSnapshot<AppSettings> settings, ILoggerFactory loggerFactory, IHttpClient httpClient) {
_settings = settings;
_remoteServiceBaseUrl = $"{_settings.Value.CatalogUrl}/api/v1/catalog/";
_apiClient = httpClient;
var log = loggerFactory.CreateLogger("catalog service");
log.LogDebug(settings.Value.CatalogUrl);
}
public async Task<Catalog> GetCatalogItems(int page,int take, int? brand, int? type)
{
_apiClient = new HttpClientWrapper();
var itemsQs = $"items?pageIndex={page}&pageSize={take}";
var filterQs = "";
@ -53,7 +52,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public async Task<IEnumerable<SelectListItem>> GetBrands()
{
_apiClient = new HttpClientWrapper();
var url = $"{_remoteServiceBaseUrl}catalogBrands";
var dataString = await _apiClient.GetStringAsync(url);
@ -72,7 +70,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
public async Task<IEnumerable<SelectListItem>> GetTypes()
{
_apiClient = new HttpClientWrapper();
var url = $"{_remoteServiceBaseUrl}catalogTypes";
var dataString = await _apiClient.GetStringAsync(url);


+ 3
- 6
src/Web/WebMVC/Services/OrderingService.cs View File

@ -14,24 +14,23 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
{
public class OrderingService : IOrderingService
{
private HttpClientWrapper _apiClient;
private IHttpClient _apiClient;
private readonly string _remoteServiceBaseUrl;
private readonly IOptionsSnapshot<AppSettings> _settings;
private readonly IHttpContextAccessor _httpContextAccesor;
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor)
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor, IHttpClient httpClient)
{
_remoteServiceBaseUrl = $"{settings.Value.OrderingUrl}/api/v1/orders";
_settings = settings;
_httpContextAccesor = httpContextAccesor;
_apiClient = httpClient;
}
async public Task<Order> GetOrder(ApplicationUser user, string Id)
{
var context = _httpContextAccesor.HttpContext;
var token = await context.Authentication.GetTokenAsync("access_token");
_apiClient = new HttpClientWrapper();
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var ordersUrl = $"{_remoteServiceBaseUrl}/{Id}";
@ -47,7 +46,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
var context = _httpContextAccesor.HttpContext;
var token = await context.Authentication.GetTokenAsync("access_token");
_apiClient = new HttpClientWrapper();
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var ordersUrl = _remoteServiceBaseUrl;
@ -78,7 +76,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
var context = _httpContextAccesor.HttpContext;
var token = await context.Authentication.GetTokenAsync("access_token");
_apiClient = new HttpClientWrapper();
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
_apiClient.Inst.DefaultRequestHeaders.Add("x-requestid", order.RequestId.ToString());


+ 45
- 0
src/Web/WebMVC/Services/Utilities/HttpApiClient.cs View File

@ -0,0 +1,45 @@
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebMVC.Services.Utilities
{
public class HttpApiClient : IHttpClient
{
private HttpClient _client;
private ILogger _logger;
public HttpClient Inst => _client;
public HttpApiClient()
{
_client = new HttpClient();
_logger = new LoggerFactory().CreateLogger(nameof(HttpApiClientWrapper));
}
public async Task<string> GetStringAsync(string uri)
{
return await HttpInvoker(async () =>
await _client.GetStringAsync(uri));
}
public async Task<HttpResponseMessage> PostAsync<T>(string uri, T item)
{
var contentString = new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json");
return await HttpInvoker(async () =>
await _client.PostAsync(uri, contentString));
}
public async Task<HttpResponseMessage> DeleteAsync(string uri)
{
return await HttpInvoker(async () =>
await _client.DeleteAsync(uri));
}
private async Task<T> HttpInvoker<T>(Func<Task<T>> action)
{
return await action();
}
}
}

src/Web/WebMVC/Services/Utilities/HttpClientWrapper.cs → src/Web/WebMVC/Services/Utilities/HttpApiClientWrapper.cs View File

@ -8,16 +8,16 @@ using System.Threading.Tasks;
namespace WebMVC.Services.Utilities
{
public class HttpClientWrapper
public class HttpApiClientWrapper : IHttpClient
{
private HttpClient _client;
private PolicyWrap _policyWrapper;
private ILogger _logger;
public HttpClient Inst => _client;
public HttpClientWrapper()
public HttpApiClientWrapper()
{
_client = new HttpClient();
_logger = new LoggerFactory().CreateLogger(nameof(HttpClientWrapper));
_logger = new LoggerFactory().CreateLogger(nameof(HttpApiClientWrapper));
// Add Policies to be applied
_policyWrapper = Policy.WrapAsync(

+ 16
- 0
src/Web/WebMVC/Services/Utilities/IHttpClient.cs View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace WebMVC.Services.Utilities
{
public interface IHttpClient
{
HttpClient Inst { get; }
Task<string> GetStringAsync(string uri);
Task<HttpResponseMessage> PostAsync<T>(string uri, T item);
Task<HttpResponseMessage> DeleteAsync(string uri);
}
}

+ 12
- 3
src/Web/WebMVC/Startup.cs View File

@ -14,6 +14,7 @@ using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Http;
using System.Threading;
using Microsoft.Extensions.Options;
using WebMVC.Services.Utilities;
namespace Microsoft.eShopOnContainers.WebMVC
{
@ -43,14 +44,22 @@ namespace Microsoft.eShopOnContainers.WebMVC
{
services.AddMvc();
services.Configure<AppSettings>(Configuration);
// Add application services.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<ICatalogService, CatalogService>();
services.AddTransient<IOrderingService, OrderingService>();
services.AddTransient<IBasketService, BasketService>();
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
if(Configuration.GetValue<string>("ActivateCircuitBreaker") == bool.TrueString)
{
services.AddSingleton<IHttpClient, HttpApiClientWrapper>();
}
else
{
services.AddSingleton<IHttpClient, HttpApiClient>();
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.


+ 1
- 0
src/Web/WebMVC/appsettings.json View File

@ -4,6 +4,7 @@
"BasketUrl": "http://localhost:5103",
"IdentityUrl": "http://localhost:5105",
"CallBackUrl": "http://localhost:5100/",
"ActivateCircuitBreaker": "True",
"Logging": {
"IncludeScopes": false,
"LogLevel": {


Loading…
Cancel
Save