How to add a new WebApiEndpoint¶
This involves two parts:
Create WebApiEndpoint¶
WebApiEndpoint separates out queries and commands.
Query maps to returning data without side-effects i.e. a HttpMethod.GET
Command maps to things that have side-effects i.e. a HttpMethod.Post | HttpMethod.Put | HttpMethod.Patch | HttpMethod.Delete
When you know which one you are creating, then you use the fluent API to allow you to specify and create a WebApiEndpoint.
Depending on the type of WebApiEndpoint you are creating, you may also have to create a mapper, the fluent API will show you what you need to create.
Query¶
Inherits from 'QueryWebApiEndpoint'. This a fluent API, that allows you to specify the request and response types.
Options available:
- NoRequest - no request dto
- Request<TDomain> - no request dto, but has request domain. Use mapper to extract data from 'HttpContext'.
- Request<TDto, TDomain> - request dto and request domain
Note what 'ApiEndpoint' is inheriting.
Query No Request¶
Query Request<TDomain>¶
Query Request<TDto, TDomain>¶
Command¶
Inherits from 'CommandWebApiEndpoint'. This a fluent API, that allows you to specify the request and response types.
Options available:
- RequestPlainText<TApiEndpoint> - plain text request
- Request<TDomain> - no request dto, but has request domain. Use mapper to extract data from 'HttpContext'.
- Request<TDto, TDomain> - request dto and request domain
Note what 'ApiEndpoint' is inheriting.
Command RequestPlainText<TApiEndpoint>¶
Command Request<TDomain>¶
Command Request<TDto, TDomain>¶
Mapper¶
If required you will need to specify the mapper type as well. See mappers for more information.
Mapper types are specified using the following:
CommandWebApiEndpoint.Request<CommandDto, Command>.Response<BlogDto, Blog>.Mapper<Mapper, BlogMapper>
Specify ApiEndpointDefinition¶
ApiEndpointDefinition's allow you to specify the WebApi (REST) route(s) that are connected to your WebApiEndpoint.
In order to connect it to WebApiEndpoints, you need to specify it like this:
This will then provide a fluent interface to set the WebApiEndpoint route configurations.
The next thing to do is to specify either 'Query' or 'Command'.
ApiEndpointDefinition - Query¶
The first thing to do it to specify the type of the WebApiEndpoint.
This give you access to the query specific route builder.
Now you can specify the route:
or route with parametersApiVersion¶
You can optionally specify a ApiVersion, if you don't choose one the default ApiVersion from configuration will be used:
Security¶
You can optionally secure the route.
Permission¶
Claim¶
Specify your own handler¶
var claimsRequired = new List<ClaimRecord>{new ClaimRecord(Authorization.ClaimType.Admin, Authorization.Claim.Admin)};
.RequireClaimAuthorization(authorizationHandlerContext => !claimsRequired.Except(authorizationHandlerContext.User.Claims.Select(x => new ClaimRecord(x.Type, x.Value))).Any());
Role¶
OpenApi¶
You can optionally specify OpenApi summary and description:
ApiEndpointDefinition - Command¶
The first thing to do it to specify the type of the WebApiEndpoint.
This give you access to the command specific route builder.
Now you can specify the route, but in order to do that you have to pick the HttpMethod (Post | Put | Patch | Delete) you want to use:
Post¶
or with parametersPut¶
or with parametersPatch¶
or with parametersDelete¶
or with parametersApiVersion¶
You can optionally specify a ApiVersion, if you don't choose one the default ApiVersion from configuration will be used:
Security¶
You can optionally secure the route.
Permission¶
Claim¶
Specify your own handler¶
var claimsRequired = new List<ClaimRecord>{new ClaimRecord(Authorization.ClaimType.Admin, Authorization.Claim.Admin)};
.RequireClaimAuthorization(authorizationHandlerContext => !claimsRequired.Except(authorizationHandlerContext.User.Claims.Select(x => new ClaimRecord(x.Type, x.Value))).Any());
Role¶
OpenApi¶
You can optionally specify OpenApi summary and description:
Complete example¶
public class ApiEndpointDefinition : IApiEndpointDefinition
{
public void Configure(ApiEndpointDefinitionBuilder definitionBuilder)
{
definitionBuilder.Web()
.Command<BlogCreate.ApiEndpoint>(builder => builder.Post("blog")
.RequireAuthorization()
.Version(WebApiEndpointVersions.V1_0).Summary("Create Blog").Description("Create Blog"))
.Command<BlogDelete.ApiEndpoint>(builder => builder.Delete("blog/{Id}", ("Id", typeof(long)))
.Version(WebApiEndpointVersions.V1_0).Summary("Delete Blog").Description("Delete Blog"))
.Query<BlogGet.ApiEndpoint>(builder => builder.Route("blog")
.Version(WebApiEndpointVersions.V1_0).Summary("Get Blogs").Description("Get Blogs"))
.Query<BlogGetById.ApiEndpoint>(builder => builder.Route("blog/{Id}", ("Id", typeof(long)))
.Version(WebApiEndpointVersions.V1_0).Summary("Get Blogs By Id").Description("Get Blogs By Id"))
.Command<BlogUpdate.ApiEndpoint>(builder => builder.Put("blog")
.Version(WebApiEndpointVersions.V1_0).Summary("Update Blog").Description("Update Blog"))
.Query<BlogGetAsyncEnumerable.ApiEndpoint>(builder => builder.Route("blog-async")
.Version(WebApiEndpointVersions.V1_0).Summary("Get Blogs").Description("Get Blogs"));
}
}