Skip to content

Commit

Permalink
Switch to file-local namespaces (#519)
Browse files Browse the repository at this point in the history
* Adjust namespaces to folder names

* Switch to file local namespaces
  • Loading branch information
Kaliumhexacyanoferrat authored Oct 11, 2024
1 parent 2547873 commit 9f87f7f
Show file tree
Hide file tree
Showing 380 changed files with 9,423 additions and 10,622 deletions.
25 changes: 11 additions & 14 deletions API/Content/Authentication/IUser.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
namespace GenHTTP.Api.Content.Authentication
namespace GenHTTP.Api.Content.Authentication;

/// <summary>
/// Information about an user that is associated with
/// the currently handled request.
/// </summary>
public interface IUser
{

/// <summary>
/// Information about an user that is associated with
/// the currently handled request.
/// The name of the user as it should be shown on
/// the UI (e.g. a rendered, themed page).
/// </summary>
public interface IUser
{

/// <summary>
/// The name of the user as it should be shown on
/// the UI (e.g. a rendered, themed page).
/// </summary>
string DisplayName { get; }

}
string DisplayName { get; }

}
73 changes: 35 additions & 38 deletions API/Content/Caching/ICache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,47 @@
using System.IO;
using System.Threading.Tasks;

namespace GenHTTP.Api.Content.Caching
namespace GenHTTP.Api.Content.Caching;

/// <summary>
/// Saves intermediate results for fast access.
/// </summary>
/// <typeparam name="T">The type of the results to be cached</typeparam>
public interface ICache<T>
{

/// <summary>
/// Saves intermediate results for fast access.
/// Fetches all entries that are stored in the
/// cache with the given key.
/// </summary>
/// <typeparam name="T">The type of the results to be cached</typeparam>
public interface ICache<T>
{

/// <summary>
/// Fetches all entries that are stored in the
/// cache with the given key.
/// </summary>
/// <param name="key">The key to look up</param>
/// <returns>The entries stored for this key</returns>
ValueTask<T[]> GetEntriesAsync(string key);

/// <summary>
/// Attempts to fetch a single entry with the given key
/// and variation.
/// </summary>
/// <param name="key">The key of the entry to be fetched</param>
/// <param name="variation">The variation to be fetched</param>
/// <returns>The requested entry, if any</returns>
ValueTask<T?> GetEntryAsync(string key, string variation);
/// <param name="key">The key to look up</param>
/// <returns>The entries stored for this key</returns>
ValueTask<T[]> GetEntriesAsync(string key);

/// <summary>
/// Stores the given entry in the cache.
/// </summary>
/// <param name="key">The key of the entry (should be file system compatible)</param>
/// <param name="variation">The variation specification of the entry</param>
/// <param name="entry">The entry to be stored (or to be deleted, if null)</param>
ValueTask StoreAsync(string key, string variation, T? entry);
/// <summary>
/// Attempts to fetch a single entry with the given key
/// and variation.
/// </summary>
/// <param name="key">The key of the entry to be fetched</param>
/// <param name="variation">The variation to be fetched</param>
/// <returns>The requested entry, if any</returns>
ValueTask<T?> GetEntryAsync(string key, string variation);

/// <summary>
/// Exposes the stream which can be written to to
/// update the specified entry.
/// </summary>
/// <param name="key">The key of the entry to be written (should be file system compatible)</param>
/// <param name="variation">The variation specification of the entry</param>
/// <param name="asyncWriter">A callback that allows to write the entry to the target stream</param>
ValueTask StoreDirectAsync(string key, string variation, Func<Stream, ValueTask> asyncWriter);
/// <summary>
/// Stores the given entry in the cache.
/// </summary>
/// <param name="key">The key of the entry (should be file system compatible)</param>
/// <param name="variation">The variation specification of the entry</param>
/// <param name="entry">The entry to be stored (or to be deleted, if null)</param>
ValueTask StoreAsync(string key, string variation, T? entry);

}
/// <summary>
/// Exposes the stream which can be written to to
/// update the specified entry.
/// </summary>
/// <param name="key">The key of the entry to be written (should be file system compatible)</param>
/// <param name="variation">The variation specification of the entry</param>
/// <param name="asyncWriter">A callback that allows to write the entry to the target stream</param>
ValueTask StoreDirectAsync(string key, string variation, Func<Stream, ValueTask> asyncWriter);

}
45 changes: 21 additions & 24 deletions API/Content/Concerns.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
using System;
using System.Collections.Generic;

namespace GenHTTP.Api.Content
namespace GenHTTP.Api.Content;

/// <summary>
/// Utility class to work with concerns.
/// </summary>
public static class Concerns
{

/// <summary>
/// Utility class to work with concerns.
/// Creates a handler chain to wrap the specified handler into the
/// specified concerns.
/// </summary>
public static class Concerns
/// <remarks>
/// Use this utility within the handler builders to add concerns
/// to the resulting handler instance. The last concern added
/// to the list of concerns will be the root handler returned by
/// this method.
/// </remarks>
/// <param name="parent">The parent handler of the chain</param>
/// <param name="concerns">The concerns that should be wrapped around the inner handler</param>
/// <param name="factory">The logic creating the actual handler to be chained</param>
/// <returns>The outermost handler or root of the chain</returns>
public static IHandler Chain(IHandler parent, IEnumerable<IConcernBuilder> concerns, Func<IHandler, IHandler> factory)
{

/// <summary>
/// Creates a handler chain to wrap the specified handler into the
/// specified concerns.
/// </summary>
/// <remarks>
/// Use this utility within the handler builders to add concerns
/// to the resulting handler instance. The last concern added
/// to the list of concerns will be the root handler returned by
/// this method.
/// </remarks>
/// <param name="parent">The parent handler of the chain</param>
/// <param name="concerns">The concerns that should be wrapped around the inner handler</param>
/// <param name="factory">The logic creating the actual handler to be chained</param>
/// <returns>The outermost handler or root of the chain</returns>
public static IHandler Chain(IHandler parent, IEnumerable<IConcernBuilder> concerns, Func<IHandler, IHandler> factory)
{
var stack = new Stack<IConcernBuilder>(concerns);

return Chain(parent, stack, factory);
}

private static IHandler Chain(IHandler parent, Stack<IConcernBuilder> remainders, Func<IHandler, IHandler> factory)
{
private static IHandler Chain(IHandler parent, Stack<IConcernBuilder> remainders, Func<IHandler, IHandler> factory)
{
if (remainders.Count > 0)
{
var concern = remainders.Pop();
Expand All @@ -43,6 +42,4 @@ private static IHandler Chain(IHandler parent, Stack<IConcernBuilder> remainders
return factory(parent);
}

}

}
21 changes: 9 additions & 12 deletions API/Content/IConcern.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
namespace GenHTTP.Api.Content
namespace GenHTTP.Api.Content;

/// <summary>
/// Functionality that wraps around a regular handler to add a
/// concern such as response compression.
/// </summary>
public interface IConcern : IHandler
{

/// <summary>
/// Functionality that wraps around a regular handler to add a
/// concern such as response compression.
/// The actual handler the concern is added to.
/// </summary>
public interface IConcern : IHandler
{

/// <summary>
/// The actual handler the concern is added to.
/// </summary>
IHandler Content { get; }

}
IHandler Content { get; }

}
25 changes: 11 additions & 14 deletions API/Content/IConcernBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using System;

namespace GenHTTP.Api.Content
namespace GenHTTP.Api.Content;

/// <summary>
/// Interface which needs to be implementd by factories providing concern instances.
/// </summary>
public interface IConcernBuilder
{

/// <summary>
/// Interface which needs to be implementd by factories providing concern instances.
/// Builds the concern and sets the inner handler of it.
/// </summary>
public interface IConcernBuilder
{

/// <summary>
/// Builds the concern and sets the inner handler of it.
/// </summary>
/// <param name="parent">The parent of the resulting concern handler instance</param>
/// <param name="contentFactory">A method providing the content of the concern</param>
/// <returns>The newly created concern instance</returns>
IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory);

}
/// <param name="parent">The parent of the resulting concern handler instance</param>
/// <param name="contentFactory">A method providing the content of the concern</param>
/// <returns>The newly created concern instance</returns>
IConcern Build(IHandler parent, Func<IHandler, IHandler> contentFactory);

}
45 changes: 21 additions & 24 deletions API/Content/IErrorMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content
namespace GenHTTP.Api.Content;

/// <summary>
/// Can be used with the error handling module to generate custom
/// responses for exceptions thrown during request handling.
/// </summary>
/// <typeparam name="T">The type of exception to be mapped (others will not be handled)</typeparam>
public interface IErrorMapper<in T> where T : Exception
{

/// <summary>
/// Can be used with the error handling module to generate custom
/// responses for exceptions thrown during request handling.
/// Generates a HTTP response to be sent for the given exception.
/// </summary>
/// <typeparam name="T">The type of exception to be mapped (others will not be handled)</typeparam>
public interface IErrorMapper<in T> where T : Exception
{

/// <summary>
/// Generates a HTTP response to be sent for the given exception.
/// </summary>
/// <param name="request">The request which caused the error</param>
/// <param name="handler">The handler which catched the exception</param>
/// <param name="error">The actual exception to be mapped</param>
/// <returns>A HTTP response to be sent or null, if the error should be handled as not found by the next error handler in the chain</returns>
ValueTask<IResponse?> Map(IRequest request, IHandler handler, T error);

/// <summary>
/// Generates a HTTP response for a resource that has not been found.
/// </summary>
/// <param name="request">The currently handled request</param>
/// <param name="handler">The inner handler of the error handling concern</param>
/// <returns>A HTTP response to be sent or null, if the error should be handled as not found by the next error handler in the chain</returns>
ValueTask<IResponse?> GetNotFound(IRequest request, IHandler handler);
/// <param name="request">The request which caused the error</param>
/// <param name="handler">The handler which catched the exception</param>
/// <param name="error">The actual exception to be mapped</param>
/// <returns>A HTTP response to be sent or null, if the error should be handled as not found by the next error handler in the chain</returns>
ValueTask<IResponse?> Map(IRequest request, IHandler handler, T error);

}
/// <summary>
/// Generates a HTTP response for a resource that has not been found.
/// </summary>
/// <param name="request">The currently handled request</param>
/// <param name="handler">The inner handler of the error handling concern</param>
/// <returns>A HTTP response to be sent or null, if the error should be handled as not found by the next error handler in the chain</returns>
ValueTask<IResponse?> GetNotFound(IRequest request, IHandler handler);

}
67 changes: 32 additions & 35 deletions API/Content/IHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,43 @@

using GenHTTP.Api.Protocol;

namespace GenHTTP.Api.Content
namespace GenHTTP.Api.Content;

/// <summary>
/// Content provider that is able to handle a request and return a HTTP
/// response to it.
/// </summary>
public interface IHandler
{

/// <summary>
/// Content provider that is able to handle a request and return a HTTP
/// response to it.
/// The parent of this handler within the routing tree.
/// </summary>
public interface IHandler
{

/// <summary>
/// The parent of this handler within the routing tree.
/// </summary>
IHandler Parent { get; }
IHandler Parent { get; }

/// <summary>
/// Invoked to perform computation heavy or IO bound work
/// that initializes the handler before handling the
/// first requests.
/// </summary>
/// <remarks>
/// Intended to keep the response time for the first
/// requests low. Handlers should relay this call to dependent
/// child handlers to initialize the whole handler chain.
/// May be called multiple times depending on the setup
/// the handler is used in.
/// </remarks>
ValueTask PrepareAsync();

/// <summary>
/// Handles the given request and returns a response, if applicable.
/// </summary>
/// <remarks>
/// Not returning a response causes the server to respond with a not found
/// response code.
/// </remarks>
/// <param name="request">The request to be handled</param>
/// <returns>The response to be sent to the requesting client</returns>
ValueTask<IResponse?> HandleAsync(IRequest request);
/// <summary>
/// Invoked to perform computation heavy or IO bound work
/// that initializes the handler before handling the
/// first requests.
/// </summary>
/// <remarks>
/// Intended to keep the response time for the first
/// requests low. Handlers should relay this call to dependent
/// child handlers to initialize the whole handler chain.
/// May be called multiple times depending on the setup
/// the handler is used in.
/// </remarks>
ValueTask PrepareAsync();

}
/// <summary>
/// Handles the given request and returns a response, if applicable.
/// </summary>
/// <remarks>
/// Not returning a response causes the server to respond with a not found
/// response code.
/// </remarks>
/// <param name="request">The request to be handled</param>
/// <returns>The response to be sent to the requesting client</returns>
ValueTask<IResponse?> HandleAsync(IRequest request);

}
Loading

0 comments on commit 9f87f7f

Please sign in to comment.