diff --git a/src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java b/src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java index 29830425ba..dcd25cea6c 100644 --- a/src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java +++ b/src/main/java/org/kohsuke/github/GHAppCreateTokenBuilder.java @@ -23,7 +23,7 @@ public class GHAppCreateTokenBuilder { GHAppCreateTokenBuilder(GitHub root, String apiUrlTail, Map permissions) { this.root = root; this.apiUrlTail = apiUrlTail; - this.builder = new Requester(root); + this.builder = root.retrieve().method("POST"); withPermissions(builder, permissions); } diff --git a/src/main/java/org/kohsuke/github/GHAsset.java b/src/main/java/org/kohsuke/github/GHAsset.java index 0003c9703d..757abe3da2 100644 --- a/src/main/java/org/kohsuke/github/GHAsset.java +++ b/src/main/java/org/kohsuke/github/GHAsset.java @@ -135,7 +135,7 @@ public String getBrowserDownloadUrl() { } private void edit(String key, Object value) throws IOException { - new Requester(root).with(key, value).method("PATCH").to(getApiRoute()); + root.retrieve().method("POST").with(key, value).method("PATCH").to(getApiRoute()); } /** @@ -145,7 +145,7 @@ private void edit(String key, Object value) throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(root).method("DELETE").to(getApiRoute()); + root.retrieve().method("DELETE").to(getApiRoute()); } private String getApiRoute() { diff --git a/src/main/java/org/kohsuke/github/GHBlobBuilder.java b/src/main/java/org/kohsuke/github/GHBlobBuilder.java index 6326525dc3..c59fae9436 100644 --- a/src/main/java/org/kohsuke/github/GHBlobBuilder.java +++ b/src/main/java/org/kohsuke/github/GHBlobBuilder.java @@ -13,7 +13,7 @@ public class GHBlobBuilder { GHBlobBuilder(GHRepository repo) { this.repo = repo; - req = new Requester(repo.root); + req = repo.root.retrieve().method("POST"); } /** diff --git a/src/main/java/org/kohsuke/github/GHBranch.java b/src/main/java/org/kohsuke/github/GHBranch.java index 7e1ff1a4f7..8146162071 100644 --- a/src/main/java/org/kohsuke/github/GHBranch.java +++ b/src/main/java/org/kohsuke/github/GHBranch.java @@ -9,8 +9,6 @@ import java.util.Collection; import java.util.Objects; -import static org.kohsuke.github.Previews.*; - /** * A branch in a repository. * @@ -122,7 +120,7 @@ public String getSHA1() { * if disabling protection fails */ public void disableProtection() throws IOException { - new Requester(root).method("DELETE").to(protection_url); + root.retrieve().method("DELETE").to(protection_url); } /** diff --git a/src/main/java/org/kohsuke/github/GHBranchProtection.java b/src/main/java/org/kohsuke/github/GHBranchProtection.java index 4c9246d9de..cbfcc1c4f6 100644 --- a/src/main/java/org/kohsuke/github/GHBranchProtection.java +++ b/src/main/java/org/kohsuke/github/GHBranchProtection.java @@ -123,7 +123,7 @@ GHBranchProtection wrap(GHBranch branch) { } private Requester requester() { - return new Requester(root).withPreview(ZZZAX); + return root.retrieve().method("POST").withPreview(ZZZAX); } /** diff --git a/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java b/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java index c94020b306..cfa06dc7c3 100644 --- a/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java +++ b/src/main/java/org/kohsuke/github/GHBranchProtectionBuilder.java @@ -352,7 +352,7 @@ private StatusChecks getStatusChecks() { } private Requester requester() { - return new Requester(branch.getRoot()).withPreview(LUKE_CAGE); + return branch.getRoot().retrieve().method("POST").withPreview(LUKE_CAGE); } private static class Restrictions { diff --git a/src/main/java/org/kohsuke/github/GHCommit.java b/src/main/java/org/kohsuke/github/GHCommit.java index b1e7b7dc06..e36396c0a6 100644 --- a/src/main/java/org/kohsuke/github/GHCommit.java +++ b/src/main/java/org/kohsuke/github/GHCommit.java @@ -466,7 +466,9 @@ public PagedIterable listComments() { * if comment is not created */ public GHCommitComment createComment(String body, String path, Integer line, Integer position) throws IOException { - GHCommitComment r = new Requester(owner.root).with("body", body) + GHCommitComment r = owner.root.retrieve() + .method("POST") + .with("body", body) .with("path", path) .with("line", line) .with("position", position) diff --git a/src/main/java/org/kohsuke/github/GHCommitBuilder.java b/src/main/java/org/kohsuke/github/GHCommitBuilder.java index 71335ee02a..c25fe0403d 100644 --- a/src/main/java/org/kohsuke/github/GHCommitBuilder.java +++ b/src/main/java/org/kohsuke/github/GHCommitBuilder.java @@ -34,7 +34,7 @@ private UserInfo(String name, String email, Date date) { GHCommitBuilder(GHRepository repo) { this.repo = repo; - req = new Requester(repo.root); + req = repo.root.retrieve().method("POST"); } /** diff --git a/src/main/java/org/kohsuke/github/GHCommitComment.java b/src/main/java/org/kohsuke/github/GHCommitComment.java index e53e8607f0..4e974a19a3 100644 --- a/src/main/java/org/kohsuke/github/GHCommitComment.java +++ b/src/main/java/org/kohsuke/github/GHCommitComment.java @@ -113,14 +113,16 @@ public GHCommit getCommit() throws IOException { * the io exception */ public void update(String body) throws IOException { - new Requester(owner.root).with("body", body).method("PATCH").to(getApiTail(), GHCommitComment.class); + owner.root.retrieve().method("POST").with("body", body).method("PATCH").to(getApiTail(), GHCommitComment.class); this.body = body; } @Preview @Deprecated public GHReaction createReaction(ReactionContent content) throws IOException { - return new Requester(owner.root).withPreview(SQUIRREL_GIRL) + return owner.root.retrieve() + .method("POST") + .withPreview(SQUIRREL_GIRL) .with("content", content.getContent()) .to(getApiTail() + "/reactions", GHReaction.class) .wrap(owner.root); @@ -141,7 +143,7 @@ public PagedIterable listReactions() { * the io exception */ public void delete() throws IOException { - new Requester(owner.root).method("DELETE").to(getApiTail()); + owner.root.retrieve().method("DELETE").to(getApiTail()); } private String getApiTail() { diff --git a/src/main/java/org/kohsuke/github/GHContent.java b/src/main/java/org/kohsuke/github/GHContent.java index 8683e6c2fb..b8e55f0ad1 100644 --- a/src/main/java/org/kohsuke/github/GHContent.java +++ b/src/main/java/org/kohsuke/github/GHContent.java @@ -306,7 +306,9 @@ public GHContentUpdateResponse update(byte[] newContentBytes, String commitMessa throws IOException { String encodedContent = Base64.encodeBase64String(newContentBytes); - Requester requester = new Requester(root).with("path", path) + Requester requester = root.retrieve() + .method("POST") + .with("path", path) .with("message", commitMessage) .with("sha", sha) .with("content", encodedContent) @@ -350,7 +352,9 @@ public GHContentUpdateResponse delete(String message) throws IOException { * the io exception */ public GHContentUpdateResponse delete(String commitMessage, String branch) throws IOException { - Requester requester = new Requester(root).with("path", path) + Requester requester = root.retrieve() + .method("POST") + .with("path", path) .with("message", commitMessage) .with("sha", sha) .method("DELETE"); diff --git a/src/main/java/org/kohsuke/github/GHContentBuilder.java b/src/main/java/org/kohsuke/github/GHContentBuilder.java index b50a7e257b..101289eb0f 100644 --- a/src/main/java/org/kohsuke/github/GHContentBuilder.java +++ b/src/main/java/org/kohsuke/github/GHContentBuilder.java @@ -21,7 +21,7 @@ public final class GHContentBuilder { GHContentBuilder(GHRepository repo) { this.repo = repo; - this.req = new Requester(repo.root).method("PUT"); + this.req = repo.root.retrieve().method("PUT"); } /** diff --git a/src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java b/src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java index 268bfa8ef3..c1d22ec2d9 100644 --- a/src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java +++ b/src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java @@ -16,7 +16,7 @@ public class GHCreateRepositoryBuilder { GHCreateRepositoryBuilder(GitHub root, String apiUrlTail, String name) { this.root = root; this.apiUrlTail = apiUrlTail; - this.builder = new Requester(root); + this.builder = root.retrieve().method("POST"); this.builder.with("name", name); } diff --git a/src/main/java/org/kohsuke/github/GHDeployKey.java b/src/main/java/org/kohsuke/github/GHDeployKey.java index f9bd1acd52..0835ba3178 100644 --- a/src/main/java/org/kohsuke/github/GHDeployKey.java +++ b/src/main/java/org/kohsuke/github/GHDeployKey.java @@ -82,7 +82,8 @@ public String toString() { * the io exception */ public void delete() throws IOException { - new Requester(owner.root).method("DELETE") + owner.root.retrieve() + .method("DELETE") .to(String.format("/repos/%s/%s/keys/%d", owner.getOwnerName(), owner.getName(), id)); } } diff --git a/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java b/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java index c041538069..07031d7e6c 100644 --- a/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java +++ b/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java @@ -19,7 +19,7 @@ public class GHDeploymentBuilder { */ public GHDeploymentBuilder(GHRepository repo) { this.repo = repo; - this.builder = new Requester(repo.root); + this.builder = repo.root.retrieve().method("POST"); } /** diff --git a/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java b/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java index 31e54e64b1..d2f79c9609 100644 --- a/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java +++ b/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java @@ -30,7 +30,7 @@ public GHDeploymentStatusBuilder(GHRepository repo, int deploymentId, GHDeployme GHDeploymentStatusBuilder(GHRepository repo, long deploymentId, GHDeploymentState state) { this.repo = repo; this.deploymentId = deploymentId; - this.builder = new Requester(repo.root); + this.builder = repo.root.retrieve().method("POST"); this.builder.with("state", state); } diff --git a/src/main/java/org/kohsuke/github/GHGist.java b/src/main/java/org/kohsuke/github/GHGist.java index d772d3dd77..b1cfc1d076 100644 --- a/src/main/java/org/kohsuke/github/GHGist.java +++ b/src/main/java/org/kohsuke/github/GHGist.java @@ -182,7 +182,7 @@ String getApiTailUrl(String tail) { * the io exception */ public void star() throws IOException { - new Requester(root).method("PUT").to(getApiTailUrl("star")); + root.retrieve().method("PUT").to(getApiTailUrl("star")); } /** @@ -192,7 +192,7 @@ public void star() throws IOException { * the io exception */ public void unstar() throws IOException { - new Requester(root).method("DELETE").to(getApiTailUrl("star")); + root.retrieve().method("DELETE").to(getApiTailUrl("star")); } /** @@ -214,7 +214,7 @@ public boolean isStarred() throws IOException { * the io exception */ public GHGist fork() throws IOException { - return new Requester(root).to(getApiTailUrl("forks"), GHGist.class).wrapUp(root); + return root.retrieve().method("POST").to(getApiTailUrl("forks"), GHGist.class).wrapUp(root); } /** @@ -233,7 +233,7 @@ public PagedIterable listForks() { * the io exception */ public void delete() throws IOException { - new Requester(root).method("DELETE").to("/gists/" + id); + root.retrieve().method("DELETE").to("/gists/" + id); } /** diff --git a/src/main/java/org/kohsuke/github/GHGistBuilder.java b/src/main/java/org/kohsuke/github/GHGistBuilder.java index b0d572038e..5d7eaa6f39 100644 --- a/src/main/java/org/kohsuke/github/GHGistBuilder.java +++ b/src/main/java/org/kohsuke/github/GHGistBuilder.java @@ -23,7 +23,7 @@ public class GHGistBuilder { */ public GHGistBuilder(GitHub root) { this.root = root; - req = new Requester(root); + req = root.retrieve().method("POST"); } /** diff --git a/src/main/java/org/kohsuke/github/GHGistUpdater.java b/src/main/java/org/kohsuke/github/GHGistUpdater.java index af5a4876a1..3169da2607 100644 --- a/src/main/java/org/kohsuke/github/GHGistUpdater.java +++ b/src/main/java/org/kohsuke/github/GHGistUpdater.java @@ -16,7 +16,7 @@ public class GHGistUpdater { GHGistUpdater(GHGist base) { this.base = base; - this.builder = new Requester(base.root); + this.builder = base.root.retrieve().method("POST"); files = new LinkedHashMap<>(); } diff --git a/src/main/java/org/kohsuke/github/GHHook.java b/src/main/java/org/kohsuke/github/GHHook.java index a88c9a372c..5b93c238a3 100644 --- a/src/main/java/org/kohsuke/github/GHHook.java +++ b/src/main/java/org/kohsuke/github/GHHook.java @@ -74,7 +74,7 @@ public Map getConfig() { * @see Ping hook */ public void ping() throws IOException { - new Requester(getRoot()).method("POST").to(getApiRoute() + "/pings"); + getRoot().retrieve().method("POST").to(getApiRoute() + "/pings"); } /** @@ -84,7 +84,7 @@ public void ping() throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(getRoot()).method("DELETE").to(getApiRoute()); + getRoot().retrieve().method("DELETE").to(getApiRoute()); } /** diff --git a/src/main/java/org/kohsuke/github/GHHooks.java b/src/main/java/org/kohsuke/github/GHHooks.java index 7f99de7a77..006774e45f 100644 --- a/src/main/java/org/kohsuke/github/GHHooks.java +++ b/src/main/java/org/kohsuke/github/GHHooks.java @@ -74,7 +74,9 @@ public GHHook createHook(String name, Map config, Collection listComments() throws IOException { @Preview @Deprecated public GHReaction createReaction(ReactionContent content) throws IOException { - return new Requester(owner.root).withPreview(SQUIRREL_GIRL) + return owner.root.retrieve() + .method("POST") + .withPreview(SQUIRREL_GIRL) .with("content", content.getContent()) .to(getApiRoute() + "/reactions", GHReaction.class) .wrap(root); @@ -513,7 +517,7 @@ public void setAssignees(GHUser... assignees) throws IOException { * the io exception */ public void setAssignees(Collection assignees) throws IOException { - new Requester(root).with(ASSIGNEES, getLogins(assignees)).method("PATCH").to(getIssuesApiRoute()); + root.retrieve().method("POST").with(ASSIGNEES, getLogins(assignees)).method("PATCH").to(getIssuesApiRoute()); } /** diff --git a/src/main/java/org/kohsuke/github/GHIssueBuilder.java b/src/main/java/org/kohsuke/github/GHIssueBuilder.java index 766449596e..cf65eee147 100644 --- a/src/main/java/org/kohsuke/github/GHIssueBuilder.java +++ b/src/main/java/org/kohsuke/github/GHIssueBuilder.java @@ -17,7 +17,7 @@ public class GHIssueBuilder { GHIssueBuilder(GHRepository repo, String title) { this.repo = repo; - this.builder = new Requester(repo.root); + this.builder = repo.root.retrieve().method("POST"); builder.with("title", title); } diff --git a/src/main/java/org/kohsuke/github/GHIssueComment.java b/src/main/java/org/kohsuke/github/GHIssueComment.java index 9c2f1ae587..dba4fd889a 100644 --- a/src/main/java/org/kohsuke/github/GHIssueComment.java +++ b/src/main/java/org/kohsuke/github/GHIssueComment.java @@ -108,7 +108,7 @@ public GHCommentAuthorAssociation getAuthorAssociation() { * the io exception */ public void update(String body) throws IOException { - new Requester(owner.root).with("body", body).method("PATCH").to(getApiRoute(), GHIssueComment.class); + owner.root.retrieve().method("POST").with("body", body).method("PATCH").to(getApiRoute(), GHIssueComment.class); this.body = body; } @@ -119,13 +119,15 @@ public void update(String body) throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(owner.root).method("DELETE").to(getApiRoute()); + owner.root.retrieve().method("DELETE").to(getApiRoute()); } @Preview @Deprecated public GHReaction createReaction(ReactionContent content) throws IOException { - return new Requester(owner.root).withPreview(SQUIRREL_GIRL) + return owner.root.retrieve() + .method("POST") + .withPreview(SQUIRREL_GIRL) .with("content", content.getContent()) .to(getApiRoute() + "/reactions", GHReaction.class) .wrap(owner.root); diff --git a/src/main/java/org/kohsuke/github/GHMilestone.java b/src/main/java/org/kohsuke/github/GHMilestone.java index 2126869cbb..b52b1538af 100644 --- a/src/main/java/org/kohsuke/github/GHMilestone.java +++ b/src/main/java/org/kohsuke/github/GHMilestone.java @@ -159,7 +159,7 @@ public void delete() throws IOException { } private void edit(String key, Object value) throws IOException { - new Requester(root).with(key, value).method("PATCH").to(getApiRoute()); + root.retrieve().method("POST").with(key, value).method("PATCH").to(getApiRoute()); } /** diff --git a/src/main/java/org/kohsuke/github/GHMyself.java b/src/main/java/org/kohsuke/github/GHMyself.java index 0d16f77d4b..e80ba1c592 100644 --- a/src/main/java/org/kohsuke/github/GHMyself.java +++ b/src/main/java/org/kohsuke/github/GHMyself.java @@ -71,7 +71,7 @@ public List getEmails() throws IOException { * the io exception */ public List getEmails2() throws IOException { - GHEmail[] addresses = root.retrieve().to("/user/emails", GHEmail[].class); + GHEmail[] addresses = root.retrieve().toArray("/user/emails", GHEmail[].class); return Collections.unmodifiableList(Arrays.asList(addresses)); } @@ -86,7 +86,7 @@ public List getEmails2() throws IOException { * the io exception */ public List getPublicKeys() throws IOException { - return Collections.unmodifiableList(Arrays.asList(root.retrieve().to("/user/keys", GHKey[].class))); + return Collections.unmodifiableList(Arrays.asList(root.retrieve().toArray("/user/keys", GHKey[].class))); } /** @@ -101,7 +101,7 @@ public List getPublicKeys() throws IOException { */ public List getPublicVerifiedKeys() throws IOException { return Collections.unmodifiableList( - Arrays.asList(root.retrieve().to("/users/" + getLogin() + "/keys", GHVerifiedKey[].class))); + Arrays.asList(root.retrieve().toArray("/users/" + getLogin() + "/keys", GHVerifiedKey[].class))); } /** @@ -114,7 +114,7 @@ public List getPublicVerifiedKeys() throws IOException { public GHPersonSet getAllOrganizations() throws IOException { GHPersonSet orgs = new GHPersonSet(); Set names = new HashSet(); - for (GHOrganization o : root.retrieve().to("/user/orgs", GHOrganization[].class)) { + for (GHOrganization o : root.retrieve().toArray("/user/orgs", GHOrganization[].class)) { if (names.add(o.getLogin())) // in case of rumoured duplicates in the data orgs.add(root.getOrganization(o.getLogin())); } diff --git a/src/main/java/org/kohsuke/github/GHNotificationStream.java b/src/main/java/org/kohsuke/github/GHNotificationStream.java index 9d62480651..26d842aaa9 100644 --- a/src/main/java/org/kohsuke/github/GHNotificationStream.java +++ b/src/main/java/org/kohsuke/github/GHNotificationStream.java @@ -101,7 +101,8 @@ public GHNotificationStream nonBlocking(boolean v) { */ public Iterator iterator() { // capture the configuration setting here - final Requester req = new Requester(root).method("GET") + final Requester req = root.retrieve() + .method("GET") .with("all", all) .with("participating", participating) .with("since", since); @@ -180,7 +181,7 @@ GHThread fetch() { req.setHeader("If-Modified-Since", lastModified); - threads = req.to(apiUrl, GHThread[].class); + threads = req.toArray(apiUrl, GHThread[].class); if (threads == null) { threads = EMPTY_ARRAY; // if unmodified, we get empty array } else { @@ -232,7 +233,7 @@ public void markAsRead() throws IOException { * the io exception */ public void markAsRead(long timestamp) throws IOException { - final Requester req = new Requester(root).method("PUT"); + final Requester req = root.retrieve().method("PUT"); if (timestamp >= 0) req.with("last_read_at", GitHub.printDate(new Date(timestamp))); req.asHttpStatusCode(apiUrl); diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index 62b38dbe35..a0ae6a3573 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -243,7 +243,7 @@ public boolean hasPublicMember(GHUser user) { * the io exception */ public void publicize(GHUser u) throws IOException { - root.retrieve().method("PUT").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); + root.retrieve().method("PUT").to("/orgs/" + login + "/public_members/" + u.getLogin()); } /** @@ -314,7 +314,7 @@ private PagedIterable listMembers(final String suffix, final String filt * the io exception */ public void conceal(GHUser u) throws IOException { - root.retrieve().method("DELETE").to("/orgs/" + login + "/public_members/" + u.getLogin(), null); + root.retrieve().method("DELETE").to("/orgs/" + login + "/public_members/" + u.getLogin()); } /** @@ -389,7 +389,7 @@ public enum Permission { */ @Deprecated public GHTeam createTeam(String name, Permission p, Collection repositories) throws IOException { - Requester post = new Requester(root).with("name", name).with("permission", p); + Requester post = root.retrieve().method("POST").with("name", name).with("permission", p); List repo_names = new ArrayList(); for (GHRepository r : repositories) { repo_names.add(login + "/" + r.getName()); @@ -430,7 +430,7 @@ public GHTeam createTeam(String name, Permission p, GHRepository... repositories * the io exception */ public GHTeam createTeam(String name, Collection repositories) throws IOException { - Requester post = new Requester(root).with("name", name); + Requester post = root.retrieve().method("POST").with("name", name); List repo_names = new ArrayList(); for (GHRepository r : repositories) { repo_names.add(login + "/" + r.getName()); diff --git a/src/main/java/org/kohsuke/github/GHProject.java b/src/main/java/org/kohsuke/github/GHProject.java index 03807b795c..baa5acd2f6 100644 --- a/src/main/java/org/kohsuke/github/GHProject.java +++ b/src/main/java/org/kohsuke/github/GHProject.java @@ -176,7 +176,7 @@ public GHProject wrap(GitHub root) { } private void edit(String key, Object value) throws IOException { - new Requester(root).withPreview(INERTIA).with(key, value).method("PATCH").to(getApiRoute()); + root.retrieve().method("POST").withPreview(INERTIA).with(key, value).method("PATCH").to(getApiRoute()); } /** @@ -270,7 +270,7 @@ public void setPublic(boolean isPublic) throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(root).withPreview(INERTIA).method("DELETE").to(getApiRoute()); + root.retrieve().method("POST").withPreview(INERTIA).method("DELETE").to(getApiRoute()); } /** diff --git a/src/main/java/org/kohsuke/github/GHProjectCard.java b/src/main/java/org/kohsuke/github/GHProjectCard.java index 2ed518fcce..8aa7405cfa 100644 --- a/src/main/java/org/kohsuke/github/GHProjectCard.java +++ b/src/main/java/org/kohsuke/github/GHProjectCard.java @@ -198,7 +198,7 @@ public void setArchived(boolean archived) throws IOException { } private void edit(String key, Object value) throws IOException { - new Requester(root).withPreview(INERTIA).with(key, value).method("PATCH").to(getApiRoute()); + root.retrieve().method("POST").withPreview(INERTIA).with(key, value).method("PATCH").to(getApiRoute()); } /** @@ -217,6 +217,6 @@ protected String getApiRoute() { * the io exception */ public void delete() throws IOException { - new Requester(root).withPreview(INERTIA).method("DELETE").to(getApiRoute()); + root.retrieve().method("POST").withPreview(INERTIA).method("DELETE").to(getApiRoute()); } } diff --git a/src/main/java/org/kohsuke/github/GHProjectColumn.java b/src/main/java/org/kohsuke/github/GHProjectColumn.java index b93f709c36..0bbca4cd96 100644 --- a/src/main/java/org/kohsuke/github/GHProjectColumn.java +++ b/src/main/java/org/kohsuke/github/GHProjectColumn.java @@ -106,7 +106,7 @@ public void setName(String name) throws IOException { } private void edit(String key, Object value) throws IOException { - new Requester(root).withPreview(INERTIA).with(key, value).method("PATCH").to(getApiRoute()); + root.retrieve().method("POST").withPreview(INERTIA).with(key, value).method("PATCH").to(getApiRoute()); } /** @@ -125,7 +125,7 @@ protected String getApiRoute() { * the io exception */ public void delete() throws IOException { - new Requester(root).withPreview(INERTIA).method("DELETE").to(getApiRoute()); + root.retrieve().method("POST").withPreview(INERTIA).method("DELETE").to(getApiRoute()); } /** diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index f8a9bb4bc7..c2763695ec 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -505,7 +505,8 @@ public GHPullRequestReviewBuilder createReview() { */ public GHPullRequestReviewComment createReviewComment(String body, String sha, String path, int position) throws IOException { - return new Requester(root).method("POST") + return root.retrieve() + .method("POST") .with("body", body) .with("commit_id", sha) .with("path", path) @@ -523,9 +524,7 @@ public GHPullRequestReviewComment createReviewComment(String body, String sha, S * the io exception */ public void requestReviewers(List reviewers) throws IOException { - new Requester(root).method("POST") - .with("reviewers", getLogins(reviewers)) - .to(getApiRoute() + REQUEST_REVIEWERS); + root.retrieve().method("POST").with("reviewers", getLogins(reviewers)).to(getApiRoute() + REQUEST_REVIEWERS); } /** @@ -541,7 +540,7 @@ public void requestTeamReviewers(List teams) throws IOException { for (GHTeam team : teams) { teamReviewers.add(team.getSlug()); } - new Requester(root).method("POST").with("team_reviewers", teamReviewers).to(getApiRoute() + REQUEST_REVIEWERS); + root.retrieve().method("POST").with("team_reviewers", teamReviewers).to(getApiRoute() + REQUEST_REVIEWERS); } /** @@ -589,7 +588,8 @@ public void merge(String msg, String sha) throws IOException { * the io exception */ public void merge(String msg, String sha, MergeMethod method) throws IOException { - new Requester(root).method("PUT") + root.retrieve() + .method("PUT") .with("commit_message", msg) .with("sha", sha) .with("merge_method", method) @@ -605,7 +605,7 @@ public enum MergeMethod { private void fetchIssue() throws IOException { if (!fetchedIssueDetails) { - new Requester(root).method("GET").to(getIssuesApiRoute(), this); + root.retrieve().method("GET").to(getIssuesApiRoute(), this); fetchedIssueDetails = true; } } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReview.java b/src/main/java/org/kohsuke/github/GHPullRequestReview.java index 65a29d33a7..75b257cf6c 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReview.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReview.java @@ -160,7 +160,8 @@ public void submit(String body, GHPullRequestReviewState state) throws IOExcepti * the io exception */ public void submit(String body, GHPullRequestReviewEvent event) throws IOException { - new Requester(owner.root).method("POST") + owner.root.retrieve() + .method("POST") .with("body", body) .with("event", event.action()) .to(getApiRoute() + "/events", this); @@ -175,7 +176,7 @@ public void submit(String body, GHPullRequestReviewEvent event) throws IOExcepti * the io exception */ public void delete() throws IOException { - new Requester(owner.root).method("DELETE").to(getApiRoute()); + owner.root.retrieve().method("DELETE").to(getApiRoute()); } /** @@ -187,7 +188,7 @@ public void delete() throws IOException { * the io exception */ public void dismiss(String message) throws IOException { - new Requester(owner.root).method("PUT").with("message", message).to(getApiRoute() + "/dismissals"); + owner.root.retrieve().method("PUT").with("message", message).to(getApiRoute() + "/dismissals"); state = GHPullRequestReviewState.DISMISSED; } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewBuilder.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewBuilder.java index cf0bf2171d..be22a98620 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReviewBuilder.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewBuilder.java @@ -17,7 +17,7 @@ public class GHPullRequestReviewBuilder { GHPullRequestReviewBuilder(GHPullRequest pr) { this.pr = pr; - this.builder = new Requester(pr.root); + this.builder = pr.root.retrieve().method("POST"); } // public GHPullRequestReview createReview(@Nullable String commitId, String body, GHPullRequestReviewEvent event, diff --git a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java index 74f9dd8d02..779c1952fc 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java +++ b/src/main/java/org/kohsuke/github/GHPullRequestReviewComment.java @@ -163,7 +163,7 @@ protected String getApiRoute() { * the io exception */ public void update(String body) throws IOException { - new Requester(owner.root).method("PATCH").with("body", body).to(getApiRoute(), this); + owner.root.retrieve().method("PATCH").with("body", body).to(getApiRoute(), this); this.body = body; } @@ -174,7 +174,7 @@ public void update(String body) throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(owner.root).method("DELETE").to(getApiRoute()); + owner.root.retrieve().method("DELETE").to(getApiRoute()); } /** @@ -187,7 +187,8 @@ public void delete() throws IOException { * the io exception */ public GHPullRequestReviewComment reply(String body) throws IOException { - return new Requester(owner.root).method("POST") + return owner.root.retrieve() + .method("POST") .with("body", body) .with("in_reply_to", getId()) .to(getApiRoute() + "/comments", GHPullRequestReviewComment.class) @@ -197,7 +198,9 @@ public GHPullRequestReviewComment reply(String body) throws IOException { @Preview @Deprecated public GHReaction createReaction(ReactionContent content) throws IOException { - return new Requester(owner.root).withPreview(SQUIRREL_GIRL) + return owner.root.retrieve() + .method("POST") + .withPreview(SQUIRREL_GIRL) .with("content", content.getContent()) .to(getApiRoute() + "/reactions", GHReaction.class) .wrap(owner.root); diff --git a/src/main/java/org/kohsuke/github/GHReaction.java b/src/main/java/org/kohsuke/github/GHReaction.java index ced6792b41..8400cd7b32 100644 --- a/src/main/java/org/kohsuke/github/GHReaction.java +++ b/src/main/java/org/kohsuke/github/GHReaction.java @@ -58,6 +58,6 @@ public URL getHtmlUrl() { * the io exception */ public void delete() throws IOException { - new Requester(root).method("DELETE").withPreview(SQUIRREL_GIRL).to("/reactions/" + id); + root.retrieve().method("DELETE").withPreview(SQUIRREL_GIRL).to("/reactions/" + id); } } diff --git a/src/main/java/org/kohsuke/github/GHRef.java b/src/main/java/org/kohsuke/github/GHRef.java index c450aae080..3a086a162a 100644 --- a/src/main/java/org/kohsuke/github/GHRef.java +++ b/src/main/java/org/kohsuke/github/GHRef.java @@ -66,7 +66,13 @@ public void updateTo(String sha) throws IOException { * the io exception */ public void updateTo(String sha, Boolean force) throws IOException { - new Requester(root).with("sha", sha).with("force", force).method("PATCH").to(url, GHRef.class).wrap(root); + root.retrieve() + .method("POST") + .with("sha", sha) + .with("force", force) + .method("PATCH") + .to(url, GHRef.class) + .wrap(root); } /** @@ -76,7 +82,7 @@ public void updateTo(String sha, Boolean force) throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(root).method("DELETE").to(url); + root.retrieve().method("DELETE").to(url); } GHRef wrap(GitHub root) { diff --git a/src/main/java/org/kohsuke/github/GHRelease.java b/src/main/java/org/kohsuke/github/GHRelease.java index 3955693071..fbe2851a70 100644 --- a/src/main/java/org/kohsuke/github/GHRelease.java +++ b/src/main/java/org/kohsuke/github/GHRelease.java @@ -240,7 +240,7 @@ public GHAsset uploadAsset(File file, String contentType) throws IOException { * the io exception */ public GHAsset uploadAsset(String filename, InputStream stream, String contentType) throws IOException { - Requester builder = new Requester(owner.root); + Requester builder = owner.root.retrieve().method("POST"); String url = getUploadUrl(); // strip the helpful garbage from the url url = url.substring(0, url.indexOf('{')); @@ -256,9 +256,9 @@ public GHAsset uploadAsset(String filename, InputStream stream, String contentTy * the io exception */ public List getAssets() throws IOException { - Requester builder = new Requester(owner.root); + Requester builder = owner.root.retrieve().method("POST"); - GHAsset[] assets = builder.method("GET").to(getApiTailUrl("assets"), GHAsset[].class); + GHAsset[] assets = builder.method("GET").toArray(getApiTailUrl("assets"), GHAsset[].class); return Arrays.asList(GHAsset.wrap(assets, this)); } @@ -269,7 +269,7 @@ public List getAssets() throws IOException { * the io exception */ public void delete() throws IOException { - new Requester(root).method("DELETE").to(owner.getApiTailUrl("releases/" + id)); + root.retrieve().method("DELETE").to(owner.getApiTailUrl("releases/" + id)); } /** diff --git a/src/main/java/org/kohsuke/github/GHReleaseBuilder.java b/src/main/java/org/kohsuke/github/GHReleaseBuilder.java index 19dbafd6bf..2c23ecb879 100644 --- a/src/main/java/org/kohsuke/github/GHReleaseBuilder.java +++ b/src/main/java/org/kohsuke/github/GHReleaseBuilder.java @@ -21,7 +21,7 @@ public class GHReleaseBuilder { */ public GHReleaseBuilder(GHRepository ghRepository, String tag) { this.repo = ghRepository; - this.builder = new Requester(repo.root); + this.builder = repo.root.retrieve().method("POST"); builder.with("tag_name", tag); } diff --git a/src/main/java/org/kohsuke/github/GHReleaseUpdater.java b/src/main/java/org/kohsuke/github/GHReleaseUpdater.java index 3c52eb0864..fc436bdd6e 100644 --- a/src/main/java/org/kohsuke/github/GHReleaseUpdater.java +++ b/src/main/java/org/kohsuke/github/GHReleaseUpdater.java @@ -14,7 +14,7 @@ public class GHReleaseUpdater { GHReleaseUpdater(GHRelease base) { this.base = base; - this.builder = new Requester(base.root); + this.builder = base.root.retrieve().method("POST"); } /** diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 932e769ad6..d5f1e7cdcc 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -387,7 +387,7 @@ public List getIssues(GHIssueState state, GHMilestone milestone) throws return Arrays.asList(GHIssue.wrap(root.retrieve() .with("state", state) .with("milestone", milestone == null ? "none" : "" + milestone.getNumber()) - .to(getApiTailUrl("issues"), GHIssue[].class), this)); + .toArray(getApiTailUrl("issues"), GHIssue[].class), this)); } /** @@ -427,7 +427,9 @@ public GHReleaseBuilder createRelease(String tag) { * the io exception */ public GHRef createRef(String name, String sha) throws IOException { - return new Requester(root).with("ref", name) + return root.retrieve() + .method("POST") + .with("ref", name) .with("sha", sha) .method("POST") .to(getApiTailUrl("git/refs"), GHRef.class) @@ -786,7 +788,7 @@ public boolean hasAssignee(GHUser u) throws IOException { */ public Set getCollaboratorNames() throws IOException { Set r = new HashSet(); - for (GHUser u : GHUser.wrap(root.retrieve().to(getApiTailUrl("collaborators"), GHUser[].class), root)) + for (GHUser u : GHUser.wrap(root.retrieve().toArray(getApiTailUrl("collaborators"), GHUser[].class), root)) r.add(u.login); return r; } @@ -829,7 +831,7 @@ public GHPermissionType getPermission(GHUser u) throws IOException { */ public Set getTeams() throws IOException { return Collections.unmodifiableSet(new HashSet( - Arrays.asList(GHTeam.wrapUp(root.retrieve().to(getApiTailUrl("teams"), GHTeam[].class), + Arrays.asList(GHTeam.wrapUp(root.retrieve().toArray(getApiTailUrl("teams"), GHTeam[].class), root.getOrganization(getOwnerName()))))); } @@ -883,7 +885,7 @@ public void removeCollaborators(Collection users) throws IOException { private void modifyCollaborators(Collection users, String method) throws IOException { for (GHUser user : users) { - new Requester(root).method(method).to(getApiTailUrl("collaborators/" + user.getLogin())); + root.retrieve().method(method).to(getApiTailUrl("collaborators/" + user.getLogin())); } } @@ -898,7 +900,8 @@ private void modifyCollaborators(Collection users, String method) throws public void setEmailServiceHook(String address) throws IOException { Map config = new HashMap(); config.put("address", address); - new Requester(root).method("POST") + root.retrieve() + .method("POST") .with("name", "email") .with("config", config) .with("active", true) @@ -906,7 +909,7 @@ public void setEmailServiceHook(String address) throws IOException { } private void edit(String key, String value) throws IOException { - Requester requester = new Requester(root); + Requester requester = root.retrieve().method("POST"); if (!key.equals("name")) requester.with("name", name); // even when we don't change the name, we need to send it in requester.with(key, value).method("PATCH").to(getApiTailUrl("")); @@ -1052,7 +1055,7 @@ public void allowRebaseMerge(boolean value) throws IOException { */ public void delete() throws IOException { try { - new Requester(root).method("DELETE").to(getApiTailUrl("")); + root.retrieve().method("DELETE").to(getApiTailUrl("")); } catch (FileNotFoundException x) { throw (FileNotFoundException) new FileNotFoundException("Failed to delete " + getOwnerName() + "/" + name + "; might not exist, or you might need the delete_repo scope in your token: http://stackoverflow.com/a/19327004/12916") @@ -1123,7 +1126,7 @@ public PagedIterable listForks(final ForkSort sort) { * the io exception */ public GHRepository fork() throws IOException { - new Requester(root).method("POST").to(getApiTailUrl("forks"), null); + root.retrieve().method("POST").to(getApiTailUrl("forks")); // this API is asynchronous. we need to wait for a bit for (int i = 0; i < 10; i++) { @@ -1149,7 +1152,7 @@ public GHRepository fork() throws IOException { * the io exception */ public GHRepository forkTo(GHOrganization org) throws IOException { - new Requester(root).to(getApiTailUrl("forks?org=" + org.getLogin())); + root.retrieve().method("POST").to(getApiTailUrl("forks?org=" + org.getLogin())); // this API is asynchronous. we need to wait for a bit for (int i = 0; i < 10; i++) { @@ -1291,7 +1294,9 @@ public GHPullRequest createPullRequest(String title, String body, boolean maintainerCanModify, boolean draft) throws IOException { - return new Requester(root).withPreview(SHADOW_CAT) + return root.retrieve() + .method("POST") + .withPreview(SHADOW_CAT) .with("title", title) .with("head", head) .with("base", base) @@ -1400,7 +1405,7 @@ public GHCompare getCompare(GHBranch id1, GHBranch id2) throws IOException { */ public GHRef[] getRefs() throws IOException { return GHRef.wrap( - root.retrieve().to(String.format("/repos/%s/%s/git/refs", getOwnerName(), name), GHRef[].class), + root.retrieve().toArray(String.format("/repos/%s/%s/git/refs", getOwnerName(), name), GHRef[].class), root); } @@ -1426,9 +1431,8 @@ public PagedIterable listRefs() throws IOException { * on failure communicating with GitHub, potentially due to an invalid ref type being requested */ public GHRef[] getRefs(String refType) throws IOException { - return GHRef.wrap( - root.retrieve() - .to(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType), GHRef[].class), + return GHRef.wrap(root.retrieve() + .toArray(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType), GHRef[].class), root); } @@ -1704,7 +1708,9 @@ public GHCommitStatus createCommitStatus(String sha1, String targetUrl, String description, String context) throws IOException { - return new Requester(root).with("state", state) + return root.retrieve() + .method("POST") + .with("state", state) .with("target_url", targetUrl) .with("description", description) .with("context", context) @@ -2013,7 +2019,7 @@ GHRepository wrap(GitHub root) { */ public Map getBranches() throws IOException { Map r = new TreeMap(); - for (GHBranch p : root.retrieve().to(getApiTailUrl("branches"), GHBranch[].class)) { + for (GHBranch p : root.retrieve().toArray(getApiTailUrl("branches"), GHBranch[].class)) { p.wrap(this); r.put(p.getName(), p); } @@ -2146,7 +2152,7 @@ public List getDirectoryContent(String path, String ref) throws IOExc } String target = getApiTailUrl("contents/" + path); - GHContent[] files = requester.with("ref", ref).to(target, GHContent[].class); + GHContent[] files = requester.with("ref", ref).toArray(target, GHContent[].class); GHContent.wrap(files, this); @@ -2265,7 +2271,9 @@ public GHContentUpdateResponse createContent(byte[] contentBytes, String commitM * the io exception */ public GHMilestone createMilestone(String title, String description) throws IOException { - return new Requester(root).with("title", title) + return root.retrieve() + .method("POST") + .with("title", title) .with("description", description) .method("POST") .to(getApiTailUrl("milestones"), GHMilestone.class) @@ -2284,7 +2292,9 @@ public GHMilestone createMilestone(String title, String description) throws IOEx * the io exception */ public GHDeployKey addDeployKey(String title, String key) throws IOException { - return new Requester(root).with("title", title) + return root.retrieve() + .method("POST") + .with("title", title) .with("key", key) .method("POST") .to(getApiTailUrl("keys"), GHDeployKey.class) @@ -2301,7 +2311,7 @@ public GHDeployKey addDeployKey(String title, String key) throws IOException { */ public List getDeployKeys() throws IOException { List list = new ArrayList( - Arrays.asList(root.retrieve().to(getApiTailUrl("keys"), GHDeployKey[].class))); + Arrays.asList(root.retrieve().toArray(getApiTailUrl("keys"), GHDeployKey[].class))); for (GHDeployKey h : list) h.wrap(this); return list; @@ -2354,7 +2364,9 @@ public GHRepository getParent() throws IOException { * the io exception */ public GHSubscription subscribe(boolean subscribed, boolean ignored) throws IOException { - return new Requester(root).with("subscribed", subscribed) + return root.retrieve() + .method("POST") + .with("subscribed", subscribed) .with("ignored", ignored) .method("PUT") .to(getApiTailUrl("subscription"), GHSubscription.class) @@ -2490,10 +2502,14 @@ public PagedIterable listProjects() throws IOException { * @see GitHub#renderMarkdown(String) GitHub#renderMarkdown(String) */ public Reader renderMarkdown(String text, MarkdownMode mode) throws IOException { - return new InputStreamReader(new Requester(root).with("text", text) - .with("mode", mode == null ? null : mode.toString()) - .with("context", getFullName()) - .asStream("/markdown"), "UTF-8"); + return new InputStreamReader( + root.retrieve() + .method("POST") + .with("text", text) + .with("mode", mode == null ? null : mode.toString()) + .with("context", getFullName()) + .asStream("/markdown"), + "UTF-8"); } /** @@ -2603,7 +2619,7 @@ public List listTopics() throws IOException { * the io exception */ public void setTopics(List topics) throws IOException { - Requester requester = new Requester(root); + Requester requester = root.retrieve().method("POST"); requester.with("names", topics); requester.method("PUT").withPreview(MERCY).to(getApiTailUrl("topics")); } diff --git a/src/main/java/org/kohsuke/github/GHSubscription.java b/src/main/java/org/kohsuke/github/GHSubscription.java index a8ddc81dd9..f71081fd1e 100644 --- a/src/main/java/org/kohsuke/github/GHSubscription.java +++ b/src/main/java/org/kohsuke/github/GHSubscription.java @@ -87,7 +87,7 @@ public GHRepository getRepository() { * the io exception */ public void delete() throws IOException { - new Requester(root).method("DELETE").to(repo.getApiTailUrl("subscription")); + root.retrieve().method("DELETE").to(repo.getApiTailUrl("subscription")); } GHSubscription wrapUp(GHRepository repo) { diff --git a/src/main/java/org/kohsuke/github/GHTeam.java b/src/main/java/org/kohsuke/github/GHTeam.java index 9254817b91..cf7a629fcd 100644 --- a/src/main/java/org/kohsuke/github/GHTeam.java +++ b/src/main/java/org/kohsuke/github/GHTeam.java @@ -189,7 +189,7 @@ public PagedIterable listRepositories() { * @since 1.59 */ public void add(GHUser u) throws IOException { - root.retrieve().method("PUT").to(api("/memberships/" + u.getLogin()), null); + root.retrieve().method("PUT").to(api("/memberships/" + u.getLogin())); } /** @@ -205,7 +205,7 @@ public void add(GHUser u) throws IOException { * the io exception */ public void add(GHUser user, Role role) throws IOException { - root.retrieve().method("PUT").with("role", role).to(api("/memberships/" + user.getLogin()), null); + root.retrieve().method("PUT").with("role", role).to(api("/memberships/" + user.getLogin())); } /** @@ -217,7 +217,7 @@ public void add(GHUser user, Role role) throws IOException { * the io exception */ public void remove(GHUser u) throws IOException { - root.retrieve().method("DELETE").to(api("/members/" + u.getLogin()), null); + root.retrieve().method("DELETE").to(api("/members/" + u.getLogin())); } /** @@ -246,7 +246,7 @@ public void add(GHRepository r, GHOrganization.Permission permission) throws IOE root.retrieve() .method("PUT") .with("permission", permission) - .to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); + .to(api("/repos/" + r.getOwnerName() + '/' + r.getName())); } /** @@ -258,7 +258,7 @@ public void add(GHRepository r, GHOrganization.Permission permission) throws IOE * the io exception */ public void remove(GHRepository r) throws IOException { - root.retrieve().method("DELETE").to(api("/repos/" + r.getOwnerName() + '/' + r.getName()), null); + root.retrieve().method("DELETE").to(api("/repos/" + r.getOwnerName() + '/' + r.getName())); } /** diff --git a/src/main/java/org/kohsuke/github/GHThread.java b/src/main/java/org/kohsuke/github/GHThread.java index 52b942512b..5bc35a227e 100644 --- a/src/main/java/org/kohsuke/github/GHThread.java +++ b/src/main/java/org/kohsuke/github/GHThread.java @@ -161,7 +161,7 @@ GHThread wrap(GitHub root) { * the io exception */ public void markAsRead() throws IOException { - new Requester(root).method("PATCH").to(url); + root.retrieve().method("PATCH").to(url); } /** @@ -176,7 +176,9 @@ public void markAsRead() throws IOException { * the io exception */ public GHSubscription subscribe(boolean subscribed, boolean ignored) throws IOException { - return new Requester(root).with("subscribed", subscribed) + return root.retrieve() + .method("POST") + .with("subscribed", subscribed) .with("ignored", ignored) .method("PUT") .to(subscription_url, GHSubscription.class) @@ -192,7 +194,7 @@ public GHSubscription subscribe(boolean subscribed, boolean ignored) throws IOEx */ public GHSubscription getSubscription() throws IOException { try { - return new Requester(root).to(subscription_url, GHSubscription.class).wrapUp(root); + return root.retrieve().method("POST").to(subscription_url, GHSubscription.class).wrapUp(root); } catch (FileNotFoundException e) { return null; } diff --git a/src/main/java/org/kohsuke/github/GHTreeBuilder.java b/src/main/java/org/kohsuke/github/GHTreeBuilder.java index ed27854b13..32f7267446 100644 --- a/src/main/java/org/kohsuke/github/GHTreeBuilder.java +++ b/src/main/java/org/kohsuke/github/GHTreeBuilder.java @@ -33,7 +33,7 @@ private TreeEntry(String path, String mode, String type) { GHTreeBuilder(GHRepository repo) { this.repo = repo; - req = new Requester(repo.root); + req = repo.root.retrieve().method("POST"); } /** diff --git a/src/main/java/org/kohsuke/github/GHUser.java b/src/main/java/org/kohsuke/github/GHUser.java index 73cdfea9bd..4158d367c3 100644 --- a/src/main/java/org/kohsuke/github/GHUser.java +++ b/src/main/java/org/kohsuke/github/GHUser.java @@ -43,7 +43,8 @@ public class GHUser extends GHPerson { * the io exception */ public List getKeys() throws IOException { - return Collections.unmodifiableList(Arrays.asList(root.retrieve().to(getApiTailUrl("keys"), GHKey[].class))); + return Collections + .unmodifiableList(Arrays.asList(root.retrieve().toArray(getApiTailUrl("keys"), GHKey[].class))); } /** @@ -53,7 +54,7 @@ public List getKeys() throws IOException { * the io exception */ public void follow() throws IOException { - new Requester(root).method("PUT").to("/user/following/" + login); + root.retrieve().method("PUT").to("/user/following/" + login); } /** @@ -63,7 +64,7 @@ public void follow() throws IOException { * the io exception */ public void unfollow() throws IOException { - new Requester(root).method("DELETE").to("/user/following/" + login); + root.retrieve().method("DELETE").to("/user/following/" + login); } /** @@ -186,7 +187,7 @@ static GHUser[] wrap(GHUser[] users, GitHub root) { public GHPersonSet getOrganizations() throws IOException { GHPersonSet orgs = new GHPersonSet(); Set names = new HashSet(); - for (GHOrganization o : root.retrieve().to("/users/" + login + "/orgs", GHOrganization[].class)) { + for (GHOrganization o : root.retrieve().toArray("/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 27b29540e4..4da6395714 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -425,7 +425,7 @@ URL getApiURL(String tailApiUrl) throws IOException { } Requester retrieve() { - return new Requester(this).method("GET"); + return new Requester(this); } /** @@ -709,7 +709,7 @@ public GHLicense getLicense(String key) throws IOException { * the io exception */ public List getMyInvitations() throws IOException { - GHInvitation[] invitations = retrieve().to("/user/repository_invitations", GHInvitation[].class); + GHInvitation[] invitations = retrieve().toArray("/user/repository_invitations", GHInvitation[].class); for (GHInvitation i : invitations) { i.wrapUp(this); } @@ -727,7 +727,7 @@ public List getMyInvitations() throws IOException { * the io exception */ public Map getMyOrganizations() throws IOException { - GHOrganization[] orgs = retrieve().to("/user/orgs", GHOrganization[].class); + GHOrganization[] orgs = retrieve().toArray("/user/orgs", GHOrganization[].class); Map r = new HashMap(); for (GHOrganization o : orgs) { // don't put 'o' into orgs because they are shallow @@ -761,7 +761,7 @@ public Map getUserPublicOrganizations(GHUser user) throw * the io exception */ public Map getUserPublicOrganizations(String login) throws IOException { - GHOrganization[] orgs = retrieve().to("/users/" + login + "/orgs", GHOrganization[].class); + GHOrganization[] orgs = retrieve().toArray("/users/" + login + "/orgs", GHOrganization[].class); Map r = new HashMap(); for (GHOrganization o : orgs) { // don't put 'o' into orgs because they are shallow @@ -782,7 +782,7 @@ public Map getUserPublicOrganizations(String login) thro */ public Map> getMyTeams() throws IOException { Map> allMyTeams = new HashMap>(); - for (GHTeam team : retrieve().to("/user/teams", GHTeam[].class)) { + for (GHTeam team : retrieve().toArray("/user/teams", GHTeam[].class)) { team.wrapUp(this); String orgLogin = team.getOrganization().getLogin(); Set teamsPerOrg = allMyTeams.get(orgLogin); @@ -816,7 +816,7 @@ public GHTeam getTeam(int id) throws IOException { * the io exception */ public List getEvents() throws IOException { - GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class); + GHEventInfo[] events = retrieve().toArray("/events", GHEventInfo[].class); for (GHEventInfo e : events) e.wrapUp(this); return Arrays.asList(events); @@ -924,7 +924,10 @@ public GHCreateRepositoryBuilder createRepository(String name) { * @see Documentation */ public GHAuthorization createToken(Collection scope, String note, String noteUrl) throws IOException { - Requester requester = new Requester(this).with("scopes", scope).with("note", note).with("note_url", noteUrl); + Requester requester = retrieve().method("POST") + .with("scopes", scope) + .with("note", note) + .with("note_url", noteUrl); return requester.method("POST").to("/authorizations", GHAuthorization.class).wrap(this); } @@ -957,7 +960,8 @@ public GHAuthorization createToken(Collection scope, String note, String return createToken(scope, note, noteUrl); } catch (GHOTPRequiredException ex) { String OTPstring = OTP.get(); - Requester requester = new Requester(this).with("scopes", scope) + Requester requester = retrieve().method("POST") + .with("scopes", scope) .with("note", note) .with("note_url", noteUrl); // Add the OTP from the user @@ -990,7 +994,8 @@ public GHAuthorization createOrGetAuth(String clientId, List scopes, String note, String note_url) throws IOException { - Requester requester = new Requester(this).with("client_secret", clientSecret) + Requester requester = retrieve().method("POST") + .with("client_secret", clientSecret) .with("scopes", scopes) .with("note", note) .with("note_url", note_url); @@ -1339,9 +1344,12 @@ public PagedIterable listAllPublicRepositories(final String since) * @see GHRepository#renderMarkdown(String, MarkdownMode) GHRepository#renderMarkdown(String, MarkdownMode) */ public Reader renderMarkdown(String text) throws IOException { - return new InputStreamReader(new Requester(this).with(new ByteArrayInputStream(text.getBytes("UTF-8"))) - .contentType("text/plain;charset=UTF-8") - .asStream("/markdown/raw"), "UTF-8"); + return new InputStreamReader( + retrieve().method("POST") + .with(new ByteArrayInputStream(text.getBytes("UTF-8"))) + .contentType("text/plain;charset=UTF-8") + .asStream("/markdown/raw"), + "UTF-8"); } static URL parseURL(String s) { diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index c2ecf983d0..d7b14255b8 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -57,6 +57,7 @@ import java.util.zip.GZIPInputStream; import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import javax.annotation.WillClose; import static java.util.Arrays.asList; @@ -77,7 +78,7 @@ class Requester { /** * Request method. */ - private String method = "POST"; + private String method = "GET"; private String contentType = null; private InputStream body; @@ -88,8 +89,8 @@ class Requester { private boolean forceBody; private static class Entry { - String key; - Object value; + final String key; + final Object value; private Entry(String key, Object value) { this.key = key; @@ -278,9 +279,9 @@ public Requester with(String key, Object value) { * @return the requester */ public Requester set(String key, Object value) { - for (Entry e : args) { - if (e.key.equals(key)) { - e.value = value; + for (int index = 0; index < args.size(); index++) { + if (args.get(index).key.equals(key)) { + args.set(index, new Entry(key, value)); return this; } } @@ -329,7 +330,7 @@ public Requester inBody() { * @throws IOException * the io exception */ - public void to(String tailApiUrl) throws IOException { + public void to(@Nonnull String tailApiUrl) throws IOException { _to(tailApiUrl, null, null); } @@ -346,10 +347,40 @@ public void to(String tailApiUrl) throws IOException { * @throws IOException * if the server returns 4xx/5xx responses. */ - public T to(String tailApiUrl, Class type) throws IOException { + public T to(@Nonnull String tailApiUrl, @Nonnull Class type) throws IOException { return _to(tailApiUrl, type, null); } + /** + * Sends a request to the specified URL, and parses the response into the given type via databinding. + * + * @param + * the type parameter + * @param tailApiUrl + * the tail api url + * @param type + * the type + * @return {@link Reader} that reads the response. + * @throws IOException + * if the server returns 4xx/5xx responses. + */ + public T[] toArray(@Nonnull String tailApiUrl, @Nonnull Class type) throws IOException { + T[] result; + + // for arrays we might have to loop for pagination + // use the iterator to handle it + List pages = new ArrayList<>(); + int totalSize = 0; + for (Iterator iterator = asIterator(tailApiUrl, type, 0); iterator.hasNext();) { + T[] nextResult = iterator.next(); + totalSize += Array.getLength(nextResult); + pages.add(nextResult); + } + + result = concatenatePages(type, pages, totalSize); + return setResponseHeaders(result); + } + /** * Like {@link #to(String, Class)} but updates an existing object instead of creating a new instance. * @@ -363,31 +394,15 @@ public T to(String tailApiUrl, Class type) throws IOException { * @throws IOException * the io exception */ - public T to(String tailApiUrl, T existingInstance) throws IOException { + public T to(@Nonnull String tailApiUrl, @Nonnull T existingInstance) throws IOException { return _to(tailApiUrl, null, existingInstance); } private T _to(String tailApiUrl, Class type, T instance) throws IOException { T result; - if (type != null && type.isArray()) { - // for arrays we might have to loop for pagination - // use the iterator to handle it - List pages = new ArrayList<>(); - int totalSize = 0; - for (Iterator iterator = asIterator(tailApiUrl, type, 0); iterator.hasNext();) { - T nextResult = iterator.next(); - totalSize += Array.getLength(nextResult); - pages.add(nextResult); - } - - result = concatenatePages(type, pages, totalSize); - return setResponseHeaders(result); - } - tailApiUrl = buildTailApiUrl(tailApiUrl); setupConnection(root.getApiURL(tailApiUrl)); - buildRequest(); while (true) {// loop while API rate limit is hit try { @@ -401,12 +416,12 @@ private T _to(String tailApiUrl, Class type, T instance) throws IOExcepti } } - private T concatenatePages(Class type, List pages, int totalLength) { + private T[] concatenatePages(Class type, List pages, int totalLength) { - T result = (T) Array.newInstance(type.getComponentType(), totalLength); + T[] result = type.cast(Array.newInstance(type.getComponentType(), totalLength)); int position = 0; - for (T page : pages) { + for (T[] page : pages) { final int pageLength = Array.getLength(page); System.arraycopy(page, 0, result, position, pageLength); position += pageLength; @@ -452,8 +467,6 @@ public int asHttpStatusCode(String tailApiUrl) throws IOException { method("GET"); setupConnection(root.getApiURL(tailApiUrl)); - buildRequest(); - try { return uc.getResponseCode(); } catch (IOException e) { @@ -477,8 +490,6 @@ public InputStream asStream(String tailApiUrl) throws IOException { while (true) {// loop while API rate limit is hit setupConnection(root.getApiURL(tailApiUrl)); - buildRequest(); - try { return wrapStream(uc.getInputStream()); } catch (IOException e) { @@ -592,31 +603,29 @@ private boolean isMethodWithBody() { } PagedIterable asPagedIterable(String tailApiUrl, Class type, Consumer consumer) { - return new PagedIterableWithConsumer<>(type, this, tailApiUrl, consumer); + return new PagedIterableWithConsumer<>(type, tailApiUrl, consumer); } - static class PagedIterableWithConsumer extends PagedIterable { + class PagedIterableWithConsumer extends PagedIterable { - private final Class clazz; - private final Requester requester; + private final Class clazz; private final String tailApiUrl; - private final Consumer consumer; + private final Consumer consumer; - PagedIterableWithConsumer(Class clazz, Requester requester, String tailApiUrl, Consumer consumer) { + PagedIterableWithConsumer(Class clazz, String tailApiUrl, Consumer consumer) { this.clazz = clazz; this.tailApiUrl = tailApiUrl; - this.requester = requester; this.consumer = consumer; } @Override - public PagedIterator _iterator(int pageSize) { - final Iterator iterator = requester.asIterator(tailApiUrl, clazz, pageSize); - return new PagedIterator(iterator) { + public PagedIterator _iterator(int pageSize) { + final Iterator iterator = asIterator(tailApiUrl, clazz, pageSize); + return new PagedIterator(iterator) { @Override - protected void wrapUp(S[] page) { + protected void wrapUp(T[] page) { if (consumer != null) { - for (S item : page) { + for (T item : page) { consumer.accept(item); } } @@ -645,6 +654,15 @@ Iterator asIterator(String tailApiUrl, Class type, int pageSize) { } } + /** + * May be used for any item that has pagination information. + * + * Works for array responses, also works for search results which are single instances with an array of items + * inside. + * + * @param + * type of each page (not the items in the page). + */ class PagingIterator implements Iterator { private final Class type; @@ -753,6 +771,7 @@ private void setupConnection(URL url) throws IOException { setRequestMethod(uc); uc.setRequestProperty("Accept-Encoding", "gzip"); + buildRequest(); } private void setRequestMethod(HttpURLConnection uc) throws IOException {