Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP Head support #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@ public IDispatcherEndpointsBuilder Delete<T>(string path, Func<T, HttpContext, T
return this;
}

public IDispatcherEndpointsBuilder Head(string path, Func<HttpContext, Task> context = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies)
{
_builder.Head(path, context, endpoint, auth, roles, policies);

return this;
}

public IDispatcherEndpointsBuilder Head<T>(string path,
Func<T, HttpContext, Task> beforeDispatch = null,
Func<T, bool, HttpContext, Task> afterDispatch = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies) where T : class, IQuery<bool>
{
_builder.Head<T>(path, async (query, ctx) =>
{
if (beforeDispatch is not null)
{
await beforeDispatch(query, ctx);
}

var dispatcher = ctx.RequestServices.GetRequiredService<IQueryDispatcher>();
var result = await dispatcher.QueryAsync<T, bool>(query);
if (afterDispatch is null)
{
ctx.Response.StatusCode = result ? 200 : 404;
return;
}

await afterDispatch(query, result, ctx);
}, endpoint, auth, roles, policies);

return this;
}

private static async Task BuildCommandContext<T>(T command, HttpContext context,
Func<T, HttpContext, Task> beforeDispatch = null,
Func<T, HttpContext, Task> afterDispatch = null) where T : class, ICommand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ IDispatcherEndpointsBuilder Delete<T>(string path, Func<T, HttpContext, Task> be
bool auth = false, string roles = null,
params string[] policies)
where T : class, ICommand;

IDispatcherEndpointsBuilder Head(string path, Func<HttpContext, Task> context = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies);

IDispatcherEndpointsBuilder Head<T>(string path,
Func<T, HttpContext, Task> beforeDispatch = null,
Func<T, bool, HttpContext, Task> afterDispatch = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies) where T : class, IQuery<bool>;
}
25 changes: 25 additions & 0 deletions src/Convey.WebApi/src/Convey.WebApi/EndpointsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,31 @@ public IEndpointsBuilder Delete<T>(string path, Func<T, HttpContext, Task> conte
return this;
}

public IEndpointsBuilder Head(string path, Func<HttpContext, Task> context = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies)
{
var builder = _routeBuilder.MapMethods(path, new[] { "HEAD" }, ctx => context?.Invoke(ctx) ?? Task.CompletedTask);
endpoint?.Invoke(builder);
ApplyAuthRolesAndPolicies(builder, auth, roles, policies);
AddEndpointDefinition(HttpMethods.Head, path);

return this;
}

public IEndpointsBuilder Head<T>(string path, Func<T, HttpContext, Task> context = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies)
where T : class
{
var builder = _routeBuilder.MapMethods(path, new[] { "HEAD" }, ctx => BuildQueryContext(ctx, context));
endpoint?.Invoke(builder);
ApplyAuthRolesAndPolicies(builder, auth, roles, policies);
AddEndpointDefinition<T>(HttpMethods.Head, path);

return this;
}

private static void ApplyAuthRolesAndPolicies(IEndpointConventionBuilder builder, bool auth, string roles,
params string[] policies)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Convey.WebApi/src/Convey.WebApi/IEndpointsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ IEndpointsBuilder Delete<T>(string path, Func<T, HttpContext, Task> context = nu
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies)
where T : class;

IEndpointsBuilder Head(string path, Func<HttpContext, Task> context = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies);

IEndpointsBuilder Head<T>(string path, Func<T, HttpContext, Task> context = null,
Action<IEndpointConventionBuilder> endpoint = null, bool auth = false, string roles = null,
params string[] policies)
where T : class;
}