now including more HTTP related work
This commit is contained in:
parent
388d490122
commit
2bcb6ff9e5
98
src/main/java/me/brianlong/github/ApiGateway.java
Normal file
98
src/main/java/me/brianlong/github/ApiGateway.java
Normal file
@ -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 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
110
src/main/java/me/brianlong/github/model/CreatePullRequest.java
Normal file
110
src/main/java/me/brianlong/github/model/CreatePullRequest.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
27
src/main/java/me/brianlong/http/BaseResponse.java
Normal file
27
src/main/java/me/brianlong/http/BaseResponse.java
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user