Last active
August 29, 2015 14:28
-
-
Save 0x1mason/250db27ec987534a7efa to your computer and use it in GitHub Desktop.
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
/// <summary> | |
/// Generator that allows lazy access to paginated resources. | |
/// </summary> | |
/// <typeparam name="TValue"></typeparam> | |
/// <param name="overrideUrl"></param> | |
/// <param name="pageLen"></param> | |
/// <returns></returns> | |
protected IEnumerable<List<TValue>> IteratePages<TValue> (string overrideUrl, int pageLen = DEFAULT_PAGE_LEN) { | |
Debug.Assert(!String.IsNullOrEmpty(overrideUrl)); | |
Debug.Assert(!overrideUrl.Contains("?")); | |
overrideUrl += "?pagelen=" + pageLen; | |
IteratorBasedPage<TValue> response = null; | |
do { | |
response = _sharpBucketV2.Get(new IteratorBasedPage<TValue>(), overrideUrl.Replace(SharpBucketV2.BITBUCKET_URL, "")); | |
if (response == null) { | |
throw new WebException("The REST client did not receive a response."); | |
} | |
yield return response.values; | |
overrideUrl = response.next; | |
} while (!String.IsNullOrEmpty(overrideUrl)); | |
} | |
/// <summary> | |
/// Returns a list of paginated values. | |
/// </summary> | |
/// <typeparam name="TValue"></typeparam> | |
/// <param name="overrideUrl"></param> | |
/// <param name="max">Set to 0 for unlimited size.</param> | |
/// <returns></returns> | |
protected List<TValue> GetPaginatedValues<TValue> (string overrideUrl, int max = 0) { | |
bool isMaxConstrained = max > 0; | |
// default page length in many cases is 10, requiring lots of requests for larger collections | |
int pageLen = (isMaxConstrained && max < DEFAULT_PAGE_LEN) ? max : DEFAULT_PAGE_LEN; | |
List<TValue> values = new List<TValue>(); | |
foreach (var page in IteratePages<TValue>(overrideUrl, pageLen)) { | |
values.AddRange(page); | |
if (isMaxConstrained && values.Count >= max) { | |
if (values.Count > max) { | |
values.RemoveRange(max, values.Count - max); | |
} | |
Debug.Assert(values.Count == max); | |
break; | |
} | |
} | |
return values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment