refactor mobile bff
This commit is contained in:
parent
454525d517
commit
63e20bb07f
@ -286,6 +286,7 @@ services:
|
||||
- urls__orders=http://ordering.api
|
||||
- urls__identity=http://identity.api
|
||||
- urls__grpcBasket=http://10.0.75.1:5580
|
||||
- urls__grpcCatalog=http://10.0.75.1:9101
|
||||
- urls__grpcOrdering=http://10.0.75.1:5581
|
||||
- CatalogUrlHC=http://catalog.api/hc
|
||||
- OrderingUrlHC=http://ordering.api/hc
|
||||
|
@ -28,6 +28,7 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config
|
||||
public string Catalog { get; set; }
|
||||
public string Orders { get; set; }
|
||||
public string GrpcBasket { get; set; }
|
||||
public string GrpcCatalog { get; set; }
|
||||
public string GrpcOrdering { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -27,50 +27,28 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
|
||||
public async Task<BasketData> GetById(string id)
|
||||
{
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
||||
|
||||
using (var httpClientHandler = new HttpClientHandler())
|
||||
return await GrpcCallerService.CallService(_urls.GrpcBasket, async httpClient =>
|
||||
{
|
||||
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
||||
using (var httpClient = new HttpClient(httpClientHandler))
|
||||
{
|
||||
//httpClient.BaseAddress = new Uri("http://10.0.75.1:5580");
|
||||
httpClient.BaseAddress = new Uri(_urls.GrpcBasket);
|
||||
|
||||
_logger.LogDebug("Creating grpc client for basket {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
||||
|
||||
var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
|
||||
|
||||
_logger.LogDebug("grpc client created, request = {@id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
|
||||
|
||||
_logger.LogDebug("grpc response {@response}", response);
|
||||
|
||||
return MapToBasketData(response);
|
||||
}
|
||||
catch (RpcException e)
|
||||
{
|
||||
_logger.LogError($"Error calling via grpc: {e.Status} - {e.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(BasketData currentBasket)
|
||||
{
|
||||
_httpClient.BaseAddress = new Uri(_urls.Basket + UrlsConfig.BasketOperations.UpdateBasket());
|
||||
|
||||
var client = GrpcClient.Create<Basket.BasketClient>(_httpClient);
|
||||
await GrpcCallerService.CallService(_urls.GrpcBasket, async httpClient =>
|
||||
{
|
||||
var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
|
||||
_logger.LogDebug("Grpc update basket currentBasket {@currentBasket}", currentBasket);
|
||||
var request = MapToCustomerBasketRequest(currentBasket);
|
||||
_logger.LogDebug("Grpc update basket request {@request}", request);
|
||||
|
||||
await client.UpdateBasketAsync(request);
|
||||
return client.UpdateBasketAsync(request);
|
||||
});
|
||||
}
|
||||
|
||||
private BasketData MapToBasketData(CustomerBasketResponse customerBasketRequest)
|
||||
|
@ -3,9 +3,8 @@ using Grpc.Net.Client;
|
||||
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
|
||||
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using static CatalogApi.Catalog;
|
||||
@ -25,21 +24,26 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
|
||||
public async Task<CatalogItem> GetCatalogItemAsync(int id)
|
||||
{
|
||||
_httpClient.BaseAddress = new Uri(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id));
|
||||
|
||||
return await GrpcCallerService.CallService(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id), async httpClient =>
|
||||
{
|
||||
var client = GrpcClient.Create<CatalogClient>(_httpClient);
|
||||
var request = new CatalogItemRequest { Id = id };
|
||||
var response = await client.GetItemByIdAsync(request);
|
||||
|
||||
return MapToCatalogItemResponse(response);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CatalogItem>> GetCatalogItemsAsync(IEnumerable<int> ids)
|
||||
{
|
||||
var stringContent = await _httpClient.GetStringAsync(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemsById(ids));
|
||||
var catalogItems = JsonConvert.DeserializeObject<CatalogItem[]>(stringContent);
|
||||
|
||||
return catalogItems;
|
||||
return await GrpcCallerService.CallService(_urls.GrpcCatalog, async httpClient =>
|
||||
{
|
||||
var client = GrpcClient.Create<CatalogClient>(httpClient);
|
||||
var request = new CatalogItemsRequest { Ids = string.Join(",", ids), PageIndex = 1, PageSize = 10 };
|
||||
var response = await client.GetItemsByIdsAsync(request);
|
||||
return response.Data.Select(this.MapToCatalogItemResponse);
|
||||
});
|
||||
}
|
||||
|
||||
private CatalogItem MapToCatalogItemResponse(CatalogItemResponse catalogItemResponse)
|
||||
|
@ -0,0 +1,73 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using Grpc.Core;
|
||||
using Serilog;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
{
|
||||
public static class GrpcCallerService
|
||||
{
|
||||
public static async Task<TResponse> CallService<TResponse>(string urlGrpc, Func<HttpClient, Task<TResponse>> func)
|
||||
{
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
||||
|
||||
using var httpClientHandler = new HttpClientHandler
|
||||
{
|
||||
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
|
||||
};
|
||||
|
||||
using var httpClient = new HttpClient(httpClientHandler)
|
||||
{
|
||||
BaseAddress = new Uri(urlGrpc)
|
||||
};
|
||||
|
||||
Log.Information("Creating grpc client base address urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress} ", urlGrpc, httpClient.BaseAddress);
|
||||
|
||||
try
|
||||
{
|
||||
return await func(httpClient);
|
||||
}
|
||||
catch (RpcException e)
|
||||
{
|
||||
Log.Error($"Error calling via grpc: {e.Status} - {e.Message}");
|
||||
}
|
||||
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", false);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public static async Task CallService(string urlGrpc, Func<HttpClient, Task> func)
|
||||
{
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
||||
|
||||
using var httpClientHandler = new HttpClientHandler
|
||||
{
|
||||
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
|
||||
};
|
||||
|
||||
using var httpClient = new HttpClient(httpClientHandler)
|
||||
{
|
||||
BaseAddress = new Uri(urlGrpc)
|
||||
};
|
||||
|
||||
Log.Debug("Creating grpc client base address {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
||||
|
||||
try
|
||||
{
|
||||
await func(httpClient);
|
||||
}
|
||||
catch (RpcException e)
|
||||
{
|
||||
Log.Error($"Error calling via grpc: {e.Status} - {e.Message}");
|
||||
}
|
||||
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", false);
|
||||
}
|
||||
}
|
||||
}
|
@ -3,12 +3,10 @@ using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
|
||||
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using GrpcOrdering;
|
||||
using Grpc.Core;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
{
|
||||
@ -27,43 +25,21 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
|
||||
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
|
||||
{
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
||||
|
||||
using (var httpClientHandler = new HttpClientHandler())
|
||||
return await GrpcCallerService.CallService(_urls.GrpcOrdering, async httpClient =>
|
||||
{
|
||||
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
||||
using (var httpClient = new HttpClient(httpClientHandler))
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(_urls.GrpcOrdering);
|
||||
|
||||
_logger.LogDebug(" Creating grpc client for ordering {@httpClient.BaseAddress}", httpClient.BaseAddress);
|
||||
|
||||
var client = GrpcClient.Create<OrderingGrpc.OrderingGrpcClient>(httpClient);
|
||||
|
||||
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
var command = MapToOrderDraftCommand(basketData);
|
||||
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
|
||||
|
||||
_logger.LogDebug(" grpc response: {@response}", response);
|
||||
|
||||
return MapToResponse(response);
|
||||
}
|
||||
catch (RpcException e)
|
||||
{
|
||||
_logger.LogError($"Error calling via grpc to ordering: {e.Status} - {e.Message}");
|
||||
}
|
||||
}
|
||||
return MapToResponse(response, basketData);
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft)
|
||||
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)
|
||||
{
|
||||
if (orderDraft == null)
|
||||
{
|
||||
@ -72,6 +48,7 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
|
||||
var data = new OrderData
|
||||
{
|
||||
Buyer = basketData.BuyerId,
|
||||
Total = (decimal)orderDraft.Total,
|
||||
};
|
||||
|
||||
|
@ -1,9 +1,4 @@
|
||||
{
|
||||
"Kestrel": {
|
||||
"EndpointDefaults": {
|
||||
"Protocols": "Http2"
|
||||
}
|
||||
},
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"Debug": {
|
||||
|
@ -5,6 +5,7 @@
|
||||
"orders": "http://localhost:55102",
|
||||
"identity": "http://localhost:55105",
|
||||
"grpcBasket": "http://localhost:5580",
|
||||
"grpcCatalog": "http://localhost:81",
|
||||
"grpcOrdering": "http://localhost:5581"
|
||||
},
|
||||
"IdentityUrlExternal": "http://localhost:5105",
|
||||
|
@ -28,13 +28,15 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
||||
|
||||
public async Task<CatalogItem> GetCatalogItemAsync(int id)
|
||||
{
|
||||
_httpClient.BaseAddress = new Uri(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id));
|
||||
|
||||
var client = GrpcClient.Create<CatalogClient>(_httpClient);
|
||||
return await GrpcCallerService.CallService(_urls.GrpcCatalog, async httpClient =>
|
||||
{
|
||||
var client = GrpcClient.Create<CatalogClient>(httpClient);
|
||||
var request = new CatalogItemRequest { Id = id };
|
||||
_logger.LogInformation("grpc client created, request = {@request}", request);
|
||||
var response = await client.GetItemByIdAsync(request);
|
||||
|
||||
_logger.LogInformation("grpc response {@response}", response);
|
||||
return MapToCatalogItemResponse(response);
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CatalogItem>> GetCatalogItemsAsync(IEnumerable<int> ids)
|
||||
|
Loading…
x
Reference in New Issue
Block a user