Loading src/main/java/me/brianlong/github/ApiGateway.java 0 → 100644 +98 −0 Original line number Diff line number Diff line package me.brianlong.github; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpResponse; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import me.brianlong.http.BaseResponse; import me.brianlong.http.PreemptiveAuthInterceptor; public class ApiGateway { private final Logger logger = LoggerFactory.getLogger(ApiGateway.class); private ObjectMapper omapper = new ObjectMapper(); private CredentialsProvider credProvider; public ApiGateway(CredentialsProvider credProvider) { this.credProvider = credProvider; } public <Response extends BaseResponse> Response get(String uri, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpGet.METHOD_NAME, uri, paramMap, null, responseType); } public <Request, Response extends BaseResponse> Response post(String uri, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); } public <Request, Response extends BaseResponse> Response put(String uri, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); } public <Response extends BaseResponse> Response delete(String uri, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpDelete.METHOD_NAME, uri, paramMap, null, responseType); } private <Request, Response extends BaseResponse> Response execute(String method, String uri, Map<String, Object> paramMap, Request requestObject, Class<Response> responseType) throws IOException { if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uri + "')"); RequestBuilder builder = RequestBuilder .create(method) .setUri(uri); if (paramMap != null) { for (Entry<String, Object> param : paramMap.entrySet()) if (param.getValue() != null) builder.addParameter(param.getKey(), param.getValue().toString()); } if (requestObject != null) { String requestJson = this.omapper.writeValueAsString(requestObject); if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uri + "'): " + requestJson); builder.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON)); } HttpUriRequest request = builder.build(); if (this.logger.isDebugEnabled()) this.logger.debug("Prepared request for " + method + " to: " + uri); HttpResponse response = HttpClientBuilder .create() .addInterceptorFirst(new PreemptiveAuthInterceptor()) .setDefaultCredentialsProvider(this.credProvider) .build() .execute(request); if (this.logger.isDebugEnabled()) this.logger.debug("Received response from " + method + ": " + response.getStatusLine().getStatusCode()); InputStream istream = response.getEntity().getContent(); try { Response responseObject = this.omapper.readerFor(responseType).readValue(istream); responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode()); responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase()); return responseObject; } finally { istream.close(); } } } src/main/java/me/brianlong/github/model/CreatePullRequest.java 0 → 100644 +110 −0 Original line number Diff line number Diff line package me.brianlong.github.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import me.brianlong.http.BaseResponse; public class CreatePullRequest { private CreatePullRequest() { } @JsonIgnoreProperties(ignoreUnknown = true) public static class Request { @JsonProperty(required = true) private String title; @JsonProperty(required = true) private String head; @JsonProperty(required = true) private String base; private String body; @JsonProperty(value = "maintainer_can_modify") private Boolean maintainerCanModify; private Boolean draft; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBase() { return base; } public void setBase(String base) { this.base = base; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Boolean getMaintainerCanModify() { return maintainerCanModify; } public void setMaintainerCanModify(Boolean maintainerCanModify) { this.maintainerCanModify = maintainerCanModify; } public Boolean getDraft() { return draft; } public void setDraft(Boolean draft) { this.draft = draft; } } @JsonIgnoreProperties(ignoreUnknown = true) public static class Response extends BaseResponse { private String url; private int id; private String title; public String getUrl() { return this.url; } public void setUrl(String url) { this.url = url; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } } } src/main/java/me/brianlong/github/model/CreateReference.java +29 −15 Original line number Diff line number Diff line Loading @@ -3,9 +3,16 @@ package me.brianlong.github.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(ignoreUnknown = true) import me.brianlong.http.BaseResponse; public class CreateReference { private CreateReference() { } @JsonIgnoreProperties(ignoreUnknown = true) public static class Request { @JsonProperty(required = true) private String ref; @JsonProperty(required = true) Loading @@ -28,3 +35,10 @@ public class CreateReference { } } @JsonIgnoreProperties(ignoreUnknown = true) public static class Response extends BaseResponse { } } src/main/java/me/brianlong/github/model/PullRequest.javadeleted 100644 → 0 +0 −68 Original line number Diff line number Diff line package me.brianlong.github.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(ignoreUnknown = true) public class PullRequest { @JsonProperty(required = true) private String title; @JsonProperty(required = true) private String head; @JsonProperty(required = true) private String base; private String body; @JsonProperty(value = "maintainer_can_modify") private Boolean maintainerCanModify; private Boolean draft; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBase() { return base; } public void setBase(String base) { this.base = base; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Boolean getMaintainerCanModify() { return maintainerCanModify; } public void setMaintainerCanModify(Boolean maintainerCanModify) { this.maintainerCanModify = maintainerCanModify; } public Boolean getDraft() { return draft; } public void setDraft(Boolean draft) { this.draft = draft; } } src/main/java/me/brianlong/http/BaseResponse.java 0 → 100644 +27 −0 Original line number Diff line number Diff line package me.brianlong.http; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class BaseResponse { private int httpStatusCode; private String httpStatusReason; public int getHttpStatusCode() { return this.httpStatusCode; } public void setHttpStatusCode(int httpStatusCode) { this.httpStatusCode = httpStatusCode; } public String getHttpStatusReason() { return this.httpStatusReason; } public void setHttpStatusReason(String httpStatusReason) { this.httpStatusReason = httpStatusReason; } } Loading
src/main/java/me/brianlong/github/ApiGateway.java 0 → 100644 +98 −0 Original line number Diff line number Diff line package me.brianlong.github; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpResponse; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.fasterxml.jackson.databind.ObjectMapper; import me.brianlong.http.BaseResponse; import me.brianlong.http.PreemptiveAuthInterceptor; public class ApiGateway { private final Logger logger = LoggerFactory.getLogger(ApiGateway.class); private ObjectMapper omapper = new ObjectMapper(); private CredentialsProvider credProvider; public ApiGateway(CredentialsProvider credProvider) { this.credProvider = credProvider; } public <Response extends BaseResponse> Response get(String uri, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpGet.METHOD_NAME, uri, paramMap, null, responseType); } public <Request, Response extends BaseResponse> Response post(String uri, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); } public <Request, Response extends BaseResponse> Response put(String uri, Request requestObject, Class<Response> responseType) throws IOException { return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); } public <Response extends BaseResponse> Response delete(String uri, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { return this.execute(HttpDelete.METHOD_NAME, uri, paramMap, null, responseType); } private <Request, Response extends BaseResponse> Response execute(String method, String uri, Map<String, Object> paramMap, Request requestObject, Class<Response> responseType) throws IOException { if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uri + "')"); RequestBuilder builder = RequestBuilder .create(method) .setUri(uri); if (paramMap != null) { for (Entry<String, Object> param : paramMap.entrySet()) if (param.getValue() != null) builder.addParameter(param.getKey(), param.getValue().toString()); } if (requestObject != null) { String requestJson = this.omapper.writeValueAsString(requestObject); if (this.logger.isTraceEnabled()) this.logger.trace("execute('" + method + "', '" + uri + "'): " + requestJson); builder.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON)); } HttpUriRequest request = builder.build(); if (this.logger.isDebugEnabled()) this.logger.debug("Prepared request for " + method + " to: " + uri); HttpResponse response = HttpClientBuilder .create() .addInterceptorFirst(new PreemptiveAuthInterceptor()) .setDefaultCredentialsProvider(this.credProvider) .build() .execute(request); if (this.logger.isDebugEnabled()) this.logger.debug("Received response from " + method + ": " + response.getStatusLine().getStatusCode()); InputStream istream = response.getEntity().getContent(); try { Response responseObject = this.omapper.readerFor(responseType).readValue(istream); responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode()); responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase()); return responseObject; } finally { istream.close(); } } }
src/main/java/me/brianlong/github/model/CreatePullRequest.java 0 → 100644 +110 −0 Original line number Diff line number Diff line package me.brianlong.github.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import me.brianlong.http.BaseResponse; public class CreatePullRequest { private CreatePullRequest() { } @JsonIgnoreProperties(ignoreUnknown = true) public static class Request { @JsonProperty(required = true) private String title; @JsonProperty(required = true) private String head; @JsonProperty(required = true) private String base; private String body; @JsonProperty(value = "maintainer_can_modify") private Boolean maintainerCanModify; private Boolean draft; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBase() { return base; } public void setBase(String base) { this.base = base; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Boolean getMaintainerCanModify() { return maintainerCanModify; } public void setMaintainerCanModify(Boolean maintainerCanModify) { this.maintainerCanModify = maintainerCanModify; } public Boolean getDraft() { return draft; } public void setDraft(Boolean draft) { this.draft = draft; } } @JsonIgnoreProperties(ignoreUnknown = true) public static class Response extends BaseResponse { private String url; private int id; private String title; public String getUrl() { return this.url; } public void setUrl(String url) { this.url = url; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } } }
src/main/java/me/brianlong/github/model/CreateReference.java +29 −15 Original line number Diff line number Diff line Loading @@ -3,9 +3,16 @@ package me.brianlong.github.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(ignoreUnknown = true) import me.brianlong.http.BaseResponse; public class CreateReference { private CreateReference() { } @JsonIgnoreProperties(ignoreUnknown = true) public static class Request { @JsonProperty(required = true) private String ref; @JsonProperty(required = true) Loading @@ -28,3 +35,10 @@ public class CreateReference { } } @JsonIgnoreProperties(ignoreUnknown = true) public static class Response extends BaseResponse { } }
src/main/java/me/brianlong/github/model/PullRequest.javadeleted 100644 → 0 +0 −68 Original line number Diff line number Diff line package me.brianlong.github.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @JsonIgnoreProperties(ignoreUnknown = true) public class PullRequest { @JsonProperty(required = true) private String title; @JsonProperty(required = true) private String head; @JsonProperty(required = true) private String base; private String body; @JsonProperty(value = "maintainer_can_modify") private Boolean maintainerCanModify; private Boolean draft; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBase() { return base; } public void setBase(String base) { this.base = base; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public Boolean getMaintainerCanModify() { return maintainerCanModify; } public void setMaintainerCanModify(Boolean maintainerCanModify) { this.maintainerCanModify = maintainerCanModify; } public Boolean getDraft() { return draft; } public void setDraft(Boolean draft) { this.draft = draft; } }
src/main/java/me/brianlong/http/BaseResponse.java 0 → 100644 +27 −0 Original line number Diff line number Diff line package me.brianlong.http; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class BaseResponse { private int httpStatusCode; private String httpStatusReason; public int getHttpStatusCode() { return this.httpStatusCode; } public void setHttpStatusCode(int httpStatusCode) { this.httpStatusCode = httpStatusCode; } public String getHttpStatusReason() { return this.httpStatusReason; } public void setHttpStatusReason(String httpStatusReason) { this.httpStatusReason = httpStatusReason; } }