Skip to content

Commit

Permalink
Adds Checkpoint Pagination support for fetching all connections (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
kailash-b authored Nov 28, 2024
2 parents d107e84 + d5a32f4 commit 6d570af
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
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 @@ -195,6 +195,17 @@ public async Task Test_when_paging_not_specified_does_not_include_totals()
// 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

0 comments on commit 6d570af

Please sign in to comment.