Skip to content

Commit d0a323c

Browse files
committed
- Add Response Headers to IApiResult
1 parent 9b67e1d commit d0a323c

File tree

9 files changed

+50
-15
lines changed

9 files changed

+50
-15
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,19 @@ The purpose of a api class is to execute the web api with api engine to fetch th
8080

8181
As mentioned previously, You can configure an api in `Parent` or `Child` (nested) mode in a hierarchical graph.
8282

83-
To define a `parent` or `nested` api you need to implement from `WebApi<TResult>` class where `TResult` is the result that will be returned from executing the api. It is an implementation of `IApiResult` type.
84-
Upon creating the api class. You need to provide below `GetUrl()` implementation.
85-
* Implement the `GetUrl(IRequestContext context, IApiResult parentApiResult)` method to return the constructed endpoint based on given parameters of the method.
86-
* For Parent Api, Only `IRequestContext` context parameter is passed to GetUrl() method to resolve the Url endpoint.
87-
* For Nested Api, api result parameter (ie. `IApiResult` parentApiResult) from the parent api is additionally passed in to GetUrl() method along with IRequestContext context parameter.
88-
89-
Please Note: `IApiResult` parentApiResult parameter is null for apis configured in parent mode.
83+
To create `Web Api` defined as `parent` or `nested` api, you need to implement from `WebApi<TResult>` class,
84+
where `TResult` is `IApiResult` interface (or `ApiResult` base class) implementation and is the result that will be returned from executing the api.
9085

91-
`Important:` The api `endpoint` needs to be resolved before executing the api with `ApiEngine`.
86+
Upon creating the web api class, you need to provide `GetUrl()` method implementation.
87+
* Implement the `GetUrl(IRequestContext context, IApiResult parentApiResult)` method to return the constructed endpoint based on given parameters of the method.
88+
* For `Parent Api`, only `IRequestContext` context parameter is passed to GetUrl() method to resolve the Url endpoint.
89+
* For `Nested Api`, api result parameter (ie. `IApiResult` parentApiResult) from the parent api is additionally passed in to GetUrl() method along with IRequestContext context parameter.
90+
* Optionally, override `GetHeaders()` method to provide any list of `request headers` for the api.
91+
* `IApiResult` implementation exposes `Headers` property for any `response headers` received as part of the api response.
92+
93+
`Important:`
94+
- The api `endpoint` needs to be resolved before executing the api with `ApiEngine`.
95+
- `IApiResult` parentApiResult parameter is null for apis configured in parent mode.
9296

9397
Examples.
9498

src/ApiAggregator/ApiResult.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace ApiAggregator.Net
2+
{
3+
public abstract class ApiResult : IApiResult
4+
{
5+
public ApiResult()
6+
{
7+
Headers = [];
8+
}
9+
10+
public List<KeyValuePair<string, string>> Headers { get; set; }
11+
}
12+
}

src/ApiAggregator/CollectionResult.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ public class CollectionResult<T> : List<T>, IApiResult
44
{
55
public CollectionResult(IEnumerable<T> list) : base(list)
66
{
7+
Headers = [];
78
}
9+
10+
public List<KeyValuePair<string, string>> Headers { get; set; }
811
}
912
}

src/ApiAggregator/IApiResult.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ namespace ApiAggregator.Net
22
{
33
public interface IApiResult
44
{
5+
List<KeyValuePair<string, string>> Headers { get; }
56
}
67
}

src/ApiAggregator/WebApi.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,22 @@ public virtual async Task<IApiResult> Run(IHttpClientFactory httpClientFactory,
128128
{
129129
var resultType = typeof(CollectionResult<>);
130130
var collectionType = resultType.MakeGenericType(typeArgs);
131-
var collectionResult = Activator.CreateInstance(collectionType, arrObject);
132-
return (TResult)collectionResult;
131+
var collectionResult = (TResult)Activator.CreateInstance(collectionType, arrObject);
132+
133+
SetResponseHeaders(result, collectionResult);
134+
135+
return collectionResult;
133136
}
134137
}
135138
else
136139
{
137140
var obj = JsonSerializer.Deserialize(raw, typeof(TResult));
138141
if (obj != null)
139-
return (TResult)obj;
142+
{
143+
var resObj = (TResult)obj;
144+
SetResponseHeaders(result, resObj);
145+
return resObj;
146+
}
140147
}
141148
}
142149
catch (TaskCanceledException ex)
@@ -157,6 +164,14 @@ public virtual async Task<IApiResult> Run(IHttpClientFactory httpClientFactory,
157164
}
158165

159166
return null;
167+
168+
static void SetResponseHeaders(HttpResponseMessage response, TResult? result)
169+
{
170+
if (response.Headers != null && result != null)
171+
foreach (var header in response.Headers)
172+
result?.Headers?.Add(new KeyValuePair<string, string>(header.Key,
173+
header.Value != null && header.Value.Any() ? header.Value.ElementAt(0) : null));
174+
}
160175
}
161176
}
162177
}

tests/ApiAggregator.Tests/ApiAggregate/ApiResults/CommunicationResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ApiAggregator.Tests.ApiAggregate.ApiResults
44
{
55
[CacheResult]
6-
public class CommunicationResult : IApiResult
6+
public class CommunicationResult : ApiResult
77
{
88
public int Id { get; set; }
99
public string Telephone { get; set; }

tests/ApiAggregator.Tests/ApiAggregate/ApiResults/CustomerResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ApiAggregator.Tests.ApiAggregate.ApiResults
44
{
5-
public class CustomerResult : IApiResult
5+
public class CustomerResult : ApiResult
66
{
77
public int Id { get; set; }
88
public string Code { get; set; }

tests/ApiAggregator.Tests/ApiAggregate/ApiResults/OrderItemResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ApiAggregator.Tests.ApiAggregate.ApiResults
44
{
5-
public class OrderItemResult : IApiResult
5+
public class OrderItemResult : ApiResult
66
{
77
public int OrderId { get; set; }
88
public int ItemId { get; set; }

tests/ApiAggregator.Tests/ApiAggregate/ApiResults/OrderResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ApiAggregator.Tests.ApiAggregate.ApiResults
44
{
5-
public class OrderResult : IApiResult
5+
public class OrderResult : ApiResult
66
{
77
public int OrderId { get; set; }
88
public string OrderNo { get; set; }

0 commit comments

Comments
 (0)