Skip to content

Commit fc35826

Browse files
Convert shipping composition handler to contract-less
1 parent 649b9b7 commit fc35826

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
lines changed

ASP.Net Core API Gateway - 01/Composition.Tests/When_calling_composition_gateway.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public async Task Get_composed_product_should_return_expected_values()
5454
HttpClient ClientProvider(string name) =>
5555
name switch
5656
{
57-
var val when val == typeof(Shipping.ViewModelComposition.ProductDetailsGetHandler).FullName => _shippingApiClient,
57+
var val when val == typeof(Shipping.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _shippingApiClient,
5858
var val when val == typeof(Warehouse.ViewModelComposition.CompositionHandlers.ProductDetailsCompositionHandler).FullName => _warehouseApiClient,
5959
var val when val == typeof(Sales.ViewModelComposition.ProductDetailsGetHandler).FullName => _salesApiClient,
6060
var val when val == typeof(Catalog.ViewModelComposition.ProductDetailsGetHandler).FullName => _catalogApiClient,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
using JsonUtils;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Mvc;
8+
using ServiceComposer.AspNetCore;
9+
10+
namespace Shipping.ViewModelComposition.CompositionHandlers;
11+
12+
public class ProductDetailsCompositionHandler(HttpClient client, IHttpContextAccessor httpContextAccessor)
13+
{
14+
[HttpGet("/products/details/{id}")]
15+
public async Task Get(string id)
16+
{
17+
var url = $"/api/shipping-options/product/{id}";
18+
var response = await client.GetAsync(url);
19+
20+
dynamic productShippingOptions = await response.Content.AsExpando();
21+
22+
var options = ((IEnumerable<dynamic>)productShippingOptions.Options)
23+
.Select(o => o.Option)
24+
.ToArray();
25+
26+
var vm = httpContextAccessor.HttpContext.Request.GetComposedResponseModel();
27+
vm.ProductShippingOptions = string.Join(", ", options);
28+
}
29+
}

ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ProductDetailsGetHandler.cs

Lines changed: 0 additions & 40 deletions
This file was deleted.

ASP.Net Core API Gateway - 01/Shipping.ViewModelComposition/ViewModelCompositionOptionsCustomization.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
55
using ServiceComposer.AspNetCore;
6+
using Shipping.ViewModelComposition.CompositionHandlers;
67

78
namespace Shipping.ViewModelComposition
89
{
910
public class ViewModelCompositionOptionsCustomization : IViewModelCompositionOptionsCustomization
1011
{
1112
public void Customize(ViewModelCompositionOptions options)
1213
{
13-
options.RegisterHttpClient<ProductDetailsGetHandler>((serviceProvider, httpClient) =>
14+
options.RegisterHttpClient<ProductDetailsCompositionHandler>((serviceProvider, httpClient) =>
1415
{
1516
var configuration = serviceProvider.GetService<IConfiguration>();
1617
var baseAddress = configuration?.GetSection("Shipping:BaseAddress").Value ?? "http://localhost:5004";

0 commit comments

Comments
 (0)