Created
September 10, 2015 07:13
com.github.kevinsawicki.http.OkConnectionFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.kevinsawicki.http; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.okhttp.OkUrlFactory; | |
import java.io.IOException; | |
import java.net.HttpURLConnection; | |
import java.net.Proxy; | |
import java.net.URL; | |
/** | |
* A {@link HttpRequest.ConnectionFactory connection factory} which uses OkHttp. | |
* <p/> | |
* Call {@link HttpRequest#setConnectionFactory(HttpRequest.ConnectionFactory)} | |
* with an instance of this class to enable. | |
*/ | |
public class OkConnectionFactory implements HttpRequest.ConnectionFactory { | |
private final OkUrlFactory okUrlFactory; | |
public OkConnectionFactory() { | |
this(new OkUrlFactory(new OkHttpClient())); | |
} | |
public OkConnectionFactory(OkUrlFactory okUrlFactory) { | |
if (okUrlFactory == null) { | |
throw new NullPointerException("Client must not be null."); | |
} | |
this.okUrlFactory = okUrlFactory; | |
} | |
public OkHttpClient getOkHttpClient() { | |
return okUrlFactory.client(); | |
} | |
public HttpURLConnection create(URL url) throws IOException { | |
return okUrlFactory.open(url); | |
} | |
public HttpURLConnection create(URL url, Proxy proxy) throws IOException { | |
throw new UnsupportedOperationException( | |
"Per-connection proxy is not supported. Use OkHttpClient's setProxy instead."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment