got rid of all retrieveXYZ methods in favor of Poster

This commit is contained in:
Kohsuke Kawaguchi
2012-09-05 18:55:44 -07:00
parent d6d73f5165
commit b6520cb6f9
7 changed files with 33 additions and 56 deletions

View File

@@ -174,29 +174,6 @@ public class GitHub {
return new Poster(this).method("GET");
}
/*package*/ <T> T retrieve(String tailApiUrl, Class<T> type) throws IOException {
return _retrieve(tailApiUrl, type, "GET", false);
}
/*package*/ <T> T retrieveWithAuth(String tailApiUrl, Class<T> type) throws IOException {
return _retrieve(tailApiUrl, type, "GET", true);
}
/*package*/ <T> T retrieveWithAuth(String tailApiUrl, Class<T> type, String method) throws IOException {
return _retrieve(tailApiUrl, type, method, true);
}
private <T> T _retrieve(String tailApiUrl, Class<T> type, String method, boolean withAuth) throws IOException {
while (true) {// loop while API rate limit is hit
HttpURLConnection uc = setupConnection(method, withAuth, getApiURL(tailApiUrl));
try {
return parse(uc,type);
} catch (IOException e) {
handleApiError(e,uc);
}
}
}
/**
* Loads pagenated resources.
*
@@ -359,7 +336,7 @@ public class GitHub {
* Gets the current rate limit.
*/
public GHRateLimit getRateLimit() throws IOException {
return retrieveWithAuth("/rate_limit", JsonRateLimit.class).rate;
return retrieve().withCredential().to("/rate_limit", JsonRateLimit.class).rate;
}
/**
@@ -369,7 +346,7 @@ public class GitHub {
public GHMyself getMyself() throws IOException {
requireCredential();
GHMyself u = retrieveWithAuth("/user", GHMyself.class);
GHMyself u = retrieve().withCredential().to("/user", GHMyself.class);
u.root = this;
users.put(u.getLogin(), u);
@@ -406,7 +383,7 @@ public class GitHub {
public GHOrganization getOrganization(String name) throws IOException {
GHOrganization o = orgs.get(name);
if (o==null) {
o = retrieve("/orgs/" + name, GHOrganization.class).wrapUp(this);
o = retrieve().to("/orgs/" + name, GHOrganization.class).wrapUp(this);
orgs.put(name,o);
}
return o;
@@ -429,7 +406,7 @@ public class GitHub {
* TODO: make this automatic.
*/
public Map<String, GHOrganization> getMyOrganizations() throws IOException {
GHOrganization[] orgs = retrieveWithAuth("/user/orgs", GHOrganization[].class);
GHOrganization[] orgs = retrieve().withCredential().to("/user/orgs", GHOrganization[].class);
Map<String, GHOrganization> r = new HashMap<String, GHOrganization>();
for (GHOrganization o : orgs) {
// don't put 'o' into orgs because they are shallow
@@ -443,7 +420,7 @@ public class GitHub {
*/
public List<GHEventInfo> getEvents() throws IOException {
// TODO: pagenation
GHEventInfo[] events = retrieve("/events", GHEventInfo[].class);
GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class);
for (GHEventInfo e : events)
e.wrapUp(this);
return Arrays.asList(events);
@@ -480,7 +457,7 @@ public class GitHub {
*/
public boolean isCredentialValid() throws IOException {
try {
retrieveWithAuth("/user", GHUser.class);
retrieve().withCredential().to("/user", GHUser.class);
return true;
} catch (IOException e) {
return false;