Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fb1d1548b | ||
|
|
cd6f36f41d | ||
|
|
f5abc63adf | ||
|
|
cc54463ac1 | ||
|
|
b2da5aa049 | ||
|
|
98dbb0eb85 |
@@ -1,5 +1,6 @@
|
||||
# Maven
|
||||
target
|
||||
pom.xml.versionsBackup
|
||||
|
||||
# Eclipse
|
||||
.project
|
||||
|
||||
@@ -19,12 +19,13 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.inteligr8.http.BaseResponse;
|
||||
import com.inteligr8.http.PreemptiveAuthInterceptor;
|
||||
import com.inteligr8.github.http.BaseResponse;
|
||||
import com.inteligr8.github.http.PreemptiveAuthInterceptor;
|
||||
|
||||
public class ApiGateway {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ApiGateway.class);
|
||||
private final String baseUrl = "https://api.github.com";
|
||||
private ObjectMapper omapper = new ObjectMapper();
|
||||
private CredentialsProvider credProvider;
|
||||
|
||||
@@ -32,29 +33,29 @@ public class ApiGateway {
|
||||
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 <Response extends BaseResponse> Response get(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException {
|
||||
return this.execute(HttpGet.METHOD_NAME, uriPath, 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 post(String uriPath, Request requestObject, Class<Response> responseType) throws IOException {
|
||||
return this.execute(HttpPost.METHOD_NAME, uriPath, 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 <Request, Response extends BaseResponse> Response put(String uriPath, Request requestObject, Class<Response> responseType) throws IOException {
|
||||
return this.execute(HttpPost.METHOD_NAME, uriPath, 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);
|
||||
public <Response extends BaseResponse> Response delete(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException {
|
||||
return this.execute(HttpDelete.METHOD_NAME, uriPath, 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 {
|
||||
private <Request, Response extends BaseResponse> Response execute(String method, String uriPath, Map<String, Object> paramMap, Request requestObject, Class<Response> responseType) throws IOException {
|
||||
if (this.logger.isTraceEnabled())
|
||||
this.logger.trace("execute('" + method + "', '" + uri + "')");
|
||||
this.logger.trace("execute('" + method + "', '" + uriPath + "')");
|
||||
|
||||
RequestBuilder builder = RequestBuilder
|
||||
.create(method)
|
||||
.setUri(uri);
|
||||
.setUri(this.baseUrl + uriPath);
|
||||
|
||||
if (paramMap != null) {
|
||||
for (Entry<String, Object> param : paramMap.entrySet())
|
||||
@@ -65,14 +66,14 @@ public class ApiGateway {
|
||||
if (requestObject != null) {
|
||||
String requestJson = this.omapper.writeValueAsString(requestObject);
|
||||
if (this.logger.isTraceEnabled())
|
||||
this.logger.trace("execute('" + method + "', '" + uri + "'): " + requestJson);
|
||||
this.logger.trace("execute('" + method + "', '" + uriPath + "'): " + 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);
|
||||
this.logger.debug("Prepared request for " + method + " to: " + uriPath);
|
||||
|
||||
HttpResponse response = HttpClientBuilder
|
||||
.create()
|
||||
@@ -83,15 +84,21 @@ public class ApiGateway {
|
||||
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();
|
||||
Response responseObject = null;
|
||||
if (response.getEntity() != null) {
|
||||
InputStream istream = response.getEntity().getContent();
|
||||
try {
|
||||
responseObject = this.omapper.readerFor(responseType).readValue(istream);
|
||||
} finally {
|
||||
istream.close();
|
||||
}
|
||||
} else {
|
||||
responseObject = this.omapper.readerFor(responseType).readValue("{}");
|
||||
}
|
||||
|
||||
responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode());
|
||||
responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase());
|
||||
return responseObject;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.inteligr8.http;
|
||||
package com.inteligr8.github.http;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
||||
+17
-1
@@ -1,4 +1,4 @@
|
||||
package com.inteligr8.http;
|
||||
package com.inteligr8.github.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -15,8 +15,24 @@ import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.apache.http.protocol.HttpCoreContext;
|
||||
|
||||
/**
|
||||
* This class allows the developer to force the sending of credentials on the
|
||||
* first HTTP request. Without this interceptor, all requests will first try
|
||||
* without credentials.
|
||||
*
|
||||
* A subsequent retry will happen only under certain HTTP status codes. Some
|
||||
* services will return a 404 instead of an expected 401 or 403. This is for
|
||||
* security reasons. As a side effect, it fools the Apache HTTP client into
|
||||
* thinking that the resource does not exist, even if it were authorized. So
|
||||
* it will not retry it with credentials.
|
||||
*
|
||||
* @author brian@inteligr8.com
|
||||
*/
|
||||
public class PreemptiveAuthInterceptor implements HttpRequestInterceptor {
|
||||
|
||||
/**
|
||||
* @see HttpRequestInterceptor#process(HttpRequest, HttpContext)
|
||||
*/
|
||||
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
|
||||
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
|
||||
|
||||
@@ -2,13 +2,17 @@ package com.inteligr8.github.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.inteligr8.http.BaseResponse;
|
||||
import com.inteligr8.github.http.BaseResponse;
|
||||
|
||||
public class CreatePullRequest {
|
||||
|
||||
private CreatePullRequest() {
|
||||
}
|
||||
|
||||
public static String constructRequestPath(String repoName) {
|
||||
return "/repos/" + repoName + "/" + httpPath;
|
||||
}
|
||||
|
||||
public static String httpPath = "pulls";
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
|
||||
@@ -2,13 +2,17 @@ package com.inteligr8.github.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.inteligr8.http.BaseResponse;
|
||||
import com.inteligr8.github.http.BaseResponse;
|
||||
|
||||
public class CreateReference {
|
||||
|
||||
private CreateReference() {
|
||||
}
|
||||
|
||||
public static String constructRequestPath(String repoName) {
|
||||
return "/repos/" + repoName + "/" + httpPath;
|
||||
}
|
||||
|
||||
public static String httpPath = "git/refs";
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.inteligr8.github.model;
|
||||
|
||||
public class DeleteReference {
|
||||
|
||||
private DeleteReference() {
|
||||
}
|
||||
|
||||
public static String constructRequestPath(String repoName, String ref) {
|
||||
return "/repos/" + repoName + "/" + httpPath + "/" + ref;
|
||||
}
|
||||
|
||||
public static String constructRequestPathByBranch(String repoName, String branchName) {
|
||||
return constructRequestPath(repoName, "refs/heads/" + branchName);
|
||||
}
|
||||
|
||||
public static String httpPath = "git";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user