Skip to content

Commit 92f23c9

Browse files
A little bit better API
1 parent d5f79b5 commit 92f23c9

File tree

9 files changed

+33
-15
lines changed

9 files changed

+33
-15
lines changed

src/ServiceComposer.AspNetCore.Tests/API/APIApprovals.Approve_API.verified.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,20 @@ namespace ServiceComposer.AspNetCore
6060
" removed in v3. Use attribute routing based composition, and CompositionEventHan" +
6161
"dler<TEvent>.", true)]
6262
public delegate System.Threading.Tasks.Task EventHandler<TEvent>(string requestId, [System.Runtime.CompilerServices.Dynamic] object viewModel, TEvent @event, Microsoft.AspNetCore.Routing.RouteData routeData, Microsoft.AspNetCore.Http.HttpRequest httpRequest);
63-
public abstract class Gatherer
63+
public abstract class Gatherer<T> : ServiceComposer.AspNetCore.IGatherer
64+
where T : class
6465
{
6566
protected Gatherer(string key) { }
6667
public string Key { get; }
67-
public abstract System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<object>> Gather(Microsoft.AspNetCore.Http.HttpContext context);
68+
public abstract System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<T>> Gather(Microsoft.AspNetCore.Http.HttpContext context);
6869
}
69-
public class HttpGatherer : ServiceComposer.AspNetCore.Gatherer
70+
public class HttpGatherer : ServiceComposer.AspNetCore.Gatherer<System.Text.Json.Nodes.JsonNode>
7071
{
7172
public HttpGatherer(string key, string destinationUrl) { }
7273
public System.Func<Microsoft.AspNetCore.Http.HttpRequest, string, string> DefaultDestinationUrlMapper { get; }
7374
public string DestinationUrl { get; }
7475
public System.Func<Microsoft.AspNetCore.Http.HttpRequest, string, string> DestinationUrlMapper { get; init; }
75-
public override System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<object>> Gather(Microsoft.AspNetCore.Http.HttpContext context) { }
76+
public override System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Text.Json.Nodes.JsonNode>> Gather(Microsoft.AspNetCore.Http.HttpContext context) { }
7677
protected virtual string MapDestinationUrl(Microsoft.AspNetCore.Http.HttpRequest request, string destination) { }
7778
protected virtual System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<System.Text.Json.Nodes.JsonNode>> TransformResponse(System.Net.Http.HttpResponseMessage responseMessage) { }
7879
}
@@ -117,6 +118,11 @@ namespace ServiceComposer.AspNetCore
117118
System.Threading.Tasks.Task Handle(Microsoft.AspNetCore.Http.HttpRequest request);
118119
}
119120
public interface IEndpointScopedViewModelFactory : ServiceComposer.AspNetCore.IViewModelFactory { }
121+
public interface IGatherer
122+
{
123+
string Key { get; }
124+
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<object>> Gather(Microsoft.AspNetCore.Http.HttpContext context);
125+
}
120126
[System.Obsolete("IHandleRequests is obsoleted and will be treated as an error starting v2 and remo" +
121127
"ved in v3. Use attribute routing based composition and ICompositionRequestsHandl" +
122128
"er.", true)]
@@ -188,7 +194,7 @@ namespace ServiceComposer.AspNetCore
188194
{
189195
public ScatterGatherOptions() { }
190196
public System.Type CustomAggregator { get; set; }
191-
public System.Collections.Generic.IList<ServiceComposer.AspNetCore.Gatherer> Gatherers { get; set; }
197+
public System.Collections.Generic.IList<ServiceComposer.AspNetCore.IGatherer> Gatherers { get; set; }
192198
}
193199
public static class ServiceCollectionExtensions
194200
{

src/ServiceComposer.AspNetCore.Tests/ScatterGather/Get_with_2_gatherers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ HttpClient ClientProvider(string name) =>
8585
{
8686
builder.MapScatterGather(template: "/samples", new ScatterGatherOptions
8787
{
88-
Gatherers = new List<Gatherer>
88+
Gatherers = new List<IGatherer>
8989
{
9090
new HttpGatherer(key: "ASamplesSource", destinationUrl: "/samples/ASamplesSource"),
9191
new HttpGatherer(key: "AnotherSamplesSource", destinationUrl: "/samples/AnotherSamplesSource")

src/ServiceComposer.AspNetCore.Tests/ScatterGather/When_using_query_string.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ HttpClient ClientProvider(string name) =>
8484
{
8585
builder.MapScatterGather(template: "/samples", new ScatterGatherOptions
8686
{
87-
Gatherers = new List<Gatherer>
87+
Gatherers = new List<IGatherer>
8888
{
8989
new HttpGatherer(key: "ASamplesSource", destinationUrl: "/samples/ASamplesSource"),
9090
new HttpGatherer(key: "AnotherSamplesSource", destinationUrl: "/samples/AnotherSamplesSource")
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Text.Json.Nodes;
34
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.Http;
56

67
namespace ServiceComposer.AspNetCore;
78

8-
public abstract class Gatherer
9+
public interface IGatherer
10+
{
11+
string Key { get; }
12+
Task<IEnumerable<object>> Gather(HttpContext context);
13+
}
14+
15+
public abstract class Gatherer<T> : IGatherer where T : class
916
{
1017
protected Gatherer(string key)
1118
{
@@ -14,5 +21,10 @@ protected Gatherer(string key)
1421

1522
public string Key { get; }
1623

17-
public abstract Task<IEnumerable<object>> Gather(HttpContext context);
24+
Task<IEnumerable<object>> IGatherer.Gather(HttpContext context)
25+
{
26+
return Gather(context).ContinueWith(t => (IEnumerable<object>)t.Result);
27+
}
28+
29+
public abstract Task<IEnumerable<T>> Gather(HttpContext context);
1830
}

src/ServiceComposer.AspNetCore/ScatterGather/HttpGatherer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace ServiceComposer.AspNetCore;
1010

11-
public class HttpGatherer : Gatherer
11+
public class HttpGatherer : Gatherer<JsonNode>
1212
{
1313
public HttpGatherer(string key, string destinationUrl)
1414
: base(key)
@@ -55,7 +55,7 @@ protected virtual async Task<IEnumerable<JsonNode>> TransformResponse(HttpRespon
5555
return nodes;
5656
}
5757

58-
public override async Task<IEnumerable<object>> Gather(HttpContext context)
58+
public override async Task<IEnumerable<JsonNode>> Gather(HttpContext context)
5959
{
6060
var factory = context.RequestServices.GetRequiredService<IHttpClientFactory>();
6161
var client = factory.CreateClient(Key);

src/ServiceComposer.AspNetCore/ScatterGather/ScatterGatherOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ internal IAggregator GetAggregator(HttpContext httpContext)
1818
return new DefaultAggregator();
1919
}
2020

21-
public IList<Gatherer> Gatherers { get; set; }
21+
public IList<IGatherer> Gatherers { get; set; }
2222
}

src/Snippets/ScatterGather/CustomizingDownstreamURLs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
1212
{
1313
app.UseEndpoints(builder => builder.MapScatterGather(template: "api/scatter-gather", new ScatterGatherOptions()
1414
{
15-
Gatherers = new List<Gatherer>
15+
Gatherers = new List<IGatherer>
1616
{
1717
new HttpGatherer("ASamplesSource", "https://a.web.server/api/samples/ASamplesSource")
1818
{

src/Snippets/ScatterGather/GatherMethodOverride.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CustomHttpGatherer : HttpGatherer
1414
{
1515
public CustomHttpGatherer(string key, string destination) : base(key, destination) { }
1616

17-
public override Task<IEnumerable<object>> Gather(HttpContext context)
17+
public override Task<IEnumerable<JsonNode>> Gather(HttpContext context)
1818
{
1919

2020
return base.Gather(context);

src/Snippets/ScatterGather/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
1414
app.UseRouting();
1515
app.UseEndpoints(builder => builder.MapScatterGather(template: "api/scatter-gather", new ScatterGatherOptions()
1616
{
17-
Gatherers = new List<Gatherer>
17+
Gatherers = new List<IGatherer>
1818
{
1919
new HttpGatherer(key: "ASamplesSource", destinationUrl: "https://a.web.server/api/samples/ASamplesSource"),
2020
new HttpGatherer(key: "AnotherSamplesSource", destinationUrl: "https://another.web.server/api/samples/AnotherSamplesSource")

0 commit comments

Comments
 (0)