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

Adds Checkpoint Pagination support for fetching all connections #752

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
41 changes: 41 additions & 0 deletions src/Auth0.ManagementApi/Clients/ConnectionsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Auth0.ManagementApi.Clients
public class ConnectionsClient : BaseClient, IConnectionsClient
{
private readonly JsonConverter[] _converters = { new PagedListConverter<Connection>("connections") };
readonly JsonConverter[] checkpointPaginationConverter = new JsonConverter[] { new CheckpointPagedListConverter<Connection>("connections") };
private readonly JsonConverter[] _defaultMappingsConverter = { new ListConverter<ScimMapping>("mapping") };

/// <summary>
Expand Down Expand Up @@ -122,6 +123,46 @@ public Task<IPagedList<Connection>> GetAllAsync(GetConnectionsRequest request, P
return Connection.GetAsync<IPagedList<Connection>>(BuildUri("connections", queryStrings), DefaultHeaders, _converters, cancellationToken);
}

/// <summary>
/// Retrieves every connection matching the specified strategy.
/// All connections are retrieved if no strategy is being specified.
/// Accepts a list of fields to include or exclude in the resulting list of connection objects.
/// </summary>
/// <param name="request"><see cref="GetConnectionsRequest"/></param>
/// <param name="pagination"><see cref="CheckpointPaginationInfo"/></param>
/// <param name="cancellationToken"><see cref="CancellationToken"/></param>
/// <returns>List of <see cref="Connection"/></returns>
public Task<ICheckpointPagedList<Connection>> GetAllAsync(GetConnectionsRequest request, CheckpointPaginationInfo pagination = null,
CancellationToken cancellationToken = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));

var queryStrings = new Dictionary<string, string>
{
{"fields", request.Fields},
{"include_fields", request.IncludeFields?.ToString().ToLower()},
{"name", request.Name},
};

if (pagination != null)
{
queryStrings["from"] = pagination.From?.ToString();
queryStrings["take"] = pagination.Take.ToString();
}

// Add each strategy as a separate querystring
if (request.Strategy != null)
{
for (var i = 0; i < request.Strategy.Length; i++)
{
queryStrings.Add($"strategy[{i}]", request.Strategy[i]);
}
}

return Connection.GetAsync<ICheckpointPagedList<Connection>>(BuildUri("connections", queryStrings), DefaultHeaders, checkpointPaginationConverter, cancellationToken);
}

/// <summary>
/// Updates a connection.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Auth0.ManagementApi/Clients/IConnectionsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public interface IConnectionsClient
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>An <see cref="IPagedList{Connection}"/> containing the list of connections.</returns>
Task<IPagedList<Connection>> GetAllAsync(GetConnectionsRequest request, PaginationInfo pagination = null, CancellationToken cancellationToken = default);

/// <summary>
/// Retrieves every connection matching the specified strategy. All connections are retrieved if no strategy is being specified. Accepts a list of fields to include or exclude in the resulting list of connection objects.
/// </summary>
/// <param name="request">Specifies criteria to use when querying connections.</param>
/// <param name="pagination">Specifies the CheckPoint Pagination info <see cref="CheckpointPaginationInfo"/></param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>An <see cref="ICheckpointPagedList{Connection}"/> containing the list of connections.</returns>
Task<ICheckpointPagedList<Connection>> GetAllAsync(GetConnectionsRequest request, CheckpointPaginationInfo pagination = null, CancellationToken cancellationToken = default);

/// <summary>
/// Updates a connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public ConnectionsCleanUpStrategy(ManagementApiClient apiClient) : base(CleanUpT
public override async Task Run(string id)
{
System.Diagnostics.Debug.WriteLine("Running ConnectionsCleanUpStrategy");
var connections = await ApiClient.Connections.GetAllAsync(new ManagementApi.Models.GetConnectionsRequest { });
await ApiClient.Connections.DeleteAsync(id);
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Auth0.ManagementApi.IntegrationTests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using System.Collections.Generic;
using Auth0.AuthenticationApi.Models;
using Auth0.ManagementApi.Models.Connections;
using Auth0.Tests.Shared;

Check warning on line 14 in tests/Auth0.ManagementApi.IntegrationTests/ConnectionTests.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'Auth0.Tests.Shared' appeared previously in this namespace

Check warning on line 14 in tests/Auth0.ManagementApi.IntegrationTests/ConnectionTests.cs

View workflow job for this annotation

GitHub Actions / build

The using directive for 'Auth0.Tests.Shared' appeared previously in this namespace

Check warning on line 14 in tests/Auth0.ManagementApi.IntegrationTests/ConnectionTests.cs

View workflow job for this annotation

GitHub Actions / Test Auth0.ManagementApi

The using directive for 'Auth0.Tests.Shared' appeared previously in this namespace
using Newtonsoft.Json;

namespace Auth0.ManagementApi.IntegrationTests
Expand Down Expand Up @@ -195,6 +195,17 @@
// Assert
Assert.Null(connections.Paging);
}

[Fact]
public async Task Test_get_all_with_checkpoint_pagination()
{
// Act
var connections = await fixture.ApiClient.Connections.GetAllAsync(
new GetConnectionsRequest(), new CheckpointPaginationInfo(10));

// Assert
connections.Count.Should().Be(10);
}

[Fact]
public async Task Test_paging_does_not_include_totals()
Expand Down