Skip to content

Commit

Permalink
- Update Request/Response Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Nov 6, 2024
1 parent d0a323c commit 8187efb
Show file tree
Hide file tree
Showing 70 changed files with 409 additions and 178 deletions.
2 changes: 1 addition & 1 deletion ApiAggregator.sln → ApiAggregator.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ VisualStudioVersion = 17.9.34723.18
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4338FF70-3C81-4370-ACFB-00E14545BA99}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiAggregator.Net", "src\ApiAggregator\ApiAggregator.Net.csproj", "{8250784C-5415-47C2-9FE4-9E54FA4672B6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiAggregator", "src\ApiAggregator\ApiAggregator.csproj", "{8250784C-5415-47C2-9FE4-9E54FA4672B6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{31E7A02C-167D-46FB-A90A-F3995FD5682D}"
EndProject
Expand Down
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# <img src="https://github.com/CodeShayk/ApiAggregator/blob/master/Images/ninja-icon-16.png" alt="ninja" style="width:30px;"/> ApiAggregator.Net v1.0
# <img src="https://github.com/CodeShayk/ApiAggregator/blob/master/Images/ninja-icon-16.png" alt="ninja" style="width:30px;"/> ApiAggregator v1.0
[![NuGet version](https://badge.fury.io/nu/ApiAggregator.svg)](https://badge.fury.io/nu/ApiAggregator) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/CodeShayk/ApiAggregator/blob/master/LICENSE.md)
[![Master-Build](https://github.com/CodeShayk/ApiAggregator/actions/workflows/Master-Build.yml/badge.svg)](https://github.com/CodeShayk/ApiAggregator/actions/workflows/Master-Build.yml)
[![GitHub Release](https://img.shields.io/github/v/release/CodeShayk/ApiAggregator?logo=github&sort=semver)](https://github.com/CodeShayk/ApiAggregator/releases/latest)
Expand Down Expand Up @@ -83,12 +83,13 @@ As mentioned previously, You can configure an api in `Parent` or `Child` (nested
To create `Web Api` defined as `parent` or `nested` api, you need to implement from `WebApi<TResult>` class,
where `TResult` is `IApiResult` interface (or `ApiResult` base class) implementation and is the result that will be returned from executing the api.

Upon creating the web api class, you need to provide `GetUrl()` method implementation.
* Implement the `GetUrl(IRequestContext context, IApiResult parentApiResult)` method to return the constructed endpoint based on given parameters of the method.
Upon creating the web api class, you need to provide `GetUrl()` method implementation to return `Uri` instance.
* Implement the `GetUrl(IRequestContext context, IApiResult parentApiResult)` method to return the constructed endpoint using given parameters of the method.
* For `Parent Api`, only `IRequestContext` context parameter is passed to GetUrl() method to resolve the Url endpoint.
* 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.
* Optionally, override `GetHeaders()` method to provide any list of `request headers` for the api.
* `IApiResult` implementation exposes `Headers` property for any `response headers` received as part of the api response.
* Optionally, override `GetRequestHeaders()` method to provide a dictionary of `outgoing headers` for the api request.
* Optionally, override `GetResponseHeaders()` method to provide any list of `incoming headers` from the api response.
* `IApiResult` implementation exposes `Headers` property for subscribed `response headers` received as part of the api response.

`Important:`
- The api `endpoint` needs to be resolved before executing the api with `ApiEngine`.
Expand All @@ -104,12 +105,28 @@ public class CustomerApi : WebApi<CustomerResult>
{
}
// Override to construct the api endpoint.
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
// Executes as root or level 1 api. parentApiResult should be null.
var customerContext = (CustomerContext)context;
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId));
return new Uri(string.Format(Endpoints.Customer, customerContext.CustomerId));
}
// Override to pass custom outgoing headers with the api request.
protected override IDictionary<string, string> GetRequestHeaders()
{
return new Dictionary<string, string>
{
{ "x-meta-branch-code", "Geneva" }
};
}
// Override to get custom incoming headers with the api response.
protected override IEnumerable<string> GetResponseHeaders()
{
return new[] { "x-meta-branch-code" };
}
}
```
Expand All @@ -125,7 +142,7 @@ internal class CommunicationApi : WebApi<CommunicationResult>
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
var customer = (CustomerResult)parentApiResult;
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id));
return new Uri(string.Format(Endpoints.Communication, customer.Id));
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/ApiAggregate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement the api aggregate with web apis and result transformers to map data to aggregated contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\..\Images\ninja-icon-16.png" Link="misc\ninja-icon-16.png">
<None Include="..\..\Images\ninja-icon-16.png" Link="Misc\ninja-icon-16.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\LICENSE" Link="misc\LICENSE">
<None Include="..\..\LICENSE" Link="Misc\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md" Link="misc\README.md">
<None Include="..\..\README.md" Link="Misc\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
Expand All @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="misc\" />
<Folder Include="Misc\" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/ApiAggregator/ApiComparer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
internal class ApiComparer : IEqualityComparer<IWebApi>
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/ApiList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
internal class ApiList : IApiList
{
Expand Down
6 changes: 3 additions & 3 deletions src/ApiAggregator/ApiResult.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public abstract class ApiResult : IApiResult
{
public ApiResult()
{
Headers = [];
Headers = new Dictionary<string, string>();
}

public List<KeyValuePair<string, string>> Headers { get; set; }
public IDictionary<string, string> Headers { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/ApiAggregator/CacheResultAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public class CacheResultAttribute : Attribute
{ }
Expand Down
6 changes: 3 additions & 3 deletions src/ApiAggregator/CollectionResult.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public class CollectionResult<T> : List<T>, IApiResult
{
public CollectionResult(IEnumerable<T> list) : base(list)
{
Headers = [];
Headers = new Dictionary<string, string>();
}

public List<KeyValuePair<string, string>> Headers { get; set; }
public IDictionary<string, string> Headers { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/ApiAggregator/CreateAggregate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
#region Helpers

Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/Helpers/ArrayUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net.Helpers
namespace ApiAggregator.Helpers
{
public static class ArrayUtil
{
Expand Down
11 changes: 11 additions & 0 deletions src/ApiAggregator/Helpers/Constraints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace ApiAggregator.Helpers
{
public static class Constraints
{
public static void NotNull<T>(this T value)
{
if (value == null)
throw new ArgumentNullException(typeof(T).Name);
}
}
}
2 changes: 1 addition & 1 deletion src/ApiAggregator/Helpers/EnumerableExtentions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace ApiAggregator.Net.Helpers
namespace ApiAggregator.Helpers
{
public static class EnumerableExtentions
{
Expand Down
11 changes: 11 additions & 0 deletions src/ApiAggregator/Helpers/JsonExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json;

namespace ApiAggregator.Helpers
{
public static class JsonExtensions
{
public static string? ToJson(this object value) => value != null ? JsonSerializer.Serialize(value) : null;

public static object? ToObject(this string value, Type type) => !string.IsNullOrEmpty(value) ? JsonSerializer.Deserialize(value, type) : null;
}
}
4 changes: 3 additions & 1 deletion src/ApiAggregator/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ApiAggregator.Net.Helpers
using System.Net.NetworkInformation;

namespace ApiAggregator.Helpers
{
public static class StringExtensions
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiAggregate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement to configure aggregate contract with api mappings.
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiAggregator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiAggregator<TContract> where TContract : IContract
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
internal interface IApiBuilder<T>
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiEngine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiEngine
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
internal interface IApiExecutor
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiList
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiNameMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiNameMatcher
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiParameter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiParameter
{
Expand Down
4 changes: 2 additions & 2 deletions src/ApiAggregator/IApiResult.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiResult
{
List<KeyValuePair<string, string>> Headers { get; }
IDictionary<string, string> Headers { get; internal set; }
}
}
2 changes: 1 addition & 1 deletion src/ApiAggregator/IApiResultCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IApiResultCache
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IContract.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement to create the aggregated contract.
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IContractBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IContractBuilder<out TContract> where TContract : IContract
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IPolymorphicResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IPolymorphicResult : IApiResult
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IRequestContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
public interface IRequestContext : IApiResultCache
{
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/IResultTransformer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement transformer to map data from supported api result to aggregated contract in context.
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/ITransformerContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement to set transform with request.
Expand Down
2 changes: 1 addition & 1 deletion src/ApiAggregator/ITransformerResultType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement to get supported Api result.
Expand Down
4 changes: 2 additions & 2 deletions src/ApiAggregator/IWebApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.Logging;

namespace ApiAggregator.Net
namespace ApiAggregator
{
/// <summary>
/// Implement IWebApi to fetch data using API.
Expand All @@ -13,7 +13,7 @@ public interface IWebApi

bool IsContextResolved();

Task<IApiResult> Run(IHttpClientFactory httpClientFactory, ILogger logger);
Task<IApiResult> Run(IHttpClientFactory httpClientFactory, ILogger logger = null);

Check warning on line 16 in src/ApiAggregator/IWebApi.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

/// <summary>
/// Implement to resolve api parameter.
Expand Down
18 changes: 8 additions & 10 deletions src/ApiAggregator/Impl/ApiAggregator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Data;
using ApiAggregator.Helpers;
using Microsoft.Extensions.Logging;

namespace ApiAggregator.Net.Impl
namespace ApiAggregator.Impl
{
internal class ApiAggregator<TContract> : IApiAggregator<TContract>
where TContract : IContract, new()
Expand All @@ -10,15 +12,6 @@ internal class ApiAggregator<TContract> : IApiAggregator<TContract>
private readonly IApiBuilder<TContract> apiBuilder;
private readonly IContractBuilder<TContract> contractBuilder;

//public ApiAggregator(
// ILogger<ApiAggregator<TContract>> logger,
// IApiAggregate<TContract> contract,
// IApiEngine apiEngine)
// : this(logger, new ApiBuilder<TContract>(contract, new StringContainsMatcher()),
// new ApiExecutor(apiEngine), new ContractBuilder<TContract>(contract))
//{
//}

public ApiAggregator(
ILogger<ApiAggregator<TContract>> logger,
IApiBuilder<TContract> apiBuilder,
Expand All @@ -29,13 +22,18 @@ public ApiAggregator(
this.apiBuilder = apiBuilder;
this.apiExecutor = apiExecutor;
this.contractBuilder = contractBuilder;

Constraints.NotNull(apiBuilder);
Constraints.NotNull(apiExecutor);
Constraints.NotNull(contractBuilder);
}

public TContract GetData(IRequestContext context)
{
// Build apis for the aggregated contract based on the included api names
var watch = System.Diagnostics.Stopwatch.StartNew();
var apis = apiBuilder.Build(context);

watch.Stop();
logger?.LogInformation("Api builder executed in " + watch.ElapsedMilliseconds + " ms");

Expand Down
Loading

0 comments on commit 8187efb

Please sign in to comment.