Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
ce92fc5b22 | |||
cd6f36f41d | |||
f5abc63adf | |||
cc54463ac1 | |||
4b7fd35201 | |||
c8ebcc48a3 | |||
b2da5aa049 | |||
98dbb0eb85 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
# Maven
|
||||
target
|
||||
pom.xml.versionsBackup
|
||||
|
||||
# Eclipse
|
||||
.project
|
||||
|
2
pom.xml
2
pom.xml
@ -5,7 +5,7 @@
|
||||
<groupId>com.inteligr8</groupId>
|
||||
<artifactId>github-api</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.1</version>
|
||||
|
||||
<name>GitHub API & Utilities</name>
|
||||
|
||||
|
@ -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,4 +1,4 @@
|
||||
package com.inteligr8.http;
|
||||
package com.inteligr8.github.http;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.inteligr8.http;
|
||||
package com.inteligr8.github.http;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -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";
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user