diff --git a/src/main/java/me/brianlong/github/ApiGateway.java b/src/main/java/me/brianlong/github/ApiGateway.java new file mode 100644 index 0000000..64facaf --- /dev/null +++ b/src/main/java/me/brianlong/github/ApiGateway.java @@ -0,0 +1,98 @@ +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 get(String uri, Map paramMap, Class responseType) throws IOException { + return this.execute(HttpGet.METHOD_NAME, uri, paramMap, null, responseType); + } + + public Response post(String uri, Request requestObject, Class responseType) throws IOException { + return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); + } + + public Response put(String uri, Request requestObject, Class responseType) throws IOException { + return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); + } + + public Response delete(String uri, Map paramMap, Class responseType) throws IOException { + return this.execute(HttpDelete.METHOD_NAME, uri, paramMap, null, responseType); + } + + private Response execute(String method, String uri, Map paramMap, Request requestObject, Class 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 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(); + } + } + +} diff --git a/src/main/java/me/brianlong/github/model/CreatePullRequest.java b/src/main/java/me/brianlong/github/model/CreatePullRequest.java new file mode 100644 index 0000000..d8b7a29 --- /dev/null +++ b/src/main/java/me/brianlong/github/model/CreatePullRequest.java @@ -0,0 +1,110 @@ +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; + } + + } + +} diff --git a/src/main/java/me/brianlong/github/model/CreateReference.java b/src/main/java/me/brianlong/github/model/CreateReference.java index 40459cd..d56ae15 100644 --- a/src/main/java/me/brianlong/github/model/CreateReference.java +++ b/src/main/java/me/brianlong/github/model/CreateReference.java @@ -3,28 +3,42 @@ 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 { - @JsonProperty(required = true) - private String ref; - @JsonProperty(required = true) - private String sha; - - public String getRef() { - return this.ref; + private CreateReference() { } - public void setRef(String ref) { - this.ref = ref; + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Request { + + @JsonProperty(required = true) + private String ref; + @JsonProperty(required = true) + private String sha; + + public String getRef() { + return this.ref; + } + + public void setRef(String ref) { + this.ref = ref; + } + + public String getSha() { + return this.sha; + } + + public void setSha(String sha) { + this.sha = sha; + } + } - public String getSha() { - return this.sha; - } - - public void setSha(String sha) { - this.sha = sha; + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Response extends BaseResponse { + } } diff --git a/src/main/java/me/brianlong/github/model/PullRequest.java b/src/main/java/me/brianlong/github/model/PullRequest.java deleted file mode 100644 index 6bacabb..0000000 --- a/src/main/java/me/brianlong/github/model/PullRequest.java +++ /dev/null @@ -1,68 +0,0 @@ -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; - } - -} diff --git a/src/main/java/me/brianlong/http/BaseResponse.java b/src/main/java/me/brianlong/http/BaseResponse.java new file mode 100644 index 0000000..f340591 --- /dev/null +++ b/src/main/java/me/brianlong/http/BaseResponse.java @@ -0,0 +1,27 @@ +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; + } + +}