Skip to content

Commit

Permalink
(GH-368) Add missing 'limit' and 'offset' parameters to IpAddresses.G…
Browse files Browse the repository at this point in the history
…etUnassignedAsync

(cherry picked from commit 25b000c)
  • Loading branch information
Jericho committed Jan 11, 2022
1 parent 8d76915 commit 73cf9a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Source/StrongGrid.IntegrationTests/Tests/IpAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken
await log.WriteLineAsync($"There are {assigned.Length} assigned IP addresses").ConfigureAwait(false);

// GET THE UNASSIGNED IP ADDRESSES
var unAssigned = await client.IpAddresses.GetUnassignedAsync(cancellationToken).ConfigureAwait(false);
var unAssigned = await client.IpAddresses.GetUnassignedAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
await log.WriteLineAsync($"There are {unAssigned.Length} unassigned IP addresses").ConfigureAwait(false);

// GET THE REMAINING IP ADDRESSES
Expand Down
12 changes: 9 additions & 3 deletions Source/StrongGrid/Extensions/Public.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,26 +1144,32 @@ public static Task<Segment> UpdateAsync(this ISegments segments, string segmentI
/// Retrieve unassigned IP addresses.
/// </summary>
/// <param name="ipAddresses">The IP addresses resource.</param>
/// <param name="limit">The number of IPs you want returned at the same time.</param>
/// <param name="offset">The offset for the number of IPs that you are requesting.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>
/// An array of <see cref="IpAddress">Ip addresses</see>.
/// </returns>
public static async Task<IpAddress[]> GetUnassignedAsync(this IIpAddresses ipAddresses, CancellationToken cancellationToken = default)
public static async Task<IpAddress[]> GetUnassignedAsync(this IIpAddresses ipAddresses, int limit = 10, int offset = 0, CancellationToken cancellationToken = default)
{
var unassignedIpAddresses = new List<IpAddress>();
var unassignedIpAddresses = new List<IpAddress>(limit);
var currentOffset = 0;

while (true)
{
var allIpAddresses = await ipAddresses.GetAllAsync(limit: Utils.MaxSendGridPagingLimit, offset: currentOffset, cancellationToken: cancellationToken).ConfigureAwait(false);
unassignedIpAddresses.AddRange(allIpAddresses.Where(ip => ip.Pools == null || !ip.Pools.Any()));

if (unassignedIpAddresses.Count >= offset + limit) break;
if (allIpAddresses.Length < Utils.MaxSendGridPagingLimit) break;

currentOffset += Utils.MaxSendGridPagingLimit;
}

return unassignedIpAddresses.ToArray();
return unassignedIpAddresses
.Skip(offset)
.Take(limit)
.ToArray();
}
}
}

0 comments on commit 73cf9a5

Please sign in to comment.