+ * For example, you can implement this to st custom timeouts. + * + * @author Kohsuke Kawaguchi + */ +public interface HttpConnector { + /** + * Opens a connection to the given URL. + */ + HttpURLConnection connect(URL url) throws IOException; + + /** + * Default implementation that uses {@link URL#openConnection()}. + */ + HttpConnector DEFAULT = new HttpConnector() { + public HttpURLConnection connect(URL url) throws IOException { + return (HttpURLConnection) url.openConnection(); + } + }; +} diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index fa9bc3191c..ef42a6e02f 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -25,7 +25,6 @@ import org.apache.commons.io.IOUtils; -import javax.net.ssl.HttpsURLConnection; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -294,7 +293,7 @@ private void findNextURL(HttpURLConnection uc) throws MalformedURLException { private HttpURLConnection setupConnection(URL url) throws IOException { - HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); + HttpURLConnection uc = root.getConnector().connect(url); // if the authentication is needed but no credential is given, try it anyway (so that some calls // that do work with anonymous access in the reduced form should still work.) diff --git a/src/main/java/org/kohsuke/github/extras/OkHttpConnector.java b/src/main/java/org/kohsuke/github/extras/OkHttpConnector.java new file mode 100644 index 0000000000..671c32eaca --- /dev/null +++ b/src/main/java/org/kohsuke/github/extras/OkHttpConnector.java @@ -0,0 +1,31 @@ +package org.kohsuke.github.extras; + +import com.squareup.okhttp.OkHttpClient; +import org.kohsuke.github.HttpConnector; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * {@link HttpConnector} for {@link OkHttpClient}. + * + * Unlike {@link #DEFAULT}, OkHttp does response caching. + * Making a conditional request against GitHubAPI and receiving a 304 + * response does not count against the rate limit. + * See http://developer.github.com/v3/#conditional-requests + * + * @author Roberto Tyley + * @author Kohsuke Kawaguchi + */ +public class OkHttpConnector implements HttpConnector { + private final OkHttpClient client; + + public OkHttpConnector(OkHttpClient client) { + this.client = client; + } + + public HttpURLConnection connect(URL url) throws IOException { + return client.open(url); + } +} diff --git a/src/site/markdown/index.md b/src/site/markdown/index.md index ba9fbb5287..3956df708a 100644 --- a/src/site/markdown/index.md +++ b/src/site/markdown/index.md @@ -7,6 +7,8 @@ operations that act on them as defined as methods (such as `GHUser.follow()`), a are used in favor of using string handle (such as `GHUser.isMemberOf(GHOrganization)` instead of `GHUser.isMemberOf(String)`) +The library supports both github.com and GitHub Enterprise. + There are some corners of the GitHub API that's not yet implemented, but the library is implemented with the right abstractions and libraries to make it very easy to improve the coverage. @@ -34,3 +36,10 @@ Alternatively, you can have just the OAuth token in this file: oauth=4d98173f7c075527cb64878561d1fe70 +OkHttp +---- +This library comes with a pluggable connector to use different HTTP client implementations +through `HttpConnector`. In particular, this means you can use [OkHttp](http://square.github.io/okhttp/), +so we can make use of it's HTTP response cache. +Making a conditional request against the GitHub API and receiving a 304 response +[does not count against the rate limit](http://developer.github.com/v3/#conditional-requests). \ No newline at end of file