Skip to content

Commit

Permalink
+ RestClientOptions class
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fischer committed Nov 9, 2023
1 parent cc5310e commit 96b0337
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions BytecodeApi.Rest/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public abstract class RestClient : IDisposable
/// </summary>
public string BaseUrl { get; private init; }
/// <summary>
/// A <see cref="RestClientOptions" /> object with formatting options for this <see cref="RestClient" /> instance.
/// </summary>
protected internal RestClientOptions Options { get; set; }
/// <summary>
/// Gets the <see cref="System.Net.Http.HttpClient" /> that is used to process requests.
/// </summary>
protected internal HttpClient HttpClient { get; private init; }
Expand All @@ -24,6 +28,7 @@ protected RestClient(string baseUrl)
Check.ArgumentNull(baseUrl);

BaseUrl = baseUrl.TrimEnd('/');
Options = new();
HttpClient = new();
}
/// <summary>
Expand Down
33 changes: 33 additions & 0 deletions BytecodeApi.Rest/RestClientOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace BytecodeApi.Rest;

/// <summary>
/// Represents a set of formatting options for a <see cref="RestClient" /> instance.
/// </summary>
public sealed class RestClientOptions
{
/// <summary>
/// Gets or sets a value specifying the format that is used to convert <see cref="DateTime" /> values.
/// <para>The default value is "yyyy-MM-ddTHH:mm:sszzz".</para>
/// </summary>
public string QueryParameterDateTimeFormat { get; set; }
/// <summary>
/// Gets or sets a value specifying the format that is used to convert <see cref="DateOnly" /> values.
/// <para>The default value is "yyyy-MM-dd".</para>
/// </summary>
public string QueryParameterDateOnlyFormat { get; set; }
/// <summary>
/// Gets or sets a value specifying the format that is used to convert <see cref="TimeOnly" /> values.
/// <para>The default value is "HH:mm:ss".</para>
/// </summary>
public string QueryParameterTimeOnlyFormat { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="RestClientOptions" /> class with default formatting options.
/// </summary>
public RestClientOptions()
{
QueryParameterDateTimeFormat = "yyyy-MM-ddTHH:mm:sszzz";
QueryParameterDateOnlyFormat = "yyyy-MM-dd";
QueryParameterTimeOnlyFormat = "HH:mm:ss";
}
}
6 changes: 4 additions & 2 deletions BytecodeApi.Rest/RestRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public RestRequest QueryParameter(string name, object? value)
{
Check.ObjectDisposed<RestClient>(RestClient.Disposed);
Check.ArgumentNull(name);
Check.ArgumentNull(RestClient.Options);

if (value == null)
{
Expand All @@ -53,8 +54,9 @@ public RestRequest QueryParameter(string name, object? value)
{
string str => str,
Enum enumValue => Convert.ToInt32(enumValue).ToString(),
DateTime dateTimeParameter => dateTimeParameter.ToStringInvariant("yyyy-MM-dd HH:mm:ss"),
DateOnly dateOnlyParameter => dateOnlyParameter.ToStringInvariant("yyyy-MM-dd"),
DateTime dateTimeParameter => dateTimeParameter.ToStringInvariant(RestClient.Options.QueryParameterDateTimeFormat),
DateOnly dateOnlyParameter => dateOnlyParameter.ToStringInvariant(RestClient.Options.QueryParameterDateOnlyFormat),
TimeOnly timeOnlyParameter => timeOnlyParameter.ToStringInvariant(RestClient.Options.QueryParameterTimeOnlyFormat),
_ => value?.ToString() ?? ""
};

Expand Down

0 comments on commit 96b0337

Please sign in to comment.