Class EndpointRouteHelper

Namespace
Cratis.Arc.Http
Assembly
Cratis.Arc.Core.dll

Provides shared helpers for building endpoint routes and mapping result status codes.

public static class EndpointRouteHelper
Inheritance
EndpointRouteHelper
Inherited Members

Methods

BuildRouteUrl(ApiEndpointOptions, IEnumerable<string>, int, string, bool)

Builds a sanitized route URL from the given endpoint location and options.

public static string BuildRouteUrl(ApiEndpointOptions options, IEnumerable<string> location, int segmentsToSkip, string endpointName, bool includeNameInRoute)

Parameters

options ApiEndpointOptions

The ApiEndpointOptions.

location IEnumerable<string>

The namespace segments for the endpoint.

segmentsToSkip int

The number of leading namespace segments to skip.

endpointName string

The name of the endpoint (command type name or query name).

includeNameInRoute bool

Whether to include the endpoint name as the last route segment.

Returns

string

A sanitized, kebab-cased, lowercase route URL.

GetStatusCode(bool, bool, bool, bool)

Determines the HTTP status code for a result that has success, authorization, validation, and readiness state.

public static HttpStatusCode GetStatusCode(bool isSuccess, bool isAuthorized, bool isValid, bool isReady = true)

Parameters

isSuccess bool

Whether the operation succeeded.

isAuthorized bool

Whether the operation was authorized.

isValid bool

Whether the input was valid.

isReady bool

Whether the result is ready; defaults to true for results that have no readiness concept (for example command results).

Returns

HttpStatusCode

The appropriate HttpStatusCode.

Remarks

A not-ready result (an observable query that has not yet produced its first result) maps to 202 Accepted rather than a server error. Not-ready and error are mutually exclusive by construction — a not-ready result carries no exception — so mapping readiness before the error fall-through cannot hide a genuine failure.

GroupByNamespace<T>(IEnumerable<T>, Func<T, IEnumerable<string>>, int)

Groups endpoint descriptors by their namespace key (location segments joined by '.').

public static IReadOnlyDictionary<string, List<T>> GroupByNamespace<T>(IEnumerable<T> endpoints, Func<T, IEnumerable<string>> locationSelector, int segmentsToSkip)

Parameters

endpoints IEnumerable<T>

The endpoints to group.

locationSelector Func<T, IEnumerable<string>>

A function to get the location segments from an endpoint.

segmentsToSkip int

The number of leading namespace segments to skip.

Returns

IReadOnlyDictionary<string, List<T>>

A dictionary mapping namespace keys to lists of endpoints.

Type Parameters

T

The type of endpoint descriptor.

ShouldIncludeNameInRoute<T>(bool, IEnumerable<string>, IReadOnlyDictionary<string, List<T>>)

Determines whether the endpoint name should be included in the route, based on the configured option and whether there are conflicting endpoints in the same namespace.

public static bool ShouldIncludeNameInRoute<T>(bool includeNameOption, IEnumerable<string> location, IReadOnlyDictionary<string, List<T>> endpointsByNamespace)

Parameters

includeNameOption bool

The configured option for including the name (e.g. IncludeCommandNameInRoute).

location IEnumerable<string>

The namespace segments for the endpoint.

endpointsByNamespace IReadOnlyDictionary<string, List<T>>

The endpoints grouped by namespace key.

Returns

bool

True if the endpoint name should be included in the route.

Type Parameters

T

The type of endpoint descriptor (command handler, query performer, etc.).