diff options
author | Jonathan Brachthäuser <jonathan@b-studios.de> | 2015-09-03 23:24:47 +0200 |
---|---|---|
committer | Jonathan Brachthäuser <jonathan@b-studios.de> | 2015-09-04 11:38:55 +0200 |
commit | 31ed45e7bf69505c0dedb6ddf19b69bb6c37ffaa (patch) | |
tree | 5bda83074e2aae7c321198965f93e17287a3c4a1 /src/main/java | |
parent | db67f5bd46fe70073548e9acfc590ecd35b72272 (diff) | |
download | bbprb-31ed45e7bf69505c0dedb6ddf19b69bb6c37ffaa.tar.gz |
Refactor http requests
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java | 61 |
1 files changed, 20 insertions, 41 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index ad9b16b..7b15069 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -37,7 +37,7 @@ public class ApiClient { } public List<Pullrequest> getPullRequests() { - String response = getRequest(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); + String response = get(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); try { return parse(response, Pullrequest.Response.class).getPullrequests(); } catch(Exception e) { @@ -47,7 +47,7 @@ public class ApiClient { } public List<Pullrequest.Comment> getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { - String response = getRequest( + String response = get( V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); try { return parse(response, new TypeReference<List<Pullrequest.Comment>>() {}); @@ -60,7 +60,7 @@ public class ApiClient { public void deletePullRequestComment(String pullRequestId, String commentId) { String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments/" + commentId; //https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/pullrequests/{pull_request_id}/comments/{comment_id} - deleteRequest(path); + delete(path); } @@ -68,7 +68,7 @@ public class ApiClient { String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments"; try { NameValuePair content = new NameValuePair("content", comment); - String response = postRequest(path, new NameValuePair[]{ content }); + String response = post(path, new NameValuePair[]{ content }); return parse(response, Pullrequest.Comment.class); } catch (UnsupportedEncodingException e) { e.printStackTrace(); @@ -81,13 +81,13 @@ public class ApiClient { public void deletePullRequestApproval(String pullRequestId) { String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; - deleteRequest(path); + delete(path); } public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; try { - String response = postRequest(path, new NameValuePair[]{}); + String response = post(path, new NameValuePair[]{}); return parse(response, Pullrequest.Participant.class); } catch (IOException e) { e.printStackTrace(); @@ -115,54 +115,33 @@ public class ApiClient { return client; } - private String getRequest(String path) { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - GetMethod httpget = new GetMethod(path); - client.getParams().setAuthenticationPreemptive(true); - String response = null; - try { - client.executeMethod(httpget); - response = httpget.getResponseBodyAsString(); - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return response; + private String get(String path) { + return send(new GetMethod(path)); } - public void deleteRequest(String path) { - HttpClient client = getHttpClient(); - client.getState().setCredentials(AuthScope.ANY, credentials); - DeleteMethod httppost = new DeleteMethod(path); - client.getParams().setAuthenticationPreemptive(true); - String response = ""; - try { - client.executeMethod(httppost); - } catch (IOException e) { - e.printStackTrace(); - } + private String post(String path, NameValuePair[] data) { + PostMethod req = new PostMethod(path); + req.setRequestBody(data); + return send(req); + } + + private void delete(String path) { + send(new DeleteMethod(path)); } - private String postRequest(String path, NameValuePair[] params) throws UnsupportedEncodingException { + private String send(HttpMethodBase req) { HttpClient client = getHttpClient(); client.getState().setCredentials(AuthScope.ANY, credentials); - PostMethod httppost = new PostMethod(path); - httppost.setRequestBody(params); client.getParams().setAuthenticationPreemptive(true); - String response = ""; try { - client.executeMethod(httppost); - response = httppost.getResponseBodyAsString(); - logger.info("API Request Response: " + response); + client.executeMethod(req); + return req.getResponseBodyAsString(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } - return response; - + return null; } private <R> R parse(String response, Class<R> cls) throws IOException { |