Skip to content

Commit a18cde2

Browse files
committed
Abstracted away HTTP connector and added OkHttp implementation for convenience
1 parent ee98e97 commit a18cde2

5 files changed

Lines changed: 75 additions & 1 deletion

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
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>

src/main/java/org/kohsuke/github/GitHub.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

src/main/java/org/kohsuke/github/Requester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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.)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)