Skip to content

Commit

Permalink
Merge pull request #107 from msperisen/general-pagination
Browse files Browse the repository at this point in the history
General pagination
  • Loading branch information
kohsuke committed Aug 30, 2014
2 parents 1f298a8 + a58a5b5 commit 92cc81d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ public GHUser getUser(String login) throws IOException {
return u;
}


/**
* clears all cached data in order for external changes (modifications and del
*/
public void refreshCache() {
users.clear();
orgs.clear();
}

/**
* Interns the given {@link GHUser}.
*/
Expand Down Expand Up @@ -357,7 +366,6 @@ public Map<String, Set<GHTeam>> getMyTeams() throws IOException {
* Public events visible to you. Equivalent of what's displayed on https://github.com/
*/
public List<GHEventInfo> getEvents() throws IOException {
// TODO: pagination
GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class);
for (GHEventInfo e : events)
e.wrapUp(this);
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.InterruptedIOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -47,10 +48,14 @@
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import org.apache.commons.io.IOUtils;

import javax.net.ssl.HttpsURLConnection;

/**
* A builder pattern for making HTTP call and parsing its output.
*
Expand Down Expand Up @@ -177,7 +182,26 @@ private <T> T _to(String tailApiUrl, Class<T> type, T instance) throws IOExcepti
buildRequest(uc);

try {
return parse(uc,type,instance);
T result = parse(uc, type, instance);
if (type != null && type.isArray()) { // we might have to loop for pagination - done through recursion
final String links = uc.getHeaderField("link");
if (links != null && links.contains("rel=\"next\"")) {
Pattern nextLinkPattern = Pattern.compile(".*<(.*)>; rel=\"next\"");
Matcher nextLinkMatcher = nextLinkPattern.matcher(links);
if (nextLinkMatcher.find()) {
final String link = nextLinkMatcher.group(1);
T nextResult = _to(link, type, instance);

final int resultLength = Array.getLength(result);
final int nextResultLength = Array.getLength(nextResult);
T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength);
System.arraycopy(result, 0, concatResult, 0, resultLength);
System.arraycopy(nextResult, 0, concatResult, resultLength, nextResultLength);
result = concatResult;
}
}
}
return result;
} catch (IOException e) {
handleApiError(e,uc);
}
Expand Down

0 comments on commit 92cc81d

Please sign in to comment.