Skip to content

Commit

Permalink
updated readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
VeritasSoftware committed Jun 27, 2024
1 parent b7b6412 commit 4024927
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions Docs/README_Customizations.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Customizations

## Customize HttpClient

You may want to customize your calls to the back end Apis.

You can customize the default **HttpClient** used by all the routes, to hit the backend Api.
Expand All @@ -16,7 +18,6 @@ Also, the library provides hooks to

* Customize the default HttpClient which each route uses to hit the backend Api.
* Use your own **HttpClient** for each route.
* Use your own custom implementation to hit the backend Api.

For eg.

Expand All @@ -42,37 +43,68 @@ These hooks are implemented in your Gateway API project (eg. WeatherService belo
HttpClient = () => new HttpClient()
};
}
}
```

Wire up the WeatherService for injection.

```C#
services.AddTransient<IWeatherService, WeatherService>();
```

Then, they are hooked up to **routes** in the **Api Orchestrator**.

```C#
public static class ApiOrchestration
{
public static void Create(IApiOrchestrator orchestrator, IApplicationBuilder app)
{
var serviceProvider = app.ApplicationServices;

var weatherService = serviceProvider.GetService<IWeatherService>();

var weatherApiClientConfig = weatherService.GetClientConfig();

orchestrator.AddApi("weatherservice", "http://localhost:63969/")
//Get using customize default HttpClient or your own custom HttpClient
.AddRoute("types", GatewayVerb.GET, new RouteInfo { Path = "weatherforecast/types", ResponseType = typeof(string[]), HttpClientConfig = weatherApiClientConfig }); }
}
```

## Custom implementation

* Use your own custom implementation to hit the backend Api.

Create a Service (like WeatherService).

Add a method (like GetForecast) with the same method signature.
See **HttpRequest** [here](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httprequest?view=aspnetcore-6.0).

In the method, put your custom implementation.

```C#
public class WeatherService : IWeatherService
{
/// <summary>
/// If you want to completely customize your backend Api call, you can do this
/// </summary>
/// <param name="apiInfo">The api info</param>
/// <param name="request">The gateway's incoming request</param>
/// <returns></returns>
/// <returns>The object to be returned</returns>
public async Task<object> GetForecast(ApiInfo apiInfo, HttpRequest request)
{
//Create your own implementation to hit the backend.
using (var client = new HttpClient())
{
var response = await client.GetAsync($"{apiInfo.BaseUrl}weatherforecast/forecast");

response.EnsureSuccessStatusCode();

return JsonConvert.DeserializeObject<WeatherForecast[]>(await response.Content.ReadAsStringAsync());
}
}
}
```

See **HttpRequest** [here](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httprequest?view=aspnetcore-6.0).

Wire up the WeatherService for injection.

```C#
services.AddTransient<IWeatherService, WeatherService>();
```

Then, they are hooked up to **routes** in the **Api Orchestrator**.
Create a Route in the **ApiOrchestrator** as shown below:

```C#
public static class ApiOrchestration
Expand All @@ -83,18 +115,13 @@ Then, they are hooked up to **routes** in the **Api Orchestrator**.

var weatherService = serviceProvider.GetService<IWeatherService>();

var weatherApiClientConfig = weatherService.GetClientConfig();

orchestrator.AddApi("weatherservice", "http://localhost:63969/")
//Get using customize default HttpClient or your own custom HttpClient
.AddRoute("types", GatewayVerb.GET, new RouteInfo { Path = "weatherforecast/types", ResponseType = typeof(string[]), HttpClientConfig = weatherApiClientConfig })
//Get using custom implementation
.AddRoute("forecast-custom", GatewayVerb.GET, weatherService.GetForecast);
}
}
```


## Request aggregation

Your Api Gateway gets one incoming request.
Expand Down

0 comments on commit 4024927

Please sign in to comment.