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

bugfix: bitbucket paged results, use next page id versus incrementing… #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
203 changes: 107 additions & 96 deletions src/Bitbucket.Cloud.Net/BitbucketCloudClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Bitbucket.Cloud.Net.Common;
Expand All @@ -13,100 +14,110 @@

namespace Bitbucket.Cloud.Net
{
public partial class BitbucketCloudClient
{
private static readonly ISerializer s_serializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

private readonly Url _url;
private readonly AuthenticationMethod _auth;

private BitbucketCloudClient(string url)
{
_url = url;
}

public BitbucketCloudClient(string url, OAuthAuthentication oauth)
: this(url)
{
_auth = oauth;
}

public BitbucketCloudClient(string url, AppPasswordAuthentication appPassword)
: this(url)
{
_auth = appPassword;
}

public BitbucketCloudClient(string url, BasicAuthentication basic)
: this(url)
{
_auth = basic;
}

public IFlurlRequest GetBaseUrl(string path) => new Url(_url)
.AppendPathSegment(path)
.ConfigureRequest(settings => settings.JsonSerializer = s_serializer)
.WithAuthentication(_auth);

private async Task<TResult> ReadResponseContentAsync<TResult>(HttpResponseMessage responseMessage, Func<string, TResult> contentHandler = null)
{
string content = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return contentHandler != null
? contentHandler(content)
: JsonConvert.DeserializeObject<TResult>(content);
}

private async Task<bool> ReadResponseContentAsync(HttpResponseMessage responseMessage)
{
string content = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return content.Length == 0;
}

private async Task HandleErrorsAsync(HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
var errorResponse = await ReadResponseContentAsync<ErrorResponse>(response).ConfigureAwait(false);

string errorMessage = string.Join(Environment.NewLine, errorResponse?.Error?.Message);
throw new InvalidOperationException($"Http request failed ({(int)response.StatusCode} - {response.StatusCode}):\n{errorMessage}");
}
}

private async Task<TResult> HandleResponseAsync<TResult>(HttpResponseMessage responseMessage, Func<string, TResult> contentHandler = null)
{
await HandleErrorsAsync(responseMessage).ConfigureAwait(false);
return await ReadResponseContentAsync(responseMessage, contentHandler).ConfigureAwait(false);
}

private async Task<bool> HandleResponseAsync(HttpResponseMessage responseMessage)
{
await HandleErrorsAsync(responseMessage).ConfigureAwait(false);
return await ReadResponseContentAsync(responseMessage).ConfigureAwait(false);
}

private async Task<IEnumerable<T>> GetPagedResultsAsync<T>(int? maxPages, IDictionary<string, object> queryParamValues, Func<IDictionary<string, object>, Task<PagedResults<T>>> selector)
{
var results = new List<T>();
bool isLastPage = false;
int numPages = 0;

while (!isLastPage && (maxPages == null || numPages < maxPages))
{
var selectorResults = await selector(queryParamValues).ConfigureAwait(false);
selectorResults.Page = Math.Max(selectorResults.Page, 1);
results.AddRange(selectorResults.Values);

isLastPage = selectorResults.Next == null || selectorResults.Size == numPages * selectorResults.PageLen;
if (!isLastPage)
{
queryParamValues["page"] = selectorResults.Page + 1;
}

numPages++;
}

return results;
}
}
public partial class BitbucketCloudClient
{
private static readonly ISerializer s_serializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });

private readonly Url _url;
private readonly AuthenticationMethod _auth;

private BitbucketCloudClient(string url)
{
_url = url;
}

public BitbucketCloudClient(string url, OAuthAuthentication oauth)
: this(url)
{
_auth = oauth;
}

public BitbucketCloudClient(string url, AppPasswordAuthentication appPassword)
: this(url)
{
_auth = appPassword;
}

public BitbucketCloudClient(string url, BasicAuthentication basic)
: this(url)
{
_auth = basic;
}

public IFlurlRequest GetBaseUrl(string path) => new Url(_url)
.AppendPathSegment(path)
.ConfigureRequest(settings => settings.JsonSerializer = s_serializer)
.WithAuthentication(_auth);

private async Task<TResult> ReadResponseContentAsync<TResult>(HttpResponseMessage responseMessage, Func<string, TResult> contentHandler = null)
{
string content = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return contentHandler != null
? contentHandler(content)
: JsonConvert.DeserializeObject<TResult>(content);
}

private async Task<bool> ReadResponseContentAsync(HttpResponseMessage responseMessage)
{
string content = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return content.Length == 0;
}

private async Task HandleErrorsAsync(HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
var errorResponse = await ReadResponseContentAsync<ErrorResponse>(response).ConfigureAwait(false);

string errorMessage = string.Join(Environment.NewLine, errorResponse?.Error?.Message);
throw new InvalidOperationException($"Http request failed ({(int)response.StatusCode} - {response.StatusCode}):\n{errorMessage}");
}
}

private async Task<TResult> HandleResponseAsync<TResult>(HttpResponseMessage responseMessage, Func<string, TResult> contentHandler = null)
{
await HandleErrorsAsync(responseMessage).ConfigureAwait(false);
return await ReadResponseContentAsync(responseMessage, contentHandler).ConfigureAwait(false);
}

private async Task<bool> HandleResponseAsync(HttpResponseMessage responseMessage)
{
await HandleErrorsAsync(responseMessage).ConfigureAwait(false);
return await ReadResponseContentAsync(responseMessage).ConfigureAwait(false);
}

private async Task<IEnumerable<T>> GetPagedResultsAsync<T>(int? maxPages, IDictionary<string, object> queryParamValues, Func<IDictionary<string, object>, Task<PagedResults<T>>> selector)
{
var results = new List<T>();
bool isLastPage = false;
int numPages = 0;

while (!isLastPage && (maxPages == null || numPages < maxPages))
{
var selectorResults = await selector(queryParamValues).ConfigureAwait(false);
selectorResults.Page = Math.Max(selectorResults.Page, 1);
results.AddRange(selectorResults.Values);

isLastPage = selectorResults.Next == null;

// parse out the next page id from the results
if (!isLastPage)
{
var myUri = new Uri(selectorResults.Next);
var nextPage = myUri.DecodeQueryParameters().FirstOrDefault(x => x.Key == "page");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse out the page id from the "next" url and use that for the next pge versus auto incrementing


if (nextPage.Value != null)
queryParamValues["page"] = nextPage.Value;
else
isLastPage = true;
}

numPages++;
}

return results;
}
}


}
27 changes: 23 additions & 4 deletions src/Bitbucket.Cloud.Net/Common/UriExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Bitbucket.Cloud.Net.Common
{
public static class UriExtensions
{
public static string GetLastPathSegment(this string s) => new Uri(s).Segments.LastOrDefault();
}
public static class UriExtensions
{
public static string GetLastPathSegment(this string s) => new Uri(s).Segments.LastOrDefault();


public static Dictionary<string, string> DecodeQueryParameters(this Uri uri)
{
if (uri == null)
throw new ArgumentNullException("uri");

if (uri.Query.Length == 0)
return new Dictionary<string, string>();

return uri.Query.TrimStart('?')
.Split(new[] { '&', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(parameter => parameter.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries))
.GroupBy(parts => parts[0],
parts => parts.Length > 2 ? string.Join("=", parts, 1, parts.Length - 1) : (parts.Length > 1 ? parts[1] : ""))
.ToDictionary(grouping => grouping.Key,
grouping => string.Join(",", grouping));
}
}
}