diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/BasketService.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/BasketService.cs index 657e3cd54..44e13dd91 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/BasketService.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/BasketService.cs @@ -1,5 +1,4 @@ -using grpc; -using Grpc.Net.Client; +using Grpc.Net.Client; using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config; using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models; using Microsoft.Extensions.Logging; @@ -8,7 +7,8 @@ using System; using System.Linq; using System.Net.Http; using System.Threading.Tasks; -using static grpc.Basket; +using GrpcBasket; +using Grpc.Core; namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services { @@ -27,49 +27,57 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services public async Task GetById(string id) { + _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetById @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true); - _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Http2UnencryptedSupport disable @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); - - _httpClient.BaseAddress = new Uri("http://localhost:5001"); - - _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {_httpClient.BaseAddress} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", _httpClient.BaseAddress); + using (var httpClientHandler = new HttpClientHandler()) + { + httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; + using (var httpClient = new HttpClient(httpClientHandler)) + { + _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Http2UnencryptedSupport disable @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); - var client = GrpcClient.Create(_httpClient); + httpClient.BaseAddress = new Uri("http://localhost:5580"); - _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ client create @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {httpClient.BaseAddress} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " + httpClient.BaseAddress, httpClient.BaseAddress); - var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id }); + var client = GrpcClient.Create(httpClient); - _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ call grpc server @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + _logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ client create @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); - _logger.LogInformation("############## DATA: {@a}", response.Buyerid); + try + { - //if (streaming.IsCompleted) - //{ - // _logger.LogInformation("############## DATA: {@a}", streaming.GetResult()); - //} - //var streaming = client.GetBasketById(new BasketRequest { Id = id }); + 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); - //var status = streaming.GetStatus(); + return MapToBasketData(response); + } + catch (RpcException e) + { + _logger.LogError($"Error calling via grpc: {e.Status} - {e.Message}"); + } + } + } - //if (status.StatusCode == Grpc.Core.StatusCode.OK) - //{ - // return null; - //} + return null; // temp + // var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id)); + // var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject(data) : null; - return null; - //return MapToBasketData(response.ResponseStream); + // return basket; } public async Task UpdateAsync(BasketData currentBasket) { _httpClient.BaseAddress = new Uri(_urls.Basket + UrlsConfig.BasketOperations.UpdateBasket()); - var client = GrpcClient.Create(_httpClient); + var client = GrpcClient.Create(_httpClient); var request = MapToCustomerBasketRequest(currentBasket); await client.UpdateBasketAsync(request); diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs index 2a9d0b341..8d7f7dbbe 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs @@ -74,6 +74,17 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator app.UseCors("CorsPolicy"); app.UseHttpsRedirection(); + + app.UseSwagger().UseSwaggerUI(c => + { + c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1"); + + c.OAuthClientId("mobileshoppingaggswaggerui"); + c.OAuthClientSecret(string.Empty); + c.OAuthRealm(string.Empty); + c.OAuthAppName("Purchase BFF Swagger UI"); + }); + app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); @@ -91,16 +102,6 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator Predicate = r => r.Name.Contains("self") }); }); - - app.UseSwagger().UseSwaggerUI(c => - { - c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1"); - - c.OAuthClientId("mobileshoppingaggswaggerui"); - c.OAuthClientSecret(string.Empty); - c.OAuthRealm(string.Empty); - c.OAuthAppName("Purchase BFF Swagger UI"); - }); } } @@ -130,8 +131,11 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator { Implicit = new OpenApiOAuthFlow() { - AuthorizationUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/authorize"), - TokenUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/token"), + // AuthorizationUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/authorize"), + // TokenUrl = new Uri($"{configuration.GetValue("IdentityUrlExternal")}/connect/token"), + + AuthorizationUrl = new Uri($"http://localhost:5105/connect/authorize"), + TokenUrl = new Uri($"http://localhost:5105/connect/token"), Scopes = new Dictionary() { { "mobileshoppingagg", "Shopping Aggregator for Mobile Clients" } diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs index 851368af1..2fefb9d8a 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; +using Serilog; namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers { @@ -30,13 +31,18 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers [ProducesResponseType(typeof(BasketData), (int)HttpStatusCode.OK)] public async Task> UpdateAllBasketAsync([FromBody] UpdateBasketRequest data) { + Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ UpdateAllBasketAsync @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + if (data.Items == null || !data.Items.Any()) { return BadRequest("Need to pass at least one basket line"); } // Retrieve the current basket + Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetByIdAsync @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); + var basket = await _basket.GetByIdAsync(data.BuyerId) ?? new BasketData(data.BuyerId); + Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ basket @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId)); diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Protos/basket.api b/src/ApiGateways/Web.Bff.Shopping/aggregator/Protos/basket.api new file mode 100644 index 000000000..1fc22bc99 --- /dev/null +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Protos/basket.api @@ -0,0 +1,34 @@ +syntax = "proto3"; + +option csharp_namespace = "GrpcBasket"; + +package BasketApi; + +service Basket { + rpc GetBasketById(BasketRequest) returns (CustomerBasketResponse); + rpc UpdateBasket(CustomerBasketRequest) returns (CustomerBasketResponse); +} + +message BasketRequest { + string id = 1; +} + +message CustomerBasketRequest { + string buyerid = 1; + repeated BasketItemResponse items = 2; +} + +message CustomerBasketResponse { + string buyerid = 1; + repeated BasketItemResponse items = 2; +} + +message BasketItemResponse { + string id = 1; + string productid = 2; + string productname = 3; + double unitprice = 4; + double oldunitprice = 5; + int32 quantity = 6; + string pictureurl = 7; +} \ No newline at end of file diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/BasketService.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/BasketService.cs index d0fb5c008..92b96dec5 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/BasketService.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/BasketService.cs @@ -23,10 +23,66 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services public async Task GetByIdAsync(string id) { - var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id)); - var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject(data) : null; - return basket; + _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(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(data) : null; + + // return basket; } public async Task UpdateAsync(BasketData currentBasket) diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs index 9b144db48..7c18f10ec 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs @@ -94,6 +94,8 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator { c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1"); //c.ConfigureOAuth2("Microsoft.eShopOnContainers.Web.Shopping.HttpAggregatorwaggerui", "", "", "Purchase BFF Swagger UI"); + c.OAuthClientId("webshoppingaggswaggerui"); + c.OAuthAppName("web shopping bff Swagger UI"); }); } } @@ -157,7 +159,8 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator TokenUrl = $"{configuration.GetValue("IdentityUrlExternal")}/connect/token", Scopes = new Dictionary() { - { "webshoppingagg", "Shopping Aggregator for Web Clients" } + { "webshoppingagg", "Shopping Aggregator for Web Clients" }, + { "basket", "basket api" } } }); diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj b/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj index ef610d572..ed51b4860 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj @@ -55,6 +55,13 @@ + + + + + + + diff --git a/src/Services/Identity/Identity.API/Configuration/Config.cs b/src/Services/Identity/Identity.API/Configuration/Config.cs index dcb3a8c7a..04a9aa043 100644 --- a/src/Services/Identity/Identity.API/Configuration/Config.cs +++ b/src/Services/Identity/Identity.API/Configuration/Config.cs @@ -284,7 +284,8 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Configuration AllowedScopes = { - "webshoppingagg" + "webshoppingagg", + "basket" } }, new Client diff --git a/src/_build/dependencies.props b/src/_build/dependencies.props index 1905ccd41..c16619251 100644 --- a/src/_build/dependencies.props +++ b/src/_build/dependencies.props @@ -17,6 +17,7 @@ 0.1.22-pre3 + 0.1.22-pre3 3.9.1 2.23.0 0.1.22-pre2