Compare commits

..

8 Commits

Author SHA1 Message Date
ce92fc5b22 Merge branch 'develop' into stable 2021-01-11 10:52:52 -05:00
cd6f36f41d fixed after delete tests 2021-01-11 10:51:56 -05:00
f5abc63adf added DeleteReference 2021-01-11 10:22:56 -05:00
cc54463ac1 renamed package 2021-01-11 10:22:48 -05:00
4b7fd35201 v1.1.1 pom 2021-01-11 10:04:37 -05:00
c8ebcc48a3 Merge branch 'develop' into stable 2021-01-11 10:04:03 -05:00
b2da5aa049 added pom.xml.versionsBackup to gitignore 2021-01-11 10:03:23 -05:00
98dbb0eb85 added hostname/url 2021-01-11 09:51:35 -05:00
8 changed files with 62 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
# Maven # Maven
target target
pom.xml.versionsBackup
# Eclipse # Eclipse
.project .project

View File

@ -5,7 +5,7 @@
<groupId>com.inteligr8</groupId> <groupId>com.inteligr8</groupId>
<artifactId>github-api</artifactId> <artifactId>github-api</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>1.1.0</version> <version>1.1.1</version>
<name>GitHub API &amp; Utilities</name> <name>GitHub API &amp; Utilities</name>

View File

@ -19,12 +19,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.inteligr8.http.BaseResponse; import com.inteligr8.github.http.BaseResponse;
import com.inteligr8.http.PreemptiveAuthInterceptor; import com.inteligr8.github.http.PreemptiveAuthInterceptor;
public class ApiGateway { public class ApiGateway {
private final Logger logger = LoggerFactory.getLogger(ApiGateway.class); private final Logger logger = LoggerFactory.getLogger(ApiGateway.class);
private final String baseUrl = "https://api.github.com";
private ObjectMapper omapper = new ObjectMapper(); private ObjectMapper omapper = new ObjectMapper();
private CredentialsProvider credProvider; private CredentialsProvider credProvider;
@ -32,29 +33,29 @@ public class ApiGateway {
this.credProvider = credProvider; this.credProvider = credProvider;
} }
public <Response extends BaseResponse> Response get(String uri, Map<String, Object> paramMap, Class<Response> responseType) throws IOException { public <Response extends BaseResponse> Response get(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException {
return this.execute(HttpGet.METHOD_NAME, uri, paramMap, null, responseType); 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 { public <Request, Response extends BaseResponse> Response post(String uriPath, Request requestObject, Class<Response> responseType) throws IOException {
return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); 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 { public <Request, Response extends BaseResponse> Response put(String uriPath, Request requestObject, Class<Response> responseType) throws IOException {
return this.execute(HttpPost.METHOD_NAME, uri, null, requestObject, responseType); 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 { public <Response extends BaseResponse> Response delete(String uriPath, Map<String, Object> paramMap, Class<Response> responseType) throws IOException {
return this.execute(HttpDelete.METHOD_NAME, uri, paramMap, null, responseType); 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()) if (this.logger.isTraceEnabled())
this.logger.trace("execute('" + method + "', '" + uri + "')"); this.logger.trace("execute('" + method + "', '" + uriPath + "')");
RequestBuilder builder = RequestBuilder RequestBuilder builder = RequestBuilder
.create(method) .create(method)
.setUri(uri); .setUri(this.baseUrl + uriPath);
if (paramMap != null) { if (paramMap != null) {
for (Entry<String, Object> param : paramMap.entrySet()) for (Entry<String, Object> param : paramMap.entrySet())
@ -65,14 +66,14 @@ public class ApiGateway {
if (requestObject != null) { if (requestObject != null) {
String requestJson = this.omapper.writeValueAsString(requestObject); String requestJson = this.omapper.writeValueAsString(requestObject);
if (this.logger.isTraceEnabled()) 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)); builder.setEntity(new StringEntity(requestJson, ContentType.APPLICATION_JSON));
} }
HttpUriRequest request = builder.build(); HttpUriRequest request = builder.build();
if (this.logger.isDebugEnabled()) 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 HttpResponse response = HttpClientBuilder
.create() .create()
@ -83,15 +84,21 @@ public class ApiGateway {
if (this.logger.isDebugEnabled()) if (this.logger.isDebugEnabled())
this.logger.debug("Received response from " + method + ": " + response.getStatusLine().getStatusCode()); this.logger.debug("Received response from " + method + ": " + response.getStatusLine().getStatusCode());
Response responseObject = null;
if (response.getEntity() != null) {
InputStream istream = response.getEntity().getContent(); InputStream istream = response.getEntity().getContent();
try { try {
Response responseObject = this.omapper.readerFor(responseType).readValue(istream); responseObject = this.omapper.readerFor(responseType).readValue(istream);
responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode());
responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase());
return responseObject;
} finally { } finally {
istream.close(); istream.close();
} }
} else {
responseObject = this.omapper.readerFor(responseType).readValue("{}");
}
responseObject.setHttpStatusCode(response.getStatusLine().getStatusCode());
responseObject.setHttpStatusReason(response.getStatusLine().getReasonPhrase());
return responseObject;
} }
} }

View File

@ -1,4 +1,4 @@
package com.inteligr8.http; package com.inteligr8.github.http;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

View File

@ -1,4 +1,4 @@
package com.inteligr8.http; package com.inteligr8.github.http;
import java.io.IOException; import java.io.IOException;

View File

@ -2,13 +2,17 @@ package com.inteligr8.github.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.inteligr8.http.BaseResponse; import com.inteligr8.github.http.BaseResponse;
public class CreatePullRequest { public class CreatePullRequest {
private CreatePullRequest() { private CreatePullRequest() {
} }
public static String constructRequestPath(String repoName) {
return "/repos/" + repoName + "/" + httpPath;
}
public static String httpPath = "pulls"; public static String httpPath = "pulls";
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)

View File

@ -2,13 +2,17 @@ package com.inteligr8.github.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.inteligr8.http.BaseResponse; import com.inteligr8.github.http.BaseResponse;
public class CreateReference { public class CreateReference {
private CreateReference() { private CreateReference() {
} }
public static String constructRequestPath(String repoName) {
return "/repos/" + repoName + "/" + httpPath;
}
public static String httpPath = "git/refs"; public static String httpPath = "git/refs";
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)

View File

@ -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";
}