Skip to content

Commit 7cb46f1

Browse files
committed
Merge pull request #2 from sharwell/GetAllPages
Aggregate data from multiple pages of results
2 parents 3e0594a + 69ae197 commit 7cb46f1

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

Git.hub/Client.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public IList<Repository> getRepositories()
6363

6464
var request = new RestRequest("/user/repos?type=all");
6565

66-
var repos = client.Get<List<Repository>>(request);
67-
if (repos.Data == null)
66+
var repos = client.GetList<Repository>(request);
67+
if (repos == null)
6868
throw new Exception("Bad Credentials");
6969

70-
repos.Data.ForEach(r => r._client = client);
71-
return repos.Data;
70+
repos.ForEach(r => r._client = client);
71+
return repos;
7272
}
7373

7474
/// <summary>
@@ -81,9 +81,10 @@ public IList<Repository> getRepositories(string username)
8181
var request = new RestRequest("/users/{name}/repos");
8282
request.AddUrlSegment("name", username);
8383

84-
var list = client.Get<List<Repository>>(request).Data;
84+
var list = client.GetList<Repository>(request);
8585
if (list == null)
8686
throw new InvalidOperationException("User does not exist.");
87+
8788
list.ForEach(r => r._client = client);
8889
return list;
8990
}
@@ -119,7 +120,7 @@ public IList<Repository> getOrganizationRepositories(string organization)
119120
var request = new RestRequest("/orgs/{org}/repos");
120121
request.AddUrlSegment("org", organization);
121122

122-
var list = client.Get<List<Repository>>(request).Data;
123+
var list = client.GetList<Repository>(request);
123124

124125
Organization org = new Organization { Login = organization };
125126
list.ForEach(r => { r._client = client; r.Organization = org; });

Git.hub/Git.hub.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="Organization.cs" />
4848
<Compile Include="Properties\AssemblyInfo.cs" />
4949
<Compile Include="PullRequest.cs" />
50+
<Compile Include="RestClientExtensions.cs" />
5051
<Compile Include="util\ReplacingJsonSerializer.cs" />
5152
<Compile Include="Repository.cs" />
5253
<Compile Include="User.cs" />

Git.hub/Issue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public List<IssueComment> GetComments()
2121
request.AddUrlSegment("repo", Repository.Name);
2222
request.AddUrlSegment("issue", Number.ToString());
2323

24-
return _client.Get<List<IssueComment>>(request).Data;
24+
return _client.GetList<IssueComment>(request);
2525
}
2626

2727
public IssueComment CreateComment(string body)

Git.hub/PullRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public List<PullRequestCommit> GetCommits()
8686
request.AddUrlSegment("repo", Repository.Name);
8787
request.AddUrlSegment("pull", Number.ToString());
8888

89-
return _client.Get<List<PullRequestCommit>>(request).Data;
89+
return _client.GetList<PullRequestCommit>(request);
9090
}
9191

9292
public Issue ToIssue()

Git.hub/Repository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public IList<Branch> GetBranches()
8484
request.AddUrlSegment("user", Owner.Login);
8585
request.AddUrlSegment("repo", Name);
8686

87-
return _client.Get<List<Branch>>(request).Data;
87+
return _client.GetList<Branch>(request);
8888
}
8989

9090
/// <summary>
@@ -97,7 +97,7 @@ public IList<PullRequest> GetPullRequests()
9797
request.AddUrlSegment("user", Owner.Login);
9898
request.AddUrlSegment("repo", Name);
9999

100-
var list = _client.Get<List<PullRequest>>(request).Data;
100+
var list = _client.GetList<PullRequest>(request);
101101
if (list == null)
102102
return null;
103103

Git.hub/RestClientExtensions.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace Git.hub
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text.RegularExpressions;
7+
using RestSharp;
8+
9+
internal static class RestClientExtensions
10+
{
11+
private static readonly Regex LinkHeaderFormat = new Regex(@"<(?<Link>[^>]*)>; rel=""(?<Rel>\w*)""", RegexOptions.Compiled);
12+
13+
public static List<T> GetList<T>(this IRestClient client, IRestRequest request)
14+
{
15+
List<T> result = new List<T>();
16+
while (true)
17+
{
18+
IRestResponse<List<T>> pageResponse = client.Get<List<T>>(request);
19+
if (pageResponse.Data == null)
20+
return null;
21+
22+
result.AddRange(pageResponse.Data);
23+
24+
Parameter linkHeader = pageResponse.Headers.FirstOrDefault(i => string.Equals(i.Name, "Link", StringComparison.OrdinalIgnoreCase));
25+
if (linkHeader == null)
26+
break;
27+
28+
bool hasNext = false;
29+
foreach (Match match in LinkHeaderFormat.Matches(linkHeader.Value.ToString()))
30+
{
31+
if (string.Equals(match.Groups["Rel"].Value, "next", StringComparison.OrdinalIgnoreCase))
32+
{
33+
request = new RestRequest(new Uri(match.Groups["Link"].Value));
34+
hasNext = true;
35+
break;
36+
}
37+
}
38+
39+
if (!hasNext)
40+
break;
41+
}
42+
43+
return result;
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)