Here's my implementation of the accepted answer below with a slight difference:
- Wrapped the fields/classes in
#region
to make it collapsible - Implement only the
Route
part as I still want to keep some values at the method. - Avoid string concatenation as I want the values to be readable.
using Operation = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiOperationAttribute;
using Parameter = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiParameterAttribute;
using RequestBody = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiRequestBodyAttribute;
using ResponseBody = Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes.OpenApiResponseWithBodyAttribute;
namespace AzureFunctionsREST.API.Functions
{
public class ReporterFunction : BaseFunction
{
#region Metadata
private const string DefaultContentType = "application/json";
private static class Route
{
public const string ParameterName = "reporterId";
public const string Base = "reporter";
public const string WithParameter = "reporter/{reporterId:required}";
}
#endregion
private readonly IReporterRepository _reporterRepository;
public ReporterFunction(IReporterRepository reporterRepository)
{
_reporterRepository = reporterRepository;
}
[Function("ReporterList")]
[Operation(tags: Route.Base, Summary = "Retrieve all reporters")]
[ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter[]), Description = "All reporters")]
public async Task<HttpResponseData> List([HttpTrigger(AuthorizationLevel.Function, "get", Route = Route.Base)] HttpRequestData req,
FunctionContext executionContext)
{
// ...
}
[Function("ReporterGet")]
[Operation(tags: Route.Base, Summary = "Retrieve reporter")]
[Parameter(name: Route.ParameterName, In = ParameterLocation.Path, Required = true, Type = typeof(string))]
[ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Matched reporter")]
public async Task<HttpResponseData> Get([HttpTrigger(AuthorizationLevel.Function, "get", Route = Route.WithParameter)] HttpRequestData req,
FunctionContext executionContext)
{
// ...
}
[Function("ReporterPost")]
[Operation(tags: Route.Base, Summary = "Create a new reporter")]
[RequestBody(contentType: DefaultContentType, bodyType: typeof(ReporterRequest))]
[ResponseBody(statusCode: HttpStatusCode.Created, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Created reporter")]
public async Task<HttpResponseData> Post([HttpTrigger(AuthorizationLevel.Function, "post", Route = Route.Base)] HttpRequestData req,
FunctionContext executionContext)
{
// ...
}
[Function("ReporterPut")]
[Operation(tags: Route.Base, Summary = "Update a reporter")]
[Parameter(name: Route.ParameterName, In = ParameterLocation.Path, Required = true, Type = typeof(string))]
[RequestBody(contentType: DefaultContentType, bodyType: typeof(ReporterRequest))]
[ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Updated reporter")]
public async Task<HttpResponseData> Put([HttpTrigger(AuthorizationLevel.Function, "put", Route = Route.WithParameter)] HttpRequestData req,
FunctionContext executionContext)
{
// ...
}
[Function("ReporterDelete")]
[Operation(tags: Route.Base, Summary = "Delete a reporter")]
[Parameter(name: Route.ParameterName, In = ParameterLocation.Path, Required = true, Type = typeof(string))]
[ResponseBody(statusCode: HttpStatusCode.OK, contentType: DefaultContentType, bodyType: typeof(Reporter), Description = "Deleted reporter")]
public async Task<HttpResponseData> Delete([HttpTrigger(AuthorizationLevel.Function, "delete", Route = Route.WithParameter)] HttpRequestData req,
FunctionContext executionContext)
{
// ...
}
}
}