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;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|
|
|
|
{
|
|
|
|
|
public class BasketService : IBasketService
|
|
|
|
|
{
|
2018-05-18 14:02:18 +02:00
|
|
|
|
private readonly HttpClient _apiClient;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
private readonly ILogger<BasketService> _logger;
|
|
|
|
|
private readonly UrlsConfig _urls;
|
|
|
|
|
|
2018-05-18 14:02:18 +02:00
|
|
|
|
public BasketService(HttpClient httpClient,ILogger<BasketService> logger, IOptions<UrlsConfig> config)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
|
|
|
|
_apiClient = httpClient;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_urls = config.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<BasketData> GetByIdAsync(string id)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2019-08-27 09:40:31 +02:00
|
|
|
|
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetById @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
|
|
|
|
|
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
|
|
|
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
|
|
|
|
using (var httpClientHandler = new HttpClientHandler())
|
|
|
|
|
{
|
|
|
|
|
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
|
|
|
|
using (var httpClient = new HttpClient(httpClientHandler))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Http2UnencryptedSupport disable @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
|
|
|
|
|
|
httpClient.BaseAddress = new Uri("http://localhost:5580");
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {httpClient.BaseAddress} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", httpClient.BaseAddress);
|
|
|
|
|
|
|
|
|
|
var client = GrpcClient.Create<BasketClient>(httpClient);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ client create @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
|
|
|
|
|
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ call grpc server @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("############## DATA: {@a}", response.Buyerid);
|
|
|
|
|
_logger.LogInformation("############## DATA:response {@response}", response);
|
|
|
|
|
}
|
|
|
|
|
catch (RpcException e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
|
|
|
_logger.logError($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//if (streaming.IsCompleted)
|
|
|
|
|
//{
|
|
|
|
|
// _logger.LogInformation("############## DATA: {@a}", streaming.GetResult());
|
|
|
|
|
//}
|
|
|
|
|
//var streaming = client.GetBasketById(new BasketRequest { Id = id });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//var status = streaming.GetStatus();
|
|
|
|
|
|
|
|
|
|
//if (status.StatusCode == Grpc.Core.StatusCode.OK)
|
|
|
|
|
//{
|
|
|
|
|
// return null;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id));
|
|
|
|
|
// var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject<BasketData>(data) : null;
|
|
|
|
|
|
|
|
|
|
// return basket;
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task UpdateAsync(BasketData currentBasket)
|
2018-02-27 14:32:25 +01:00
|
|
|
|
{
|
2018-05-18 14:02:18 +02:00
|
|
|
|
var basketContent = new StringContent(JsonConvert.SerializeObject(currentBasket), System.Text.Encoding.UTF8, "application/json");
|
2018-02-27 14:32:25 +01:00
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
await _apiClient.PostAsync(_urls.Basket + UrlsConfig.BasketOperations.UpdateBasket(), basketContent);
|
2018-02-27 14:32:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|