Skip to content

Commit 016089f

Browse files
author
ericuss
committed
Partial checkin
1 parent 9b1c690 commit 016089f

9 files changed

Lines changed: 163 additions & 43 deletions

File tree

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/BasketService.cs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using grpc;
2-
using Grpc.Net.Client;
1+
using Grpc.Net.Client;
32
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
43
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models;
54
using Microsoft.Extensions.Logging;
@@ -8,7 +7,8 @@
87
using System.Linq;
98
using System.Net.Http;
109
using System.Threading.Tasks;
11-
using static grpc.Basket;
10+
using GrpcBasket;
11+
using Grpc.Core;
1212

1313
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
1414
{
@@ -27,49 +27,57 @@ public BasketService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger
2727

2828
public async Task<BasketData> GetById(string id)
2929
{
30+
3031
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetById @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
3132

3233
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
34+
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
3335

34-
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Http2UnencryptedSupport disable @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
35-
36-
_httpClient.BaseAddress = new Uri("http://localhost:5001");
37-
38-
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {_httpClient.BaseAddress} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", _httpClient.BaseAddress);
36+
using (var httpClientHandler = new HttpClientHandler())
37+
{
38+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
39+
using (var httpClient = new HttpClient(httpClientHandler))
40+
{
41+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Http2UnencryptedSupport disable @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
3942

40-
var client = GrpcClient.Create<BasketClient>(_httpClient);
43+
httpClient.BaseAddress = new Uri("http://localhost:5580");
4144

42-
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ client create @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
45+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {httpClient.BaseAddress} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ " + httpClient.BaseAddress, httpClient.BaseAddress);
4346

44-
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
47+
var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
4548

46-
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ call grpc server @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
49+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ client create @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
4750

48-
_logger.LogInformation("############## DATA: {@a}", response.Buyerid);
51+
try
52+
{
4953

50-
//if (streaming.IsCompleted)
51-
//{
52-
// _logger.LogInformation("############## DATA: {@a}", streaming.GetResult());
53-
//}
54-
//var streaming = client.GetBasketById(new BasketRequest { Id = id });
54+
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
55+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ call grpc server @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
5556

57+
_logger.LogInformation("############## DATA: {@a}", response.Buyerid);
58+
_logger.LogInformation("############## DATA:response {@response}", response);
5659

57-
//var status = streaming.GetStatus();
60+
return MapToBasketData(response);
61+
}
62+
catch (RpcException e)
63+
{
64+
_logger.LogError($"Error calling via grpc: {e.Status} - {e.Message}");
65+
}
66+
}
67+
}
5868

59-
//if (status.StatusCode == Grpc.Core.StatusCode.OK)
60-
//{
61-
// return null;
62-
//}
69+
return null; // temp
70+
// var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id));
71+
// var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject<BasketData>(data) : null;
6372

64-
return null;
65-
//return MapToBasketData(response.ResponseStream);
73+
// return basket;
6674
}
6775

6876
public async Task UpdateAsync(BasketData currentBasket)
6977
{
7078
_httpClient.BaseAddress = new Uri(_urls.Basket + UrlsConfig.BasketOperations.UpdateBasket());
7179

72-
var client = GrpcClient.Create<BasketClient>(_httpClient);
80+
var client = GrpcClient.Create<Basket.BasketClient>(_httpClient);
7381
var request = MapToCustomerBasketRequest(currentBasket);
7482

7583
await client.UpdateBasketAsync(request);

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
7474

7575
app.UseCors("CorsPolicy");
7676
app.UseHttpsRedirection();
77+
78+
app.UseSwagger().UseSwaggerUI(c =>
79+
{
80+
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
81+
82+
c.OAuthClientId("mobileshoppingaggswaggerui");
83+
c.OAuthClientSecret(string.Empty);
84+
c.OAuthRealm(string.Empty);
85+
c.OAuthAppName("Purchase BFF Swagger UI");
86+
});
87+
7788
app.UseRouting();
7889
app.UseAuthentication();
7990
app.UseAuthorization();
@@ -91,16 +102,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
91102
Predicate = r => r.Name.Contains("self")
92103
});
93104
});
94-
95-
app.UseSwagger().UseSwaggerUI(c =>
96-
{
97-
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
98-
99-
c.OAuthClientId("mobileshoppingaggswaggerui");
100-
c.OAuthClientSecret(string.Empty);
101-
c.OAuthRealm(string.Empty);
102-
c.OAuthAppName("Purchase BFF Swagger UI");
103-
});
104105
}
105106
}
106107

@@ -130,8 +131,11 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
130131
{
131132
Implicit = new OpenApiOAuthFlow()
132133
{
133-
AuthorizationUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
134-
TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
134+
// AuthorizationUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/authorize"),
135+
// TokenUrl = new Uri($"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token"),
136+
137+
AuthorizationUrl = new Uri($"http://localhost:5105/connect/authorize"),
138+
TokenUrl = new Uri($"http://localhost:5105/connect/token"),
135139
Scopes = new Dictionary<string, string>()
136140
{
137141
{ "mobileshoppingagg", "Shopping Aggregator for Mobile Clients" }

src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Net;
99
using System.Threading.Tasks;
10+
using Serilog;
1011

1112
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers
1213
{
@@ -30,13 +31,18 @@ public BasketController(ICatalogService catalogService, IBasketService basketSer
3031
[ProducesResponseType(typeof(BasketData), (int)HttpStatusCode.OK)]
3132
public async Task<ActionResult<BasketData>> UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
3233
{
34+
Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ UpdateAllBasketAsync @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
35+
3336
if (data.Items == null || !data.Items.Any())
3437
{
3538
return BadRequest("Need to pass at least one basket line");
3639
}
3740

3841
// Retrieve the current basket
42+
Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetByIdAsync @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
43+
3944
var basket = await _basket.GetByIdAsync(data.BuyerId) ?? new BasketData(data.BuyerId);
45+
Log.Information("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ basket @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
4046

4147
var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));
4248

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
syntax = "proto3";
2+
3+
option csharp_namespace = "GrpcBasket";
4+
5+
package BasketApi;
6+
7+
service Basket {
8+
rpc GetBasketById(BasketRequest) returns (CustomerBasketResponse);
9+
rpc UpdateBasket(CustomerBasketRequest) returns (CustomerBasketResponse);
10+
}
11+
12+
message BasketRequest {
13+
string id = 1;
14+
}
15+
16+
message CustomerBasketRequest {
17+
string buyerid = 1;
18+
repeated BasketItemResponse items = 2;
19+
}
20+
21+
message CustomerBasketResponse {
22+
string buyerid = 1;
23+
repeated BasketItemResponse items = 2;
24+
}
25+
26+
message BasketItemResponse {
27+
string id = 1;
28+
string productid = 2;
29+
string productname = 3;
30+
double unitprice = 4;
31+
double oldunitprice = 5;
32+
int32 quantity = 6;
33+
string pictureurl = 7;
34+
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Services/BasketService.cs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,66 @@ public BasketService(HttpClient httpClient,ILogger<BasketService> logger, IOptio
2323

2424
public async Task<BasketData> GetByIdAsync(string id)
2525
{
26-
var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id));
27-
var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject<BasketData>(data) : null;
2826

29-
return basket;
27+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ GetById @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
28+
29+
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
30+
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
31+
using (var httpClientHandler = new HttpClientHandler())
32+
{
33+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
34+
using (var httpClient = new HttpClient(httpClientHandler))
35+
{
36+
37+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Http2UnencryptedSupport disable @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
38+
39+
httpClient.BaseAddress = new Uri("http://localhost:5580");
40+
41+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ {httpClient.BaseAddress} @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", httpClient.BaseAddress);
42+
43+
var client = GrpcClient.Create<BasketClient>(httpClient);
44+
45+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ client create @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
46+
47+
try{
48+
49+
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
50+
_logger.LogInformation("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ call grpc server @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
51+
52+
_logger.LogInformation("############## DATA: {@a}", response.Buyerid);
53+
_logger.LogInformation("############## DATA:response {@response}", response);
54+
}
55+
catch (RpcException e)
56+
{
57+
Console.WriteLine($"Error calling via grpc: {e.Status} - {e.Message}");
58+
_logger.logError($"Error calling via grpc: {e.Status} - {e.Message}");
59+
60+
}
61+
62+
63+
//if (streaming.IsCompleted)
64+
//{
65+
// _logger.LogInformation("############## DATA: {@a}", streaming.GetResult());
66+
//}
67+
//var streaming = client.GetBasketById(new BasketRequest { Id = id });
68+
69+
70+
//var status = streaming.GetStatus();
71+
72+
//if (status.StatusCode == Grpc.Core.StatusCode.OK)
73+
//{
74+
// return null;
75+
//}
76+
77+
return response;
78+
79+
}
80+
}
81+
82+
// var data = await _apiClient.GetStringAsync(_urls.Basket + UrlsConfig.BasketOperations.GetItemById(id));
83+
// var basket = !string.IsNullOrEmpty(data) ? JsonConvert.DeserializeObject<BasketData>(data) : null;
84+
85+
// return basket;
3086
}
3187

3288
public async Task UpdateAsync(BasketData currentBasket)

src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
9494
{
9595
c.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Purchase BFF V1");
9696
//c.ConfigureOAuth2("Microsoft.eShopOnContainers.Web.Shopping.HttpAggregatorwaggerui", "", "", "Purchase BFF Swagger UI");
97+
c.OAuthClientId("webshoppingaggswaggerui");
98+
c.OAuthAppName("web shopping bff Swagger UI");
9799
});
98100
}
99101
}
@@ -157,7 +159,8 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
157159
TokenUrl = $"{configuration.GetValue<string>("IdentityUrlExternal")}/connect/token",
158160
Scopes = new Dictionary<string, string>()
159161
{
160-
{ "webshoppingagg", "Shopping Aggregator for Web Clients" }
162+
{ "webshoppingagg", "Shopping Aggregator for Web Clients" },
163+
{ "basket", "basket api" }
161164
}
162165
});
163166

src/ApiGateways/Web.Bff.Shopping/aggregator/Web.Shopping.HttpAggregator.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="2.2.0" />
5656
</ItemGroup>
5757

58+
<!-- grpc -->
59+
<ItemGroup>
60+
<PackageReference Include="Google.Protobuf" Version="$(Google_Protobuf)" />
61+
<PackageReference Include="Grpc.Net.Client" Version="$(Grpc_Net_Client)" />
62+
<PackageReference Include="Grpc.Tools" Version="$(Grpc_Tools)" PrivateAssets="All" />
63+
</ItemGroup>
64+
5865
<ItemGroup>
5966
<ProjectReference Include="..\..\..\BuildingBlocks\Devspaces.Support\Devspaces.Support.csproj" />
6067
</ItemGroup>

src/Services/Identity/Identity.API/Configuration/Config.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ public static IEnumerable<Client> GetClients(Dictionary<string, string> clientsU
284284

285285
AllowedScopes =
286286
{
287-
"webshoppingagg"
287+
"webshoppingagg",
288+
"basket"
288289
}
289290
},
290291
new Client

src/_build/dependencies.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
<PropertyGroup Label="Package Versions">
1919
<Grpc_AspNetCore_Server>0.1.22-pre3</Grpc_AspNetCore_Server>
20+
<Grpc_Net_Client>0.1.22-pre3</Grpc_Net_Client>
2021
<Google_Protobuf>3.9.1</Google_Protobuf>
2122
<Grpc_Tools>2.23.0</Grpc_Tools>
2223
<Grpc_AspNetCore_Server_ClientFactory>0.1.22-pre2</Grpc_AspNetCore_Server_ClientFactory>

0 commit comments

Comments
 (0)