HTTP requests are basically a must in every app. We didn’t want people to have to generate their API client, contracts, and then have to global
add the boilerplate mediator contract and subsequent request handler, so… we found some very convenient ways to build this in.
Let’s start with our mediator request contract - Notice that we have some attributes & interfaces that are specific to HTTP requests
Next, let’s create our configuration for the base URI. This assumes you use Microsoft.Extensions.Configuration JSON, but any other configuration provider with the same structure will work.
Now - let’s make the request with mediator
Decorating the HTTP Request
A common scenario is having to refresh an access token, add it to the next HTTP request, and maybe add some additional header while you’re there. Shiny Mediator allows you to register
a different type of middleware of HTTP Requests called IHttpRequestDecorator.
Here’s an example that already exists in Shiny.Mediator.Maui that adds some headers to the request.
Decorators apply to all HTTP requests
Generating All The Contracts and Responses
Some WebAPIs can have a large API surface with many routes, parameters, and types. Generating OpenAPI files is pretty standard these days with things like NSwag, Swashbuckle, & Refitter. Shiny Mediator can help with this as well. You can generate all the contracts and responses from an OpenAPI file or URI to an OpenAPI document.
Simply edit your csproj file that has Shiny.Mediator installed and add the following to it.
If there is no OperationId defined for the OpenAPI method, we cannot generate a contract.
:::note INFO
If you’re setting the Uri attribute, you must still set the Include value to satisfy the ItemGroup. Also note, the use of Visible=“false”. This removes the registration of the msbuild variable from the IDE solution explorer.
There are scenarios we don’t yet support like discrimated types. We’re looking at this for a future implementation.
:::