-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ExampleProject): add HttpRequest example (#26)
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Non-nullable member is uninitialized | ||
#pragma warning disable CS8618 | ||
// ReSharper disable All | ||
|
||
// Example from https://github.com/dotnet/csharplang/discussions/7325. | ||
|
||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
using M31.FluentApi.Attributes; | ||
|
||
namespace ExampleProject; | ||
|
||
[FluentApi] | ||
public class HttpRequest | ||
{ | ||
[FluentMember(0)] | ||
public HttpMethod Method { get; private set; } | ||
|
||
[FluentMember(1)] | ||
public string Url { get; private set; } | ||
|
||
[FluentCollection(2, "Header")] | ||
public List<(string, string)> Headers { get; private set; } | ||
|
||
[FluentMember(3)] | ||
public HttpContent Content { get; private set; } | ||
|
||
[FluentMethod(3)] | ||
public void WithJsonContent<T>(T body, Action<JsonSerializerOptions>? configureSerializer = null) | ||
{ | ||
JsonSerializerOptions options = new JsonSerializerOptions(JsonSerializerDefaults.Web); | ||
configureSerializer?.Invoke(options); | ||
Content = new StringContent(JsonSerializer.Serialize(body)); | ||
} | ||
|
||
[FluentMethod(4)] | ||
[FluentReturn] | ||
public HttpRequestMessage GetMessage() | ||
{ | ||
HttpRequestMessage request = new HttpRequestMessage(Method, Url); | ||
request.Content = Content; | ||
Headers.ForEach(h => request.Headers.Add(h.Item1, h.Item2)); | ||
return request; | ||
} | ||
} |
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