Ocelot aggregate interceptor
See original GitHub issueNew Feature
Motivation for New Feature
Let say, I have 2 micro services, called as CustomerMicroService and OrderMicroService CustomerMicroService is in charge of managing customer information. OrderMicroService manages orders that a customer has made.
Let me talk about 2 api end-point from Customer micro-service and Order micro service first.
api/customer/search
- Request:
{
"pagination": {
"page": 1,
"records": 30
}
}
-> Get the first 30 customers from Customer micro service.
- Response:
{
"records": [
.. list of customers
]
"total": 30
}
-> Returns a list of customers with total number of customer that match the search condition.
api/order/search
- Request:
{
"customerIds": [1, 2, 3, 4, 5],
"status": ["pending", "active"]
}
-> Get list of order base on the previous loaded customer from the above api.
- Response:
{
"records": [
.. list of orders
]
"total": 300
}
-> Returns a list of orders that match the search condition.
Steps to Reproduce the Problem
Now, my API Gateway uses Ocelot to aggregate 2 api endpoint api/customer/search and api/order/search into one API for mobile & web to be consumed, let call it api/gateway/load-customer-orders/{customerId}
This is my ocelot.json
:
{
"Aggregates": [
{
"ReRouteKeys": [
"Customer",
"Order"
],
"UpstreamPathTemplate": "api/gateway/load-customer-orders/{customerId}",
"Aggregator": "LoadCustomerOrdersAggregator"
}
],
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/customer/search",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/customer/search",
"UpstreamHttpMethod": [ "POST" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 62744
}
],
"Key": "Customer"
},
{
"DownstreamPathTemplate": "/api/order/search",
"DownstreamScheme": "http",
"UpstreamPathTemplate": "/api/order/search",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 62724
}
],
"Key": "Order"
}
]
}
My LoadCustomerOrdersAggregator.cs
:
public class LoadCustomerOrdersAggregator : IDefinedAggregator
{
#region Methods
/// <summary>
/// Load customer orders.
/// </summary>
/// <param name="responses"></param>
/// <returns></returns>
public virtual async Task<DownstreamResponse> Aggregate(List<DownstreamResponse> responses)
{
throw new NotImplementedException();
}
#endregion
}
As far as I know Ocelot only support GET method.
It is Ok for me, for now, since I just pass {customerId}
into my aggregate api api/gateway/load-customer-order/{customerId}.
But it would be nice if I can get the information of Dictionary<string, DownStreamRequest>
in aggregate class to manually handle the data by my self.
The aggregate class implemenation should be :
public virtual async Task<DownstreamResponse> Aggregate(Dictionary<string, DownstreamRequest> requests, List<DownstreamResponse> responses)
{
// Base on the request dictionary to manually handle HttpClient call.
throw new NotImplementedException();
}
Specifications
- Version: 13.0.0
- Platform: Windows
- Subsystem:
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Thank you for you reply. I’m waiting for that release, coz my project needs that function 😃
Any updates ?