From e2b7778e347b787707dfe4dcb46fa7e515dce643 Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Thu, 3 Sep 2015 23:41:17 +0200 Subject: Factor out url construction to v1 and v2 --- .../bitbucket/ApiClient.java | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index 7b15069..173ae1c 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -37,9 +37,8 @@ public class ApiClient { } public List getPullRequests() { - String response = get(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); try { - return parse(response, Pullrequest.Response.class).getPullrequests(); + return parse(get(v2("/pullrequests/")), Pullrequest.Response.class).getPullrequests(); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -47,10 +46,8 @@ public class ApiClient { } public List getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { - String response = get( - V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); try { - return parse(response, new TypeReference>() {}); + return parse(get(v1("/pullrequests/" + pullRequestId + "/comments")), new TypeReference>() {}); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -65,10 +62,10 @@ public class ApiClient { public Pullrequest.Comment postPullRequestComment(String pullRequestId, String comment) { - String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments"; try { - NameValuePair content = new NameValuePair("content", comment); - String response = post(path, new NameValuePair[]{ content }); + String response = post( + v1("/pullrequests/" + pullRequestId + "/comments"), + new NameValuePair[]{ new NameValuePair("content", comment) }); return parse(response, Pullrequest.Comment.class); } catch (UnsupportedEncodingException e) { e.printStackTrace(); @@ -80,15 +77,13 @@ public class ApiClient { } public void deletePullRequestApproval(String pullRequestId) { - String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; - delete(path); + delete(v2("/pullrequests/" + pullRequestId + "/approve")); } public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { - String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; try { - String response = post(path, new NameValuePair[]{}); - return parse(response, Pullrequest.Participant.class); + return parse(post(v2("/pullrequests/" + pullRequestId + "/approve"), + new NameValuePair[]{}), Pullrequest.Participant.class); } catch (IOException e) { e.printStackTrace(); } @@ -115,6 +110,14 @@ public class ApiClient { return client; } + private String v1(String url) { + return V1_API_BASE_URL + this.owner + "/" + this.repositoryName + url; + } + + private String v2(String url) { + return V2_API_BASE_URL + this.owner + "/" + this.repositoryName + url; + } + private String get(String path) { return send(new GetMethod(path)); } -- cgit v1.2.3