File tree Expand file tree Collapse file tree
src/main/java/org/kohsuke/github Expand file tree Collapse file tree Original file line number Diff line number Diff line change 9292 <version >3.1.0.201310021548-r</version >
9393 <scope >test</scope >
9494 </dependency >
95+ <dependency >
96+ <groupId >com.squareup.okhttp</groupId >
97+ <artifactId >okhttp</artifactId >
98+ <version >1.5.3</version >
99+ <scope >optional</scope >
100+ </dependency >
95101 </dependencies >
96102 <repositories >
97103 <repository >
Original file line number Diff line number Diff line change @@ -68,6 +68,8 @@ public class GitHub {
6868
6969 private final String apiUrl ;
7070
71+ private HttpConnector connector = HttpConnector .DEFAULT ;
72+
7173 /**
7274 * Connects to GitHub.com
7375 */
@@ -201,6 +203,17 @@ public boolean isAnonymous() {
201203 return login ==null && encodedAuthorization ==null ;
202204 }
203205
206+ public HttpConnector getConnector () {
207+ return connector ;
208+ }
209+
210+ /**
211+ * Sets the custom connector used to make requests to GitHub.
212+ */
213+ public void setConnector (HttpConnector connector ) {
214+ this .connector = connector ;
215+ }
216+
204217 /*package*/ void requireCredential () {
205218 if (isAnonymous ())
206219 throw new IllegalStateException ("This operation requires a credential but none is given to the GitHub constructor" );
Original file line number Diff line number Diff line change 1+ package org .kohsuke .github ;
2+
3+ import java .io .IOException ;
4+ import java .net .HttpURLConnection ;
5+ import java .net .URL ;
6+
7+ /**
8+ * @author Kohsuke Kawaguchi
9+ */
10+ public interface HttpConnector {
11+ /**
12+ * Opens a connection to the given URL.
13+ */
14+ HttpURLConnection connect (URL url ) throws IOException ;
15+
16+ /**
17+ * Default implementation that uses {@link URL#openConnection()}.
18+ */
19+ HttpConnector DEFAULT = new HttpConnector () {
20+ public HttpURLConnection connect (URL url ) throws IOException {
21+ return (HttpURLConnection ) url .openConnection ();
22+ }
23+ };
24+ }
Original file line number Diff line number Diff line change @@ -293,7 +293,7 @@ private void findNextURL(HttpURLConnection uc) throws MalformedURLException {
293293
294294
295295 private HttpURLConnection setupConnection (URL url ) throws IOException {
296- HttpURLConnection uc = ( HttpURLConnection ) url . openConnection ( );
296+ HttpURLConnection uc = root . getConnector (). connect ( url );
297297
298298 // if the authentication is needed but no credential is given, try it anyway (so that some calls
299299 // that do work with anonymous access in the reduced form should still work.)
Original file line number Diff line number Diff line change 1+ package org .kohsuke .github .extras ;
2+
3+ import com .squareup .okhttp .OkHttpClient ;
4+ import org .kohsuke .github .HttpConnector ;
5+
6+ import java .io .IOException ;
7+ import java .net .HttpURLConnection ;
8+ import java .net .URL ;
9+
10+ /**
11+ * {@link HttpConnector} for {@link OkHttpClient}.
12+ *
13+ * Unlike {@link #DEFAULT}, OkHttp does response caching.
14+ * Making a conditional request against GitHubAPI and receiving a 304
15+ * response does not count against the rate limit.
16+ * See http://developer.github.com/v3/#conditional-requests
17+ *
18+ * @author Roberto Tyley
19+ * @author Kohsuke Kawaguchi
20+ */
21+ public class OkHttpConnector implements HttpConnector {
22+ private final OkHttpClient client ;
23+
24+ public OkHttpConnector (OkHttpClient client ) {
25+ this .client = client ;
26+ }
27+
28+ public HttpURLConnection connect (URL url ) throws IOException {
29+ return client .open (url );
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments