From b6520cb6f9195ab005969e232a2913bed17387ca Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 5 Sep 2012 18:55:44 -0700 Subject: [PATCH] got rid of all retrieveXYZ methods in favor of Poster --- .../java/org/kohsuke/github/GHMyself.java | 4 +-- .../org/kohsuke/github/GHOrganization.java | 8 ++--- .../java/org/kohsuke/github/GHPerson.java | 2 +- .../java/org/kohsuke/github/GHRepository.java | 22 ++++++------ src/main/java/org/kohsuke/github/GHTeam.java | 12 +++---- src/main/java/org/kohsuke/github/GHUser.java | 6 ++-- src/main/java/org/kohsuke/github/GitHub.java | 35 ++++--------------- 7 files changed, 33 insertions(+), 56 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHMyself.java b/src/main/java/org/kohsuke/github/GHMyself.java index ddceccd70..8edc66577 100644 --- a/src/main/java/org/kohsuke/github/GHMyself.java +++ b/src/main/java/org/kohsuke/github/GHMyself.java @@ -22,7 +22,7 @@ public class GHMyself extends GHUser { * Always non-null. */ public List getEmails() throws IOException { - String[] addresses = root.retrieveWithAuth("/user/emails", String[].class); + String[] addresses = root.retrieve().withCredential().to("/user/emails", String[].class); return Collections.unmodifiableList(Arrays.asList(addresses)); } @@ -33,7 +33,7 @@ public class GHMyself extends GHUser { * Always non-null. */ public List getPublicKeys() throws IOException { - return Collections.unmodifiableList(Arrays.asList(root.retrieveWithAuth("/user/keys", GHKey[].class))); + return Collections.unmodifiableList(Arrays.asList(root.retrieve().withCredential().to("/user/keys", GHKey[].class))); } // public void addEmails(Collection emails) throws IOException { diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index 21d50102c..d68368e9b 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -42,7 +42,7 @@ public class GHOrganization extends GHPerson { * Teams by their names. */ public Map getTeams() throws IOException { - GHTeam[] teams = root.retrieveWithAuth("/orgs/" + login + "/teams", GHTeam[].class); + GHTeam[] teams = root.retrieve().withCredential().to("/orgs/" + login + "/teams", GHTeam[].class); Map r = new TreeMap(); for (GHTeam t : teams) { r.put(t.getName(),t.wrapUp(this)); @@ -54,7 +54,7 @@ public class GHOrganization extends GHPerson { * Publicizes the membership. */ public void publicize(GHUser u) throws IOException { - root.retrieveWithAuth("/orgs/" + login + "/public_members/" + u.getLogin(), null, "PUT"); + root.retrieve().withCredential().method("PUT").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); } /** @@ -64,7 +64,7 @@ public class GHOrganization extends GHPerson { return new AbstractList() { // these are shallow objects with only some limited values filled out // TODO: it's better to allow objects to fill themselves in later when missing values are requested - final GHUser[] shallow = root.retrieveWithAuth("/orgs/" + login + "/members", GHUser[].class); + final GHUser[] shallow = root.retrieve().withCredential().to("/orgs/" + login + "/members", GHUser[].class); @Override public GHUser get(int index) { @@ -86,7 +86,7 @@ public class GHOrganization extends GHPerson { * Conceals the membership. */ public void conceal(GHUser u) throws IOException { - root.retrieveWithAuth("/orgs/" + login + "/public_members/" + u.getLogin(), null, "DELETE"); + root.retrieve().withCredential().method("DELETE").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); } public enum Permission { ADMIN, PUSH, PULL } diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java index 4811839c0..b6484dcd8 100644 --- a/src/main/java/org/kohsuke/github/GHPerson.java +++ b/src/main/java/org/kohsuke/github/GHPerson.java @@ -85,7 +85,7 @@ public abstract class GHPerson { */ public GHRepository getRepository(String name) throws IOException { try { - return root.retrieveWithAuth("/repos/" + login + '/' + name, GHRepository.class).wrap(root); + return root.retrieve().withCredential().to("/repos/" + login + '/' + name, GHRepository.class).wrap(root); } catch (FileNotFoundException e) { return null; } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 0106deab4..e7ace143c 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -141,7 +141,7 @@ public class GHRepository { } public List getIssues(GHIssueState state) throws IOException { - return Arrays.asList(GHIssue.wrap(root.retrieve("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this)); + return Arrays.asList(GHIssue.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/issues?state=" + state.toString().toLowerCase(), GHIssue[].class), this)); } protected String getOwnerName() { @@ -213,7 +213,7 @@ public class GHRepository { */ @WithBridgeMethods(Set.class) public GHPersonSet getCollaborators() throws IOException { - return new GHPersonSet(GHUser.wrap(root.retrieve("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root)); + return new GHPersonSet(GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root)); } /** @@ -222,7 +222,7 @@ public class GHRepository { */ public Set getCollaboratorNames() throws IOException { Set r = new HashSet(); - for (GHUser u : GHUser.wrap(root.retrieve("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root)) + for (GHUser u : GHUser.wrap(root.retrieve().to("/repos/" + owner.login + "/" + name + "/collaborators", GHUser[].class),root)) r.add(u.login); return r; } @@ -231,7 +231,7 @@ public class GHRepository { * If this repository belongs to an organization, return a set of teams. */ public Set getTeams() throws IOException { - return Collections.unmodifiableSet(new HashSet(Arrays.asList(GHTeam.wrapUp(root.retrieveWithAuth("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login))))); + return Collections.unmodifiableSet(new HashSet(Arrays.asList(GHTeam.wrapUp(root.retrieve().withCredential().to("/repos/" + owner.login + "/" + name + "/teams", GHTeam[].class), root.getOrganization(owner.login))))); } public void addCollaborators(GHUser... users) throws IOException { @@ -352,7 +352,7 @@ public class GHRepository { * Retrieves a specified pull request. */ public GHPullRequest getPullRequest(int i) throws IOException { - return root.retrieveWithAuth("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this); + return root.retrieve().withCredential().to("/repos/" + owner.login + '/' + name + "/pulls/" + i, GHPullRequest.class).wrapUp(this); } /** @@ -386,14 +386,14 @@ public class GHRepository { */ public List getHooks() throws IOException { List list = new ArrayList(Arrays.asList( - root.retrieveWithAuth(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class))); + root.retrieve().withCredential().to(String.format("/repos/%s/%s/hooks", owner.login, name), GHHook[].class))); for (GHHook h : list) h.wrap(this); return list; } public GHHook getHook(int id) throws IOException { - return root.retrieveWithAuth(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this); + return root.retrieve().withCredential().to(String.format("/repos/%s/%s/hooks/%d", owner.login, name, id), GHHook.class).wrap(this); } /** @@ -402,7 +402,7 @@ public class GHRepository { public GHCommit getCommit(String sha1) throws IOException { GHCommit c = commits.get(sha1); if (c==null) { - c = root.retrieve(String.format("/repos/%s/%s/commits/%s", owner.login, name, sha1), GHCommit.class).wrapUp(this); + c = root.retrieve().to(String.format("/repos/%s/%s/commits/%s", owner.login, name, sha1), GHCommit.class).wrapUp(this); commits.put(sha1,c); } return c; @@ -610,7 +610,7 @@ public class GHRepository { */ public Map getBranches() throws IOException { Map r = new TreeMap(); - for (GHBranch p : root.retrieve("/repos/" + owner.login + "/" + name + "/branches", GHBranch[].class)) { + for (GHBranch p : root.retrieve().to("/repos/" + owner.login + "/" + name + "/branches", GHBranch[].class)) { p.wrap(this); r.put(p.getName(),p); } @@ -619,7 +619,7 @@ public class GHRepository { public Map getMilestones() throws IOException { Map milestones = new TreeMap(); - GHMilestone[] ms = root.retrieve("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone[].class); + GHMilestone[] ms = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones", GHMilestone[].class); for (GHMilestone m : ms) { m.owner = this; m.root = root; @@ -631,7 +631,7 @@ public class GHRepository { public GHMilestone getMilestone(int number) throws IOException { GHMilestone m = milestones.get(number); if (m == null) { - m = root.retrieve("/repos/" + owner.login + "/" + name + "/milestones/" + number, GHMilestone.class); + m = root.retrieve().to("/repos/" + owner.login + "/" + name + "/milestones/" + number, GHMilestone.class); m.owner = this; m.root = root; milestones.put(m.getNumber(), m); diff --git a/src/main/java/org/kohsuke/github/GHTeam.java b/src/main/java/org/kohsuke/github/GHTeam.java index d4bf99d58..dca37e331 100644 --- a/src/main/java/org/kohsuke/github/GHTeam.java +++ b/src/main/java/org/kohsuke/github/GHTeam.java @@ -46,11 +46,11 @@ public class GHTeam { * Retrieves the current members. */ public Set getMembers() throws IOException { - return new HashSet(Arrays.asList(GHUser.wrap(org.root.retrieveWithAuth(api("/members"), GHUser[].class), org.root))); + return new HashSet(Arrays.asList(GHUser.wrap(org.root.retrieve().withCredential().to(api("/members"), GHUser[].class), org.root))); } public Map getRepositories() throws IOException { - GHRepository[] repos = org.root.retrieveWithAuth(api("/repos"), GHRepository[].class); + GHRepository[] repos = org.root.retrieve().withCredential().to(api("/repos"), GHRepository[].class); Map m = new TreeMap(); for (GHRepository r : repos) { m.put(r.getName(),r.wrap(org.root)); @@ -62,22 +62,22 @@ public class GHTeam { * Adds a member to the team. */ public void add(GHUser u) throws IOException { - org.root.retrieveWithAuth(api("/members/" + u.getLogin()), null, "PUT"); + org.root.retrieve().withCredential().method("PUT").to(api("/members/" + u.getLogin()), null); } /** * Removes a member to the team. */ public void remove(GHUser u) throws IOException { - org.root.retrieveWithAuth(api("/members/" + u.getLogin()), null, "DELETE"); + org.root.retrieve().withCredential().method("DELETE").to(api("/members/" + u.getLogin()), null); } public void add(GHRepository r) throws IOException { - org.root.retrieveWithAuth(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null, "PUT"); + org.root.retrieve().withCredential().method("PUT").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); } public void remove(GHRepository r) throws IOException { - org.root.retrieveWithAuth(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null, "DELETE"); + org.root.retrieve().withCredential().method("DELETE").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); } private String api(String tail) { diff --git a/src/main/java/org/kohsuke/github/GHUser.java b/src/main/java/org/kohsuke/github/GHUser.java index 373a867c2..074b96093 100644 --- a/src/main/java/org/kohsuke/github/GHUser.java +++ b/src/main/java/org/kohsuke/github/GHUser.java @@ -56,7 +56,7 @@ public class GHUser extends GHPerson { */ @WithBridgeMethods(Set.class) public GHPersonSet getFollows() throws IOException { - GHUser[] followers = root.retrieve("/users/" + login + "/following", GHUser[].class); + GHUser[] followers = root.retrieve().to("/users/" + login + "/following", GHUser[].class); return new GHPersonSet(Arrays.asList(wrap(followers,root))); } @@ -65,7 +65,7 @@ public class GHUser extends GHPerson { */ @WithBridgeMethods(Set.class) public GHPersonSet getFollowers() throws IOException { - GHUser[] followers = root.retrieve("/users/" + login + "/followers", GHUser[].class); + GHUser[] followers = root.retrieve().to("/users/" + login + "/followers", GHUser[].class); return new GHPersonSet(Arrays.asList(wrap(followers,root))); } @@ -82,7 +82,7 @@ public class GHUser extends GHPerson { public GHPersonSet getOrganizations() throws IOException { GHPersonSet orgs = new GHPersonSet(); Set names = new HashSet(); - for (GHOrganization o : root.retrieve("/users/" + login + "/orgs", GHOrganization[].class)) { + for (GHOrganization o : root.retrieve().to("/users/" + login + "/orgs", GHOrganization[].class)) { if (names.add(o.getLogin())) // I've seen some duplicates in the data orgs.add(root.getOrganization(o.getLogin())); } diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index b0d2b60f2..af343875b 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -174,29 +174,6 @@ public class GitHub { return new Poster(this).method("GET"); } - /*package*/ T retrieve(String tailApiUrl, Class type) throws IOException { - return _retrieve(tailApiUrl, type, "GET", false); - } - - /*package*/ T retrieveWithAuth(String tailApiUrl, Class type) throws IOException { - return _retrieve(tailApiUrl, type, "GET", true); - } - - /*package*/ T retrieveWithAuth(String tailApiUrl, Class type, String method) throws IOException { - return _retrieve(tailApiUrl, type, method, true); - } - - private T _retrieve(String tailApiUrl, Class 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 getMyOrganizations() throws IOException { - GHOrganization[] orgs = retrieveWithAuth("/user/orgs", GHOrganization[].class); + GHOrganization[] orgs = retrieve().withCredential().to("/user/orgs", GHOrganization[].class); Map r = new HashMap(); for (GHOrganization o : orgs) { // don't put 'o' into orgs because they are shallow @@ -443,7 +420,7 @@ public class GitHub { */ public List 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;