-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add result type which allows to do response modifications (#439)
- Loading branch information
1 parent
6738b6d
commit 1e19149
Showing
50 changed files
with
540 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,31 @@ | ||
using System; | ||
|
||
using GenHTTP.Api.Infrastructure; | ||
using GenHTTP.Api.Infrastructure; | ||
|
||
namespace GenHTTP.Api.Protocol | ||
{ | ||
|
||
/// <summary> | ||
/// Allows to configure a HTTP response to be send. | ||
/// </summary> | ||
public interface IResponseBuilder : IBuilder<IResponse> | ||
public interface IResponseBuilder : IBuilder<IResponse>, IResponseModification<IResponseBuilder> | ||
{ | ||
|
||
/// <summary> | ||
/// The request the response belongs to. | ||
/// </summary> | ||
IRequest Request { get; } | ||
|
||
/// <summary> | ||
/// Specifies the HTTP status code of the response. | ||
/// </summary> | ||
/// <param name="status">The HTTP status code of the response</param> | ||
IResponseBuilder Status(ResponseStatus status); | ||
|
||
/// <summary> | ||
/// Specifies the HTTP status code of the response. | ||
/// </summary> | ||
/// <param name="status">The status code of the response</param> | ||
/// <param name="reason">The reason phrase of the response (such as "Not Found" for 404)</param> | ||
IResponseBuilder Status(int status, string reason); | ||
|
||
/// <summary> | ||
/// Sets the given header field on the response. Changing HTTP | ||
/// protocol headers may cause incorrect behavior. | ||
/// </summary> | ||
/// <param name="key">The name of the header to be set</param> | ||
/// <param name="value">The value of the header field</param> | ||
IResponseBuilder Header(string key, string value); | ||
|
||
/// <summary> | ||
/// Sets the expiration date of the response. | ||
/// </summary> | ||
/// <param name="expiryDate">The expiration date of the response</param> | ||
IResponseBuilder Expires(DateTime expiryDate); | ||
|
||
/// <summary> | ||
/// Sets the point in time when the requested resource has been | ||
/// modified last. | ||
/// </summary> | ||
/// <param name="modificationDate">The point in time when the requested resource has been modified last</param> | ||
IResponseBuilder Modified(DateTime modificationDate); | ||
|
||
/// <summary> | ||
/// Adds the given cookie to the response. | ||
/// </summary> | ||
/// <param name="cookie">The cookie to be added</param> | ||
IResponseBuilder Cookie(Cookie cookie); | ||
|
||
/// <summary> | ||
/// Specifies the content to be sent to the client. | ||
/// </summary> | ||
/// <param name="content">The content to be send to the client</param> | ||
IResponseBuilder Content(IResponseContent content); | ||
|
||
/// <summary> | ||
/// Specifies the content type of this response. | ||
/// </summary> | ||
/// <param name="contentType">The content type of this response</param> | ||
IResponseBuilder Type(FlexibleContentType contentType); | ||
|
||
/// <summary> | ||
/// Specifies the length of the content stream, if known. | ||
/// </summary> | ||
/// <param name="length">The length of the content stream</param> | ||
IResponseBuilder Length(ulong length); | ||
|
||
/// <summary> | ||
/// Sets the encoding of the content. | ||
/// </summary> | ||
/// <param name="encoding">The encoding of the content</param> | ||
IResponseBuilder Encoding(string encoding); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
|
||
namespace GenHTTP.Api.Protocol | ||
{ | ||
|
||
/// <summary> | ||
/// The protocol allowing to manipulate the response sent by | ||
/// the server. | ||
/// </summary> | ||
/// <typeparam name="T">The type of builder used as a return value</typeparam> | ||
public interface IResponseModification<out T> | ||
{ | ||
|
||
/// <summary> | ||
/// Specifies the HTTP status code of the response. | ||
/// </summary> | ||
/// <param name="status">The HTTP status code of the response</param> | ||
T Status(ResponseStatus status); | ||
|
||
/// <summary> | ||
/// Specifies the HTTP status code of the response. | ||
/// </summary> | ||
/// <param name="status">The status code of the response</param> | ||
/// <param name="reason">The reason phrase of the response (such as "Not Found" for 404)</param> | ||
T Status(int status, string reason); | ||
|
||
/// <summary> | ||
/// Sets the given header field on the response. Changing HTTP | ||
/// protocol headers may cause incorrect behavior. | ||
/// </summary> | ||
/// <param name="key">The name of the header to be set</param> | ||
/// <param name="value">The value of the header field</param> | ||
T Header(string key, string value); | ||
|
||
/// <summary> | ||
/// Sets the expiration date of the response. | ||
/// </summary> | ||
/// <param name="expiryDate">The expiration date of the response</param> | ||
T Expires(DateTime expiryDate); | ||
|
||
/// <summary> | ||
/// Sets the point in time when the requested resource has been | ||
/// modified last. | ||
/// </summary> | ||
/// <param name="modificationDate">The point in time when the requested resource has been modified last</param> | ||
T Modified(DateTime modificationDate); | ||
|
||
/// <summary> | ||
/// Adds the given cookie to the response. | ||
/// </summary> | ||
/// <param name="cookie">The cookie to be added</param> | ||
T Cookie(Cookie cookie); | ||
|
||
/// <summary> | ||
/// Specifies the content type of this response. | ||
/// </summary> | ||
/// <param name="contentType">The content type of this response</param> | ||
T Type(FlexibleContentType contentType); | ||
|
||
/// <summary> | ||
/// Sets the encoding of the content. | ||
/// </summary> | ||
/// <param name="encoding">The encoding of the content</param> | ||
T Encoding(string encoding); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
|
||
using GenHTTP.Api.Protocol; | ||
|
||
namespace GenHTTP.Modules.Reflection | ||
{ | ||
|
||
internal static class Adjustments | ||
{ | ||
|
||
/// <summary> | ||
/// Allows to chain the execution of the given adjustments into | ||
/// the given response builder. | ||
/// </summary> | ||
/// <param name="builder">The response builder to be adjusted</param> | ||
/// <param name="adjustments">The adjustments to be executed (if any)</param> | ||
/// <returns>The response builder to be chained</returns> | ||
internal static IResponseBuilder Adjust(this IResponseBuilder builder, Action<IResponseBuilder>? adjustments) | ||
{ | ||
adjustments?.Invoke(builder); | ||
|
||
return builder; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using GenHTTP.Api.Protocol; | ||
|
||
namespace GenHTTP.Modules.Reflection | ||
{ | ||
|
||
/// <summary> | ||
/// Allows the framework to unwrap <see cref="Result{T}" /> | ||
/// instances. | ||
/// </summary> | ||
internal interface IResultWrapper | ||
{ | ||
|
||
/// <summary> | ||
/// The actual result to be returned. | ||
/// </summary> | ||
object? Payload { get; } | ||
|
||
/// <summary> | ||
/// Performs the configured modifications to the response | ||
/// on the given builder. | ||
/// </summary> | ||
/// <param name="builder">The response builder to manipulate</param> | ||
void Apply(IResponseBuilder builder); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.