Skip to content

Commit

Permalink
Use primary constructors (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Apr 12, 2024
1 parent 700c245 commit fe89a06
Show file tree
Hide file tree
Showing 26 changed files with 126 additions and 257 deletions.
13 changes: 3 additions & 10 deletions src/FaluSdk/Core/BaseServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@
namespace Falu.Core;

///
public abstract class BaseServiceClient // This class exists because not all service clients may be based on a resource
public abstract class BaseServiceClient(HttpClient backChannel, FaluClientOptions options) // This class exists because not all service clients may be based on a resource
{
///
protected internal static string DefaultJsonContentType { get; } = "application/json";

///
protected BaseServiceClient(HttpClient backChannel, FaluClientOptions options)
{
BackChannel = backChannel ?? throw new ArgumentNullException(nameof(backChannel));
Options = options ?? throw new ArgumentNullException(nameof(options));
}

///
protected HttpClient BackChannel { get; }
protected HttpClient BackChannel { get; } = backChannel ?? throw new ArgumentNullException(nameof(backChannel));

///
protected FaluClientOptions Options { get; }
protected FaluClientOptions Options { get; } = options ?? throw new ArgumentNullException(nameof(options));

#region Helpers

Expand Down
20 changes: 6 additions & 14 deletions src/FaluSdk/Core/BaseServiceClientOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
namespace Falu.Core;

///
public abstract class BaseServiceClient<TResource> : BaseServiceClient
public abstract class BaseServiceClient<TResource>(HttpClient backChannel,
FaluClientOptions options,
JsonTypeInfo<TResource> jsonTypeInfo,
JsonTypeInfo<List<TResource>>? listJsonTypeInfo = null) : BaseServiceClient(backChannel, options)
{
///
protected BaseServiceClient(HttpClient backChannel, FaluClientOptions options)
Expand All @@ -13,25 +16,14 @@ protected BaseServiceClient(HttpClient backChannel, FaluClientOptions options)
Serialization.FaluSerializerContext.Default.GetRequriedTypeInfo<TResource>(),
Serialization.FaluSerializerContext.Default.GetTypeInfo<List<TResource>>()) { }

///
protected BaseServiceClient(HttpClient backChannel,
FaluClientOptions options,
JsonTypeInfo<TResource> jsonTypeInfo,
JsonTypeInfo<List<TResource>>? listJsonTypeInfo = null) // this allows one to pass JsonTypeInfo<T> from a different JsonSerializerContext e.g. in the CLI
: base(backChannel, options)
{
JsonTypeInfo = jsonTypeInfo ?? throw new ArgumentNullException(nameof(jsonTypeInfo));
ListJsonTypeInfo = listJsonTypeInfo;
}

///
protected abstract string BasePath { get; }

///
protected virtual JsonTypeInfo<TResource> JsonTypeInfo { get; }
protected virtual JsonTypeInfo<TResource> JsonTypeInfo { get; } = jsonTypeInfo ?? throw new ArgumentNullException(nameof(jsonTypeInfo));

///
protected virtual JsonTypeInfo<List<TResource>>? ListJsonTypeInfo { get; }
protected virtual JsonTypeInfo<List<TResource>>? ListJsonTypeInfo { get; } = listJsonTypeInfo;


///
Expand Down
11 changes: 2 additions & 9 deletions src/FaluSdk/Core/QueryValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
namespace Falu.Core;

/// <summary>Helper for handling query values.</summary>
public sealed class QueryValues : ICollection<KeyValuePair<string, StringValues>>
public sealed class QueryValues(Dictionary<string, StringValues>? values = null) : ICollection<KeyValuePair<string, StringValues>>
{
private readonly Dictionary<string, StringValues> values;

///
public QueryValues(Dictionary<string, StringValues>? values = null)
{
// keys are case insensitive
this.values = values == null
private readonly Dictionary<string, StringValues> values = values == null
? new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
: new Dictionary<string, StringValues>(values, StringComparer.OrdinalIgnoreCase);
}

/// <summary>Gets or sets the value associated with the specified key.</summary>
/// <param name="key">The key of the value to get or set.</param>
Expand Down
29 changes: 9 additions & 20 deletions src/FaluSdk/Core/RangeFilteringOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,21 @@

/// <summary>Standard options for range filtering.</summary>
/// <typeparam name="T"></typeparam>
public readonly struct RangeFilteringOptions<T> where T : struct, IComparable<T>, IEquatable<T>
/// <param name="lt">Option for less than (&lt;).</param>
/// <param name="lte">Option for less than or equal to (&lt;=).</param>
/// <param name="gt">Option for greater than (&gt;).</param>
/// <param name="gte">Option for greater than or equal to (&gt;=).</param>
public readonly struct RangeFilteringOptions<T>(T? lt = null, T? lte = null, T? gt = null, T? gte = null) where T : struct, IComparable<T>, IEquatable<T>
{
/// <summary>
/// Creates an instance <see cref="RangeFilteringOptions{T}"/>.
/// </summary>
/// <param name="lt">Option for less than (&lt;).</param>
/// <param name="lte">Option for less than or equal to (&lt;=).</param>
/// <param name="gt">Option for greater than (&gt;).</param>
/// <param name="gte">Option for greater than or equal to (&gt;=).</param>
public RangeFilteringOptions(T? lt = null, T? lte = null, T? gt = null, T? gte = null)
{
LessThan = lt;
LessThanOrEqualTo = lte;
GreaterThan = gt;
GreaterThanOrEqualTo = gte;
}

/// <summary>Option for less than (&lt;).</summary>
public T? LessThan { get; init; }
public T? LessThan { get; init; } = lt;

/// <summary>Option for less than or equal to (&lt;=).</summary>
public T? LessThanOrEqualTo { get; init; }
public T? LessThanOrEqualTo { get; init; } = lte;

/// <summary>Option for greater than (&gt;).</summary>
public T? GreaterThan { get; init; }
public T? GreaterThan { get; init; } = gt;

/// <summary>Option for greater than or equal to (&gt;=).</summary>
public T? GreaterThanOrEqualTo { get; init; }
public T? GreaterThanOrEqualTo { get; init; } = gte;
}
13 changes: 5 additions & 8 deletions src/FaluSdk/Customers/CustomersServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
namespace Falu.Customers;

///
public class CustomersServiceClient : BaseServiceClient<Customer>,
ISupportsListing<Customer, CustomersListOptions>,
ISupportsRetrieving<Customer>,
ISupportsCreation<Customer, CustomerCreateRequest>,
ISupportsUpdating<Customer, CustomerPatchModel>
public class CustomersServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<Customer>(backChannel, options),
ISupportsListing<Customer, CustomersListOptions>,
ISupportsRetrieving<Customer>,
ISupportsCreation<Customer, CustomerCreateRequest>,
ISupportsUpdating<Customer, CustomerPatchModel>
{
///
public CustomersServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/customers";

Expand Down
9 changes: 3 additions & 6 deletions src/FaluSdk/Events/EventsServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
namespace Falu.Events;

///
public class EventsServiceClient : BaseServiceClient<WebhookEvent>,
ISupportsListing<WebhookEvent, EventsListOptions>,
ISupportsRetrieving<WebhookEvent>
public class EventsServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<WebhookEvent>(backChannel, options),
ISupportsListing<WebhookEvent, EventsListOptions>,
ISupportsRetrieving<WebhookEvent>
{
///
public EventsServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/events";

Expand Down
12 changes: 3 additions & 9 deletions src/FaluSdk/FaluClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ public FaluClient(HttpClient backChannel, IOptionsSnapshot<TOptions> optionsAcce
/// <summary>
/// Official client for Falu API
/// </summary>
public class FaluClient : FaluClient<FaluClientOptions>
{
/// <summary>
/// Creates an instance of <see cref="FaluClient"/>
/// </summary>
/// <param name="backChannel"></param>
/// <param name="optionsAccessor"></param>
public FaluClient(HttpClient backChannel, IOptionsSnapshot<FaluClientOptions> optionsAccessor) : base(backChannel, optionsAccessor) { }
}
/// <param name="backChannel"></param>
/// <param name="optionsAccessor"></param>
public class FaluClient(HttpClient backChannel, IOptionsSnapshot<FaluClientOptions> optionsAccessor) : FaluClient<FaluClientOptions>(backChannel, optionsAccessor) { }
15 changes: 5 additions & 10 deletions src/FaluSdk/FileLinks/FileLinksServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@
namespace Falu.FileLinks;

///
public class FileLinksServiceClient : BaseServiceClient<FileLink>,
ISupportsListing<FileLink, FileLinksListOptions>,
ISupportsRetrieving<FileLink>,
ISupportsCreation<FileLink, FileLinkCreateRequest>,
ISupportsUpdating<FileLink, FileLinkPatchModel>
public class FileLinksServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<FileLink>(backChannel, options),
ISupportsListing<FileLink, FileLinksListOptions>,
ISupportsRetrieving<FileLink>,
ISupportsCreation<FileLink, FileLinkCreateRequest>,
ISupportsUpdating<FileLink, FileLinkPatchModel>
{
///
public FileLinksServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options)
{
}

///
protected override string BasePath => "/v1/file_links";

Expand Down
13 changes: 4 additions & 9 deletions src/FaluSdk/Files/FilesServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
namespace Falu.Files;

///
public class FilesServiceClient : BaseServiceClient<File>,
ISupportsListing<File, FilesListOptions>,
ISupportsRetrieving<File>,
ISupportsCreation<File, FileCreateRequest>
public class FilesServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<File>(backChannel, options),
ISupportsListing<File, FilesListOptions>,
ISupportsRetrieving<File>,
ISupportsCreation<File, FileCreateRequest>
{
///
public FilesServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options)
{
}

///
protected override string BasePath => "/v1/files";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
namespace Falu.IdentityVerificationReports;

///
public class IdentityVerificationReportsServiceClient : BaseServiceClient<IdentityVerificationReport>,
ISupportsListing<IdentityVerificationReport, IdentityVerificationReportsListOptions>,
ISupportsRetrieving<IdentityVerificationReport>
public class IdentityVerificationReportsServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<IdentityVerificationReport>(backChannel, options),
ISupportsListing<IdentityVerificationReport, IdentityVerificationReportsListOptions>,
ISupportsRetrieving<IdentityVerificationReport>
{
///
public IdentityVerificationReportsServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/identity/verification_reports";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
namespace Falu.IdentityVerifications;

///
public class IdentityVerificationsServiceClient : BaseServiceClient<IdentityVerification>,
ISupportsListing<IdentityVerification, IdentityVerificationsListOptions>,
ISupportsRetrieving<IdentityVerification>,
ISupportsCreation<IdentityVerification, IdentityVerificationCreateRequest>,
ISupportsUpdating<IdentityVerification, IdentityVerificationPatchModel>,
ISupportsCanceling<IdentityVerification>,
ISupportsRedaction<IdentityVerification>
public class IdentityVerificationsServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<IdentityVerification>(backChannel, options),
ISupportsListing<IdentityVerification, IdentityVerificationsListOptions>,
ISupportsRetrieving<IdentityVerification>,
ISupportsCreation<IdentityVerification, IdentityVerificationCreateRequest>,
ISupportsUpdating<IdentityVerification, IdentityVerificationPatchModel>,
ISupportsCanceling<IdentityVerification>,
ISupportsRedaction<IdentityVerification>
{
///
public IdentityVerificationsServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/identity/verifications";

Expand Down
17 changes: 7 additions & 10 deletions src/FaluSdk/MessageBatches/MessageBatchesServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
namespace Falu.MessageBatches;

///
public class MessageBatchesServiceClient : BaseServiceClient<MessageBatch>,
ISupportsListing<MessageBatch, MessageBatchesListOptions>,
ISupportsRetrieving<MessageBatch>,
ISupportsCreation<MessageBatch, MessageBatchCreateRequest>,
ISupportsUpdating<MessageBatch, MessageBatchPatchModel>,
ISupportsCanceling<MessageBatch>,
ISupportsRedaction<MessageBatch>
public class MessageBatchesServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<MessageBatch>(backChannel, options),
ISupportsListing<MessageBatch, MessageBatchesListOptions>,
ISupportsRetrieving<MessageBatch>,
ISupportsCreation<MessageBatch, MessageBatchCreateRequest>,
ISupportsUpdating<MessageBatch, MessageBatchPatchModel>,
ISupportsCanceling<MessageBatch>,
ISupportsRedaction<MessageBatch>
{
///
public MessageBatchesServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/message_batches";

Expand Down
13 changes: 5 additions & 8 deletions src/FaluSdk/MessageStreams/MessageStreamsServiceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
namespace Falu.MessageStreams;

///
public class MessageStreamsServiceClient : BaseServiceClient<MessageStream>,
ISupportsListing<MessageStream, MessageStreamsListOptions>,
ISupportsRetrieving<MessageStream>,
ISupportsCreation<MessageStream, MessageStreamCreateRequest>,
ISupportsUpdating<MessageStream, MessageStreamPatchModel>
public class MessageStreamsServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<MessageStream>(backChannel, options),
ISupportsListing<MessageStream, MessageStreamsListOptions>,
ISupportsRetrieving<MessageStream>,
ISupportsCreation<MessageStream, MessageStreamCreateRequest>,
ISupportsUpdating<MessageStream, MessageStreamPatchModel>
{
///
public MessageStreamsServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/message_streams";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Falu.MessageSuppressions;

///
public class MessageSuppressionsServiceClient : BaseServiceClient<MessageSuppression>,
///
public class MessageSuppressionsServiceClient(HttpClient backChannel, FaluClientOptions options) : BaseServiceClient<MessageSuppression>(backChannel, options),
ISupportsListing<MessageSuppression, MessageSuppressionsListOptions>,
ISupportsRetrieving<MessageSuppression>,
ISupportsCreation<MessageSuppression, MessageSuppressionCreateRequest>
{
///
public MessageSuppressionsServiceClient(HttpClient backChannel, FaluClientOptions options) : base(backChannel, options) { }

/// <inheritdoc/>
protected override string BasePath => "/v1/message_suppressions";
Expand Down
15 changes: 3 additions & 12 deletions src/FaluSdk/MessageTemplates/MessageTemplateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,16 @@ namespace Falu.MessageTemplates;
/// <summary>
/// Helper for creating models used to render message templates when sending messages.
/// </summary>
/// <param name="object">The <see cref="JsonObject"/> instance to be held.</param>
[JsonConverter(typeof(Serialization.MessageTemplateModelJsonConverter))]
public readonly struct MessageTemplateModel : IEquatable<MessageTemplateModel>
public readonly struct MessageTemplateModel(JsonObject @object) : IEquatable<MessageTemplateModel>
{
/// <summary>
/// An empty template model that does not require serialization
/// </summary>
public static readonly MessageTemplateModel Empty = new([]);

private readonly JsonObject @object;

/// <summary>Creates an instance of <see cref="MessageTemplateModel"/>.</summary>
/// <param name="object">The <see cref="JsonObject"/> instance to be held.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="object"/> is <see langword="null"/>.
/// </exception>
public MessageTemplateModel(JsonObject @object)
{
this.@object = @object ?? throw new ArgumentNullException(nameof(@object));
}
private readonly JsonObject @object = @object ?? throw new ArgumentNullException(nameof(@object));

/// <summary>
/// The <see cref="JsonObject"/> instance to be held.
Expand Down
Loading

0 comments on commit fe89a06

Please sign in to comment.