-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Add result type which allows to do response modifications #439
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c08aa2
First API draft
Kaliumhexacyanoferrat 58cbec4
Hide implementation details, add documentation
Kaliumhexacyanoferrat 1d45c75
Add first tests
Kaliumhexacyanoferrat 5b21982
Additional coverage
Kaliumhexacyanoferrat bdd0872
Polishment
Kaliumhexacyanoferrat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API looks good to me; it's essentially what I've already put together on my own, but with the benefit that it handles the response building implicitly under the hood, and since all it does is modify a default response, it removes a lot of the busywork that's necessary when building a response manually. Very nice.