Skip to content
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

Added some additional helper ctors to the DatasyncServiceClient #77

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.Datasync.Client.Http;
using CommunityToolkit.Datasync.Client.Paging;
using CommunityToolkit.Datasync.Client.Query;
using CommunityToolkit.Datasync.Client.Query.Linq;
Expand All @@ -23,6 +24,31 @@ namespace CommunityToolkit.Datasync.Client;
/// <typeparam name="TEntity">The type of entity being processed by this service client.</typeparam>
internal class DatasyncServiceClient<TEntity> : IDatasyncServiceClient<TEntity> where TEntity : class
{
/// <summary>
/// Creates a new <see cref="DatasyncServiceClient{TEntity}"/> using default information based on
/// the <see cref="HttpClientOptions"/> provided.
/// </summary>
/// <remarks>
/// The default path is /tables/entityName as a relative URI to the Endpoint in the options.
/// </remarks>
/// <param name="options">The <see cref="HttpClientOptions"/> to use.</param>
public DatasyncServiceClient(HttpClientOptions options)
: this(new Uri($"/tables/{typeof(TEntity).Name.ToLowerInvariant()}", UriKind.Relative), new HttpClientFactory(options).CreateClient())
{
}

/// <summary>
/// Creates a new <see cref="DatasyncServiceClient{TEntity}"/> with the normal information required for
/// communicating with a datasync service, using the default JSON Serializer Options.
/// </summary>
/// <param name="endpoint">The endpoint of the table controller that processes the entity.</param>
/// <param name="client">The <see cref="HttpClient"/> to use for communication.</param>
/// <exception cref="UriFormatException">Thrown if the endpoint is not valid.</exception>
public DatasyncServiceClient(Uri endpoint, HttpClient client)
: this(endpoint, client, DatasyncSerializer.JsonSerializerOptions)
{
}

/// <summary>
/// Creates a new <see cref="DatasyncServiceClient{TEntity}"/> with the normal information required for
/// communicating with a datasync service.
Expand All @@ -33,6 +59,7 @@ internal class DatasyncServiceClient<TEntity> : IDatasyncServiceClient<TEntity>
/// <exception cref="UriFormatException">Thrown if the endpoint is not valid.</exception>
public DatasyncServiceClient(Uri endpoint, HttpClient client, JsonSerializerOptions serializerOptions)
{
endpoint = MakeAbsoluteUri(client.BaseAddress, endpoint);
ThrowIf.IsNotValidEndpoint(endpoint, nameof(endpoint));
ArgumentNullException.ThrowIfNull(client, nameof(client));
ArgumentNullException.ThrowIfNull(serializerOptions, nameof(serializerOptions));
Expand Down Expand Up @@ -493,4 +520,28 @@ internal async ValueTask<Page<TEntity>> GetNextPageAsync(string queryOrContinuat
result.ThrowIfNotSuccessful(requireContent: true);
return result.Value!;
}

/// <summary>
/// Converts a base address + relative/absolute URI into the appropriate URI for the datasync service.
/// </summary>
/// <param name="baseAddress">The base address from the client.</param>
/// <param name="relativeOrAbsoluteUri">A relative or absolute URI</param>
/// <returns></returns>
internal static Uri MakeAbsoluteUri(Uri? baseAddress, Uri relativeOrAbsoluteUri)
{
if (relativeOrAbsoluteUri.IsAbsoluteUri)
{
return new Uri($"{relativeOrAbsoluteUri.ToString().TrimEnd('/')}/");
}

if (baseAddress != null)
{
if (baseAddress.IsAbsoluteUri)
{
return new Uri($"{new Uri(baseAddress, relativeOrAbsoluteUri).ToString().TrimEnd('/')}/");
}
}

throw new UriFormatException("Invalid combination of baseAddress and relativeUri");
}
}
Loading
Loading