Last active
April 10, 2018 22:08
-
-
Save Kralizek/23a54b9df3fd8799b2003096e19842b3 to your computer and use it in GitHub Desktop.
A C# function that fetches all the items returned by a service across several pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IAmazonS3 client = new AmazonS3Client (RegionEndpoint.EUWest1); | |
var objects = await FetchAll<S3Object, ListObjectsV2Request, ListObjectsV2Response>( | |
() => new ListObjectsV2Request { BucketName = "my-bucket", MaxKeys = 1000 }, | |
req => client.ListObjectsV2Async (req), | |
res => res.NextContinuationToken, | |
(req, token) => req.ContinuationToken = token, | |
res => res.S3Objects, | |
res => res.IsTruncated | |
); | |
objects.Dump(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IAmazonSecretsManager client = new AmazonSecretsManagerClient (RegionEndpoint.EUWest1); | |
var allSecrets = await FetchAll<SecretListEntry, ListSecretsRequest, ListSecretsResponse> ( | |
() => new ListSecretsRequest(), | |
request => client.ListSecretsAsync(request), | |
response => response.NextToken, | |
(request, token) => request.NextToken = token, | |
response => response.SecretList, | |
response => response.NextToken != null | |
); | |
allSecrets.Dump(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async Task<IReadOnlyList<T>> FetchAll<T, TRequest, TResponse> ( | |
Func<TRequest> requestFactory, | |
Func<TRequest, Task<TResponse>> requestor, | |
Func<TResponse, string> tokenGetter, | |
Action<TRequest, string> tokenSetter, | |
Func<TResponse, IEnumerable<T>> resultExtractor, | |
Func<TResponse, bool> hasMore | |
) | |
{ | |
if (requestFactory == null) throw new ArgumentNullException(nameof(requestFactory)); | |
if (requestor == null) throw new ArgumentNullException(nameof(requestor)); | |
if (tokenGetter == null) throw new ArgumentNullException(nameof(tokenGetter)); | |
if (tokenSetter == null) throw new ArgumentNullException(nameof(tokenSetter)); | |
if (resultExtractor == null) throw new ArgumentNullException(nameof(resultExtractor)); | |
if (hasMore == null) throw new ArgumentNullException(nameof(hasMore)); | |
TResponse response = default; | |
List<T> result = new List<T>(); | |
do | |
{ | |
var nextToken = response != null ? tokenGetter (response) : null; | |
var request = requestFactory(); | |
tokenSetter (request, nextToken); | |
response = await requestor (request); | |
var items = resultExtractor(response); | |
result.AddRange (items); | |
} while (hasMore(response)); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment