Built in support for multiple response payloads
Json
Example
Complete example
public record QueryDto
{
[MapFromPath("Id")] public long Id { get; set; }
}
public record Query(Id Id);
public class ApiEndpoint : QueryWebApiEndpoint.Request<QueryDto, Query>.Response<BlogDto, Blog>
{
private readonly IBlogStorageBroker _storageBroker;
public ApiEndpoint(IBlogStorageBroker storageBroker)
{
_storageBroker = storageBroker;
}
public override Task<Result<Blog>> ExecuteAsync(Query query, CancellationToken cancellationToken) =>
_storageBroker.GetByIdAsync(query.Id);
}
Async Enumerable
Example
.ResponseAsyncEnumerable<ApiEndpoint, BlogDto, Blog>
Complete example
public class ApiEndpoint : QueryWebApiEndpoint.NoRequest.ResponseAsyncEnumerable<ApiEndpoint, BlogDto, Blog>
{
private readonly IBlogStorageBroker _storageBroker;
public ApiEndpoint(IBlogStorageBroker storageBroker)
{
_storageBroker = storageBroker;
}
public override Task<Result<ResponseAsyncEnumerable<Blog>>> ExecuteAsync(CancellationToken cancellationToken)
{
return _storageBroker.GetAsync()
.MapAsync(xs => new ResponseAsyncEnumerable<Blog>(AsyncEnumerable(xs)));
async IAsyncEnumerable<Blog> AsyncEnumerable(IEnumerable<Blog> dtos)
{
await Task.Yield();
foreach (var dto in dtos)
{
yield return dto;
}
}
}
}
Bytes
Example
.ResponseBytes<ApiEndpoint>
Complete example
public class ApiEndpoint : QueryWebApiEndpoint.NoRequest.ResponseBytes<ApiEndpoint>
{
public override Task<Result<ResponseBytes>> ExecuteAsync(CancellationToken cancellationToken) =>
new ResponseBytes(File.ReadAllBytes("./Data/hello-world.txt"), "hello-world-bytes").ToResultOkAsync();
}
Support for Range header
Enumerable / Data Collection
Example
.ResponseDataCollection<ApiEndpoint, BlogDto, Blog>
Complete example
public class ApiEndpoint : QueryWebApiEndpoint.NoRequest.ResponseDataCollection<ApiEndpoint, BlogDto, Blog>
{
private readonly IBlogStorageBroker _storageBroker;
public ApiEndpoint(IBlogStorageBroker storageBroker)
{
_storageBroker = storageBroker;
}
public override Task<Result<ResponseDataCollection<Blog>>> ExecuteAsync(CancellationToken cancellationToken) =>
_storageBroker.GetAsync()
.ToResponseDataCollectionAsync();
}
Empty JSON
Example
.ResponseEmptyJson<ApiEndpoint>
Complete example
public class ApiEndpoint : QueryWebApiEndpoint.NoRequest.ResponseEmptyJson<ApiEndpoint>
{
public override Task<Result<ResponseEmptyJson>> ExecuteAsync(CancellationToken cancellationToken) =>
new ResponseEmptyJson().ToResultOkAsync();
}
File stream
Example
.ResponseFileStream<ApiEndpoint>
Complete example
public class ApiEndpoint : QueryWebApiEndpoint.NoRequest.ResponseFileStream<ApiEndpoint>
{
public override Task<Result<ResponseFileStream>> ExecuteAsync(CancellationToken cancellationToken) =>
new ResponseFileStream(new FileInfo("./Data/hello-world.txt")).ToResultOkAsync();
}
Support for Range header
Stream
Example
.ResponseStream<ApiEndpoint>
Complete example
public class ApiEndpoint : QueryWebApiEndpoint.NoRequest.ResponseStream<ApiEndpoint>
{
public override Task<Result<ResponseStream>> ExecuteAsync(CancellationToken cancellationToken) =>
new ResponseStream(new FileInfo("./Data/hello-world.txt").OpenRead(), "hello-world-stream").ToResultOkAsync();
}
Support for Range header