2018-05-18 14:02:18 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
|
|
|
|
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Newtonsoft.Json;
|
2018-05-18 14:02:18 +02:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-08-27 15:08:39 +02:00
|
|
|
|
using Grpc.Net.Client;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using GrpcBasket;
|
|
|
|
|
using Grpc.Core;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|
|
|
|
{
|
|
|
|
|
public class BasketService : IBasketService
|
|
|
|
|
{
|
2019-08-27 15:08:39 +02:00
|
|
|
|
private readonly HttpClient _httpClient;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
private readonly UrlsConfig _urls;
|
2019-08-27 15:08:39 +02:00
|
|
|
|
private readonly ILogger<BasketService> _logger;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
public BasketService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<BasketService> logger)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_httpClient = httpClient;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
_urls = config.Value;
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_logger = logger;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
public async Task<BasketData> GetById(string id)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-08-27 09:40:31 +02:00
|
|
|
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
|
|
|
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
2019-08-27 15:08:39 +02:00
|
|
|
|
|
|
|
|
|
using (var httpClientHandler = new HttpClientHandler())
|
2019-08-27 09:40:31 +02:00
|
|
|
|
{
|
|
|
|
|
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
|
|
|
|
using (var httpClient = new HttpClient(httpClientHandler))
|
|
|
|
|
{
|
2019-08-27 15:08:39 +02:00
|
|
|
|
//httpClient.BaseAddress = new Uri("http://10.0.75.1:5580");
|
|
|
|
|
httpClient.BaseAddress = new Uri(_urls.GrpcBasket);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_logger.LogDebug("Creating grpc client for basket {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_logger.LogDebug("grpc client created, request = {@id}", id);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_logger.LogDebug("grpc response {@response}", response);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
return MapToBasketData(response);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
}
|
|
|
|
|
catch (RpcException e)
|
|
|
|
|
{
|
2019-08-27 15:08:39 +02:00
|
|
|
|
_logger.LogError($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
public async Task UpdateAsync(BasketData currentBasket)
|
|
|
|
|
{
|
|
|
|
|
_httpClient.BaseAddress = new Uri(_urls.Basket + UrlsConfig.BasketOperations.UpdateBasket());
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var client = GrpcClient.Create<Basket.BasketClient>(_httpClient);
|
|
|
|
|
var request = MapToCustomerBasketRequest(currentBasket);
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
await client.UpdateBasketAsync(request);
|
|
|
|
|
}
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
private BasketData MapToBasketData(CustomerBasketResponse customerBasketRequest)
|
|
|
|
|
{
|
|
|
|
|
if (customerBasketRequest == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var map = new BasketData
|
|
|
|
|
{
|
|
|
|
|
BuyerId = customerBasketRequest.Buyerid
|
|
|
|
|
};
|
2019-08-27 09:40:31 +02:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
customerBasketRequest.Items.ToList().ForEach(item => map.Items.Add(new BasketDataItem
|
|
|
|
|
{
|
|
|
|
|
Id = item.Id,
|
|
|
|
|
OldUnitPrice = (decimal)item.Oldunitprice,
|
|
|
|
|
PictureUrl = item.Pictureurl,
|
|
|
|
|
ProductId = item.Productid,
|
|
|
|
|
ProductName = item.Productname,
|
|
|
|
|
Quantity = item.Quantity,
|
|
|
|
|
UnitPrice = (decimal)item.Unitprice
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return map;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
private CustomerBasketRequest MapToCustomerBasketRequest(BasketData basketData)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2019-08-27 15:08:39 +02:00
|
|
|
|
if (basketData == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2019-08-27 15:08:39 +02:00
|
|
|
|
var map = new CustomerBasketRequest
|
|
|
|
|
{
|
|
|
|
|
Buyerid = basketData.BuyerId
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
basketData.Items.ToList().ForEach(item => map.Items.Add(new BasketItemResponse
|
|
|
|
|
{
|
|
|
|
|
Id = item.Id,
|
|
|
|
|
Oldunitprice = (double)item.OldUnitPrice,
|
|
|
|
|
Pictureurl = item.PictureUrl,
|
|
|
|
|
Productid = item.ProductId,
|
|
|
|
|
Productname = item.ProductName,
|
|
|
|
|
Quantity = item.Quantity,
|
|
|
|
|
Unitprice = (double)item.UnitPrice
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return map;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|