diff options
author | Jonathan Brachthäuser <jonathan@b-studios.de> | 2015-09-03 23:16:16 +0200 |
---|---|---|
committer | Jonathan Brachthäuser <jonathan@b-studios.de> | 2015-09-04 11:38:55 +0200 |
commit | db67f5bd46fe70073548e9acfc590ecd35b72272 (patch) | |
tree | b12356f0f9a3d46409cca09c10dada32e2b4316a /src | |
parent | 69baef38cbde57293451dc841b1b370ec0708e88 (diff) | |
download | bbprb-db67f5bd46fe70073548e9acfc590ecd35b72272.tar.gz |
Refactor: Have only one generic parse method
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore | 20 | ||||
-rw-r--r-- | src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java | 45 |
2 files changed, 28 insertions, 37 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore new file mode 100644 index 0000000..21e5893 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/.gitignore @@ -0,0 +1,20 @@ +target/ +work/ + +# +# Eclipse metadata. +# +.project +.classpath +.settings/ + +# +# Eclipse and Maven build results +# +bin/ + +# IntelliJ metadata. +*.iml +*.ipr +*.iws +.idea/ diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index ceec1a8..ad9b16b 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -39,7 +39,7 @@ public class ApiClient { public List<Pullrequest> getPullRequests() { String response = getRequest(V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/"); try { - return parsePullRequestJson(response).getPullrequests(); + return parse(response, Pullrequest.Response.class).getPullrequests(); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -50,7 +50,7 @@ public class ApiClient { String response = getRequest( V1_API_BASE_URL + commentOwnerName + "/" + commentRepositoryName + "/pullrequests/" + pullRequestId + "/comments"); try { - return parseCommentJson(response); + return parse(response, new TypeReference<List<Pullrequest.Comment>>() {}); } catch(Exception e) { logger.log(Level.WARNING, "invalid pull request response.", e); } @@ -69,8 +69,7 @@ public class ApiClient { try { NameValuePair content = new NameValuePair("content", comment); String response = postRequest(path, new NameValuePair[]{ content }); - return parseSingleCommentJson(response); - + return parse(response, Pullrequest.Comment.class); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } @@ -89,7 +88,7 @@ public class ApiClient { String path = V2_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/approve"; try { String response = postRequest(path, new NameValuePair[]{}); - return parseApprovalJson(response); + return parse(response, Pullrequest.Participant.class); } catch (IOException e) { e.printStackTrace(); } @@ -166,39 +165,11 @@ public class ApiClient { } - private Pullrequest.Response parsePullRequestJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Pullrequest.Response parsedResponse; - parsedResponse = mapper.readValue(response, Pullrequest.Response.class); - return parsedResponse; - } - - private List<Pullrequest.Comment> parseCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - List<Pullrequest.Comment> parsedResponse; - parsedResponse = mapper.readValue( - response, - new TypeReference<List<Pullrequest.Comment>>() { - }); - return parsedResponse; + private <R> R parse(String response, Class<R> cls) throws IOException { + return new ObjectMapper().readValue(response, cls); } - - private Pullrequest.Comment parseSingleCommentJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Pullrequest.Comment parsedResponse; - parsedResponse = mapper.readValue( - response, - Pullrequest.Comment.class); - return parsedResponse; - } - - private Pullrequest.Participant parseApprovalJson(String response) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - Pullrequest.Participant parsedResponse; - parsedResponse = mapper.readValue( - response, - Pullrequest.Participant.class); - return parsedResponse; + private <R> R parse(String response, TypeReference<R> ref) throws IOException { + return new ObjectMapper().readValue(response, ref); } } |