Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Request Aggregation #1069

Closed
stefanvoicila opened this issue Dec 2, 2019 · 3 comments · Fixed by #1826
Closed

Custom Request Aggregation #1069

stefanvoicila opened this issue Dec 2, 2019 · 3 comments · Fixed by #1826
Assignees
Labels
Aggregation Ocelot feature: Request Aggregation documentation Needs a documentation update Jan'24 January 2024 release merged Issue has been merged to dev and is waiting for the next release
Milestone

Comments

@stefanvoicila
Copy link

Expected Behavior / New Feature

I am trying to aggregate two requests in a way in which a value from the first response is used in the second response as a parameter.

I want to return the results from both requests in a way that resembles something like this:
"Response": [
FirstResponse_FirstItem:{
"Key": 1,
SecondResponse_Item1: {
"ForeignKey": 1,
"Value" : "aaaaa"
}
}
]

Actual Behavior / Motivation for New Feature

I've been trying to set up an aggregator to do this implementing IDefinedAggregator (as described in the docs) but I had no success. Also, I am struggling to find examples of aggregators in order to have any starting points or ideas on how to do this.

Steps to Reproduce the Problem

  1. config:
    {
    "ReRoutes": [
    {
    "DownstreamPathTemplate": "/api/{everything}",
    "UpstreamPathTemplate": "/api/logistics/{everything}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
    {
    "Host": "localhost",
    "Port": 5001
    }
    ],
    "SwaggerKey": "Logistics"
    },
    {
    "DownstreamPathTemplate": "/api/{everything}",
    "UpstreamPathTemplate": "/api/identity/{everything}",
    "UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ],
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
    {
    "Host": "localhost",
    "Port": 50026
    }
    ],
    "SwaggerKey": "Identity"
    },
    {
    "DownstreamPathTemplate": "/api/Buildings",
    "UpstreamPathTemplate": "/buildings",
    "UpstreamHttpMethod": [ "Get" ],
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
    {
    "Host": "localhost",
    "Port": 5001
    }
    ],
    "Key": "buildings"
    },
    {
    "DownstreamPathTemplate": "/api/Device/{Id}",
    "UpstreamPathTemplate": "/devices/{Id}",
    "UpstreamHttpMethod": [ "Get" ],
    "DownstreamScheme": "https",
    "DownstreamHostAndPorts": [
    {
    "Host": "localhost",
    "Port": 5001
    }
    ],
    "Key": "devices"
    }
    ],
    "SwaggerEndPoints": [
    {
    "Key": "Logistics",
    "Config": [
    {
    "Name": "Logistics API",
    "Version": "v1",
    "Url": "https://localhost:5001/swagger/v1/swagger.json"
    }
    ]
    },
    {
    "Key": "Identity",
    "Config": [
    {
    "Name": "Identity API",
    "Version": "v1",
    "Url": "https://localhost:50026/swagger/v1/swagger.json"
    }
    ]
    }
    ],
    "Aggregates": [
    {
    "ReRouteKeys": [
    "devices",
    "buildings"
    ],
    "UpstreamPathTemplate": "/both",
    "Aggregator": "TestAggregator"
    }
    ],
    "GlobalConfiguration": {
    "BaseUrl": "https://localhost"
    }
    }

  2. Startup:
    services.AddOcelot(Configuration).AddSingletonDefinedAggregator();

  3. Aggregator:
    public class TestAggregator : IDefinedAggregator
    {
    public async Task Aggregate(List responses)
    {
    ??
    }
    }

Specifications

  • Version: 13.9.0-alpha0115
  • Platform: Windows 10
  • Subsystem: .NET Core 3.0
@jr-araujo
Copy link

Hi,

I think that you are missing something in your Ocelot configuration.

In the picture below is showing the Ocelot version which has configured in my project:
image

Here is my Program.cs configuration. And here is your missing point, take a look:
image

And here is my ocelot.json:
image

I hope all of this can help you to solve this problem.

@neetra
Copy link

neetra commented Apr 24, 2020

@stefanvoicila here is the sample.

@debayanmitra002-git
Copy link

Is this issue resolved? I am fairly new to Ocelot and what I could understand if we are aggregating two service calls and the key is same, then it can be handled via the reroute stuff.
However, if I have a requirement where I hit the first service with the parameter I am passing from client but want to use a different value from the response of first downstream service as a parameter of next downstream service, how can I do that even with IDefinedAggregator. Any example will help

@raman-m raman-m self-assigned this Mar 1, 2024
@raman-m raman-m added documentation Needs a documentation update Aggregation Ocelot feature: Request Aggregation Jan'24 January 2024 release labels Mar 1, 2024
@raman-m raman-m added this to the January'24 milestone Mar 1, 2024
raman-m added a commit that referenced this issue Mar 1, 2024
…1826)

* Cleanup of Multiplexing middleware, avoiding creating copies of the Http context if only one downstream route.

* updating comments

* preparing rebase, it's a hack.

* fixing test case "Copy_User_ToTarget",  method name has changed.

* adding some unit tests, checking that if 1 route, ProcessSingleRoute is called and no copies of context are made, if more than 2 Map is called, then more than 2 copies are created.

* Applying refactoring suggested.

* some code cleanup

* Some code cleanup in Aggregate Logic

* Cleanup in Aggregate Tests, verifying multiplexer cleanup

* Finalizing unit test, with complex keys.

* some code refactoring and applying suggestions from reviews

* Update requestaggregation.rst

Recover docs

* updating docs

* Update requestaggregation.rst

* Update requestaggregation.rst

* Code review by @raman-m

* Inherit `Steps` functionality instead of private aggregation

---------

Co-authored-by: Raman Maksimchuk <dotnet044@gmail.com>
@raman-m raman-m added the merged Issue has been merged to dev and is waiting for the next release label Mar 1, 2024
@raman-m raman-m mentioned this issue Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Aggregation Ocelot feature: Request Aggregation documentation Needs a documentation update Jan'24 January 2024 release merged Issue has been merged to dev and is waiting for the next release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants