comment stringlengths 1 45k | method_body stringlengths 23 281k | target_code stringlengths 0 5.16k | method_body_after stringlengths 12 281k | context_before stringlengths 8 543k | context_after stringlengths 8 543k |
|---|---|---|---|---|---|
I think this builder pattern needs consideration. For example, it would not be possible for person to create two HttpClient instances from this builder where the first one has a dispatcher and the second one does not, because they are unable to null it out. I would probably remove the constructor that takes the HttpClientBuilder and to only create that in the build() method here. | public HttpClient build() {
if (connectionPool != null) {
this.httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (dispatcher != null) {
this.httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
return new OkHttpAsyncHttpClient(this.httpClientBuilder.build());
} | } | public HttpClient build() {
OkHttpClient.Builder httpClientBuilder = this.okHttpClient == null
? new OkHttpClient.Builder()
: this.okHttpClient.newBuilder();
for (Interceptor interceptor : this.networkInterceptors) {
httpClientBuilder = httpClientBuilder.addNetworkInterceptor(interceptor);
}
if (this.readTimeout != null) {
httpClientBuilder = httpClientBuilder.readTimeout(this.readTimeout);
} else {
httpClientBuilder = httpClientBuilder.readTimeout(DEFAULT_READ_TIMEOUT);
}
if (this.connectionTimeout != null) {
httpClientBuilder = httpClientBuilder.connectTimeout(this.connectionTimeout);
} else {
httpClientBuilder = httpClientBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT);
}
if (this.connectionPool != null) {
httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (this.dispatcher != null) {
httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
httpClientBuilder = httpClientBuilder.proxy(this.proxy);
if (this.proxyAuthenticator != null) {
httpClientBuilder = httpClientBuilder.authenticator(this.proxyAuthenticator);
}
return new OkHttpAsyncHttpClient(httpClientBuilder.build());
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private okhttp3.OkHttpClient.Builder httpClientBuilder;
private Dispatcher dispatcher;
private ConnectionPool connectionPool;
private final static long DEFAULT_READ_TIMEOUT_IN_SEC = 120;
private final static long DEFAULT_CONNECT_TIMEOUT_IN_SEC = 60;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this(new okhttp3.OkHttpClient.Builder());
}
/**
* Creates OkHttpAsyncHttpClientBuilder from an existing OkHttp builder.
*
* @param httpClientBuilder the base builder to use.
*/
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient.Builder httpClientBuilder) {
Objects.requireNonNull(httpClientBuilder);
this.httpClientBuilder = httpClientBuilder
.readTimeout(DEFAULT_READ_TIMEOUT_IN_SEC, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_CONNECT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
}
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.httpClientBuilder.addNetworkInterceptor(networkInterceptor);
return this;
}
/**
* Set the read timeout, default is 120 seconds.
*
* @param timeout the timeout
* @param unit the time unit for the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(long timeout, TimeUnit unit) {
this.httpClientBuilder.readTimeout(timeout, unit);
return this;
}
/**
* Set the connection timeout, default is 60 seconds.
*
* @param timeout the timeout
* @param unit the time unit for the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(long timeout, TimeUnit unit) {
this.httpClientBuilder.connectTimeout(timeout, unit);
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.httpClientBuilder.proxy(proxy);
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.httpClientBuilder.proxyAuthenticator(proxyAuthenticator);
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private final okhttp3.OkHttpClient okHttpClient;
private final static Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(120);
private final static Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(60);
private List<Interceptor> networkInterceptors = new ArrayList<>();
private Duration readTimeout;
private Duration connectionTimeout;
private ConnectionPool connectionPool;
private Dispatcher dispatcher;
private java.net.Proxy proxy;
private Authenticator proxyAuthenticator;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this.okHttpClient = null;
}
/**
* Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.
*
* @param okHttpClient the httpclient
*/
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) {
this.okHttpClient = Objects.requireNonNull(okHttpClient, "okHttpClient == null");
}
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.networkInterceptors.add(networkInterceptor);
return this;
}
/**
* Add network layer interceptors to Http request pipeline.
*
* This replaces all previously-set interceptors.
*
* @param networkInterceptors the interceptors to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptors(List<Interceptor> networkInterceptors) {
this.networkInterceptors = Objects.requireNonNull(networkInterceptors);
return this;
}
/**
* Sets the read timeout.
*
* The default read timeout is 120 seconds.
*
* @param readTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Sets the connection timeout.
*
* The default read timeout is 60 seconds.
*
* @param connectionTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.proxyAuthenticator = Objects.requireNonNull(proxyAuthenticator, "proxyAuthenticator == null");
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
} |
Same here - empty array vs `Mono.empty()`. I am not sure if there's a difference between a `null` responseBody vs a non-null responsebody with empty byte array. Should we instead return `Mono.just(content)` where content could potentially be empty? | public Mono<byte[]> bodyAsByteArray() {
if (this.responseBody() == null) {
return Mono.empty();
} else {
return Mono.using(() -> this.responseBody(),
rb -> {
try {
byte[] content = rb.bytes();
return content.length == 0 ? Mono.empty() : Mono.just(content);
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
},
rb -> rb.close());
}
} | return content.length == 0 ? Mono.empty() : Mono.just(content); | public Mono<byte[]> bodyAsByteArray() {
if (this.responseBody() == null) {
return Mono.empty();
} else {
return Mono.using(() -> this.responseBody(),
rb -> {
try {
byte[] content = rb.bytes();
return content.length == 0 ? Mono.empty() : Mono.just(content);
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
},
rb -> rb.close());
}
} | class OkHttpResponse extends HttpResponse {
private final okhttp3.Response inner;
private final HttpHeaders headers;
private final static int BYTE_BUFFER_CHUNK_SIZE = 1024;
public OkHttpResponse(okhttp3.Response inner, HttpRequest request) {
this.inner = inner;
this.headers = fromOkHttpHeaders(this.inner.headers());
super.request(request);
}
@Override
public int statusCode() {
return this.inner.code();
}
@Override
public String headerValue(String name) {
return this.headers.value(name);
}
@Override
public HttpHeaders headers() {
return this.headers;
}
@Override
public Flux<ByteBuffer> body() {
return this.responseBody() != null
? toFluxByteBuffer(this.responseBody().byteStream())
: Flux.empty();
}
@Override
@Override
public Mono<String> bodyAsString() {
if (this.responseBody() == null) {
return Mono.empty();
} else {
return Mono.using(() -> this.responseBody(),
rb -> {
try {
String content = rb.string();
return content.length() == 0 ? Mono.empty() : Mono.just(content);
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
},
rb -> rb.close());
}
}
@Override
public Mono<String> bodyAsString(Charset charset) {
return bodyAsByteArray()
.map(bytes -> new String(bytes, charset));
}
@Override
public void close() {
if (this.inner.body() != null) {
this.inner.body().close();
}
}
private okhttp3.ResponseBody responseBody() {
return this.inner.body();
}
/**
* Creates azure-core HttpHeaders from okhttp headers.
*
* @param headers okhttp headers
* @return azure-core HttpHeaders
*/
private static HttpHeaders fromOkHttpHeaders(okhttp3.Headers headers) {
HttpHeaders httpHeaders = new HttpHeaders();
for (String headerName : headers.names()) {
httpHeaders.put(headerName, headers.get(headerName));
}
return httpHeaders;
}
/**
* Creates a Flux of ByteBuffer, with each ByteBuffer wrapping bytes read from the given
* InputStream.
*
* @param inputStream InputStream to back the Flux
* @return Flux of ByteBuffer backed by the InputStream
*/
private static Flux<ByteBuffer> toFluxByteBuffer(InputStream inputStream) {
Pair pair = new Pair();
return Flux.using(() -> inputStream,
is -> Flux.just(true)
.repeat()
.map(ignore -> {
byte[] buffer = new byte[BYTE_BUFFER_CHUNK_SIZE];
try {
int numBytes = is.read(buffer);
if (numBytes > 0) {
return pair.buffer(ByteBuffer.wrap(buffer, 0, numBytes)).readBytes(numBytes);
} else {
return pair.buffer(null).readBytes(numBytes);
}
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
})
.takeUntil(p -> p.readBytes() == -1)
.filter(p -> p.readBytes() > 0)
.map(p -> p.buffer()),
is -> {
try {
is.close();
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
}
);
}
private static class Pair {
private ByteBuffer byteBuffer;
private int readBytes;
ByteBuffer buffer() {
return this.byteBuffer;
}
int readBytes() {
return this.readBytes;
}
Pair buffer(ByteBuffer byteBuffer) {
this.byteBuffer = byteBuffer;
return this;
}
Pair readBytes(int cnt) {
this.readBytes = cnt;
return this;
}
}
} | class OkHttpResponse extends HttpResponse {
private final okhttp3.Response inner;
private final HttpHeaders headers;
private final static int BYTE_BUFFER_CHUNK_SIZE = 1024;
public OkHttpResponse(okhttp3.Response inner, HttpRequest request) {
this.inner = inner;
this.headers = fromOkHttpHeaders(this.inner.headers());
super.request(request);
}
@Override
public int statusCode() {
return this.inner.code();
}
@Override
public String headerValue(String name) {
return this.headers.value(name);
}
@Override
public HttpHeaders headers() {
return this.headers;
}
@Override
public Flux<ByteBuffer> body() {
return this.responseBody() != null
? toFluxByteBuffer(this.responseBody().byteStream())
: Flux.empty();
}
@Override
@Override
public Mono<String> bodyAsString() {
if (this.responseBody() == null) {
return Mono.empty();
} else {
return Mono.using(() -> this.responseBody(),
rb -> {
try {
String content = rb.string();
return content.length() == 0 ? Mono.empty() : Mono.just(content);
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
},
rb -> rb.close());
}
}
@Override
public Mono<String> bodyAsString(Charset charset) {
return bodyAsByteArray()
.map(bytes -> new String(bytes, charset));
}
@Override
public void close() {
if (this.inner.body() != null) {
this.inner.body().close();
}
}
private okhttp3.ResponseBody responseBody() {
return this.inner.body();
}
/**
* Creates azure-core HttpHeaders from okhttp headers.
*
* @param headers okhttp headers
* @return azure-core HttpHeaders
*/
private static HttpHeaders fromOkHttpHeaders(okhttp3.Headers headers) {
HttpHeaders httpHeaders = new HttpHeaders();
for (String headerName : headers.names()) {
httpHeaders.put(headerName, headers.get(headerName));
}
return httpHeaders;
}
/**
* Creates a Flux of ByteBuffer, with each ByteBuffer wrapping bytes read from the given
* InputStream.
*
* @param inputStream InputStream to back the Flux
* @return Flux of ByteBuffer backed by the InputStream
*/
private static Flux<ByteBuffer> toFluxByteBuffer(InputStream inputStream) {
Pair pair = new Pair();
return Flux.using(() -> inputStream,
is -> Flux.just(true)
.repeat()
.map(ignore -> {
byte[] buffer = new byte[BYTE_BUFFER_CHUNK_SIZE];
try {
int numBytes = is.read(buffer);
if (numBytes > 0) {
return pair.buffer(ByteBuffer.wrap(buffer, 0, numBytes)).readBytes(numBytes);
} else {
return pair.buffer(null).readBytes(numBytes);
}
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
})
.takeUntil(p -> p.readBytes() == -1)
.filter(p -> p.readBytes() > 0)
.map(p -> p.buffer()),
is -> {
try {
is.close();
} catch (IOException ioe) {
throw Exceptions.propagate(ioe);
}
}
);
}
private static class Pair {
private ByteBuffer byteBuffer;
private int readBytes;
ByteBuffer buffer() {
return this.byteBuffer;
}
int readBytes() {
return this.readBytes;
}
Pair buffer(ByteBuffer byteBuffer) {
this.byteBuffer = byteBuffer;
return this;
}
Pair readBytes(int cnt) {
this.readBytes = cnt;
return this;
}
}
} |
@Jonathan, thanks for the feedback. Agree with you on reusing builder, I will update PR. On the other hand, I just learned that okhttp3.OkHttpClient instance itself has a `builder()` method. A builder returned by this method can be used to create new OkHttpClient instance with different configuration but it enables OkHttpClient instances to share very expensive resources (e.g. socket pool). https://medium.com/@leandromazzuquini/if-you-are-using-okhttp-you-should-know-this-61d68e065a2b . This helpful for android scenario, I heard from @@bsiegel that android apps often want to share these expensive resources with other libraries it uses. Do you think one of the Ctr in Builder can take okhttp3.OkHttpClient whose builder it uses as base? | public HttpClient build() {
if (connectionPool != null) {
this.httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (dispatcher != null) {
this.httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
return new OkHttpAsyncHttpClient(this.httpClientBuilder.build());
} | } | public HttpClient build() {
OkHttpClient.Builder httpClientBuilder = this.okHttpClient == null
? new OkHttpClient.Builder()
: this.okHttpClient.newBuilder();
for (Interceptor interceptor : this.networkInterceptors) {
httpClientBuilder = httpClientBuilder.addNetworkInterceptor(interceptor);
}
if (this.readTimeout != null) {
httpClientBuilder = httpClientBuilder.readTimeout(this.readTimeout);
} else {
httpClientBuilder = httpClientBuilder.readTimeout(DEFAULT_READ_TIMEOUT);
}
if (this.connectionTimeout != null) {
httpClientBuilder = httpClientBuilder.connectTimeout(this.connectionTimeout);
} else {
httpClientBuilder = httpClientBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT);
}
if (this.connectionPool != null) {
httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (this.dispatcher != null) {
httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
httpClientBuilder = httpClientBuilder.proxy(this.proxy);
if (this.proxyAuthenticator != null) {
httpClientBuilder = httpClientBuilder.authenticator(this.proxyAuthenticator);
}
return new OkHttpAsyncHttpClient(httpClientBuilder.build());
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private okhttp3.OkHttpClient.Builder httpClientBuilder;
private Dispatcher dispatcher;
private ConnectionPool connectionPool;
private final static long DEFAULT_READ_TIMEOUT_IN_SEC = 120;
private final static long DEFAULT_CONNECT_TIMEOUT_IN_SEC = 60;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this(new okhttp3.OkHttpClient.Builder());
}
/**
* Creates OkHttpAsyncHttpClientBuilder from an existing OkHttp builder.
*
* @param httpClientBuilder the base builder to use.
*/
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient.Builder httpClientBuilder) {
Objects.requireNonNull(httpClientBuilder);
this.httpClientBuilder = httpClientBuilder
.readTimeout(DEFAULT_READ_TIMEOUT_IN_SEC, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_CONNECT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
}
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.httpClientBuilder.addNetworkInterceptor(networkInterceptor);
return this;
}
/**
* Set the read timeout, default is 120 seconds.
*
* @param timeout the timeout
* @param unit the time unit for the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(long timeout, TimeUnit unit) {
this.httpClientBuilder.readTimeout(timeout, unit);
return this;
}
/**
* Set the connection timeout, default is 60 seconds.
*
* @param timeout the timeout
* @param unit the time unit for the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(long timeout, TimeUnit unit) {
this.httpClientBuilder.connectTimeout(timeout, unit);
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.httpClientBuilder.proxy(proxy);
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.httpClientBuilder.proxyAuthenticator(proxyAuthenticator);
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private final okhttp3.OkHttpClient okHttpClient;
private final static Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(120);
private final static Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(60);
private List<Interceptor> networkInterceptors = new ArrayList<>();
private Duration readTimeout;
private Duration connectionTimeout;
private ConnectionPool connectionPool;
private Dispatcher dispatcher;
private java.net.Proxy proxy;
private Authenticator proxyAuthenticator;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this.okHttpClient = null;
}
/**
* Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.
*
* @param okHttpClient the httpclient
*/
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) {
this.okHttpClient = Objects.requireNonNull(okHttpClient, "okHttpClient == null");
}
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.networkInterceptors.add(networkInterceptor);
return this;
}
/**
* Add network layer interceptors to Http request pipeline.
*
* This replaces all previously-set interceptors.
*
* @param networkInterceptors the interceptors to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptors(List<Interceptor> networkInterceptors) {
this.networkInterceptors = Objects.requireNonNull(networkInterceptors);
return this;
}
/**
* Sets the read timeout.
*
* The default read timeout is 120 seconds.
*
* @param readTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Sets the connection timeout.
*
* The default read timeout is 60 seconds.
*
* @param connectionTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.proxyAuthenticator = Objects.requireNonNull(proxyAuthenticator, "proxyAuthenticator == null");
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
} |
Yes, we should support that | public HttpClient build() {
if (connectionPool != null) {
this.httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (dispatcher != null) {
this.httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
return new OkHttpAsyncHttpClient(this.httpClientBuilder.build());
} | } | public HttpClient build() {
OkHttpClient.Builder httpClientBuilder = this.okHttpClient == null
? new OkHttpClient.Builder()
: this.okHttpClient.newBuilder();
for (Interceptor interceptor : this.networkInterceptors) {
httpClientBuilder = httpClientBuilder.addNetworkInterceptor(interceptor);
}
if (this.readTimeout != null) {
httpClientBuilder = httpClientBuilder.readTimeout(this.readTimeout);
} else {
httpClientBuilder = httpClientBuilder.readTimeout(DEFAULT_READ_TIMEOUT);
}
if (this.connectionTimeout != null) {
httpClientBuilder = httpClientBuilder.connectTimeout(this.connectionTimeout);
} else {
httpClientBuilder = httpClientBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT);
}
if (this.connectionPool != null) {
httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (this.dispatcher != null) {
httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
httpClientBuilder = httpClientBuilder.proxy(this.proxy);
if (this.proxyAuthenticator != null) {
httpClientBuilder = httpClientBuilder.authenticator(this.proxyAuthenticator);
}
return new OkHttpAsyncHttpClient(httpClientBuilder.build());
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private okhttp3.OkHttpClient.Builder httpClientBuilder;
private Dispatcher dispatcher;
private ConnectionPool connectionPool;
private final static long DEFAULT_READ_TIMEOUT_IN_SEC = 120;
private final static long DEFAULT_CONNECT_TIMEOUT_IN_SEC = 60;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this(new okhttp3.OkHttpClient.Builder());
}
/**
* Creates OkHttpAsyncHttpClientBuilder from an existing OkHttp builder.
*
* @param httpClientBuilder the base builder to use.
*/
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient.Builder httpClientBuilder) {
Objects.requireNonNull(httpClientBuilder);
this.httpClientBuilder = httpClientBuilder
.readTimeout(DEFAULT_READ_TIMEOUT_IN_SEC, TimeUnit.SECONDS)
.connectTimeout(DEFAULT_CONNECT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
}
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.httpClientBuilder.addNetworkInterceptor(networkInterceptor);
return this;
}
/**
* Set the read timeout, default is 120 seconds.
*
* @param timeout the timeout
* @param unit the time unit for the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(long timeout, TimeUnit unit) {
this.httpClientBuilder.readTimeout(timeout, unit);
return this;
}
/**
* Set the connection timeout, default is 60 seconds.
*
* @param timeout the timeout
* @param unit the time unit for the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(long timeout, TimeUnit unit) {
this.httpClientBuilder.connectTimeout(timeout, unit);
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.httpClientBuilder.proxy(proxy);
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.httpClientBuilder.proxyAuthenticator(proxyAuthenticator);
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private final okhttp3.OkHttpClient okHttpClient;
private final static Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(120);
private final static Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(60);
private List<Interceptor> networkInterceptors = new ArrayList<>();
private Duration readTimeout;
private Duration connectionTimeout;
private ConnectionPool connectionPool;
private Dispatcher dispatcher;
private java.net.Proxy proxy;
private Authenticator proxyAuthenticator;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this.okHttpClient = null;
}
/**
* Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.
*
* @param okHttpClient the httpclient
*/
public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) {
this.okHttpClient = Objects.requireNonNull(okHttpClient, "okHttpClient == null");
}
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.networkInterceptors.add(networkInterceptor);
return this;
}
/**
* Add network layer interceptors to Http request pipeline.
*
* This replaces all previously-set interceptors.
*
* @param networkInterceptors the interceptors to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptors(List<Interceptor> networkInterceptors) {
this.networkInterceptors = Objects.requireNonNull(networkInterceptors);
return this;
}
/**
* Sets the read timeout.
*
* The default read timeout is 120 seconds.
*
* @param readTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Sets the connection timeout.
*
* The default read timeout is 60 seconds.
*
* @param connectionTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.proxyAuthenticator = Objects.requireNonNull(proxyAuthenticator, "proxyAuthenticator == null");
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
} |
nit: "okHttpClient cannot be null" to make it easier for user to debug. | public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) {
this.okHttpClient = Objects.requireNonNull(okHttpClient, "okHttpClient == null");
} | this.okHttpClient = Objects.requireNonNull(okHttpClient, "okHttpClient == null"); | public OkHttpAsyncHttpClientBuilder(okhttp3.OkHttpClient okHttpClient) {
this.okHttpClient = Objects.requireNonNull(okHttpClient, "okHttpClient == null");
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private final okhttp3.OkHttpClient okHttpClient;
private final static Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(120);
private final static Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(60);
private List<Interceptor> networkInterceptors = new ArrayList<>();
private Duration readTimeout;
private Duration connectionTimeout;
private ConnectionPool connectionPool;
private Dispatcher dispatcher;
private java.net.Proxy proxy;
private Authenticator proxyAuthenticator;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this.okHttpClient = null;
}
/**
* Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.
*
* @param okHttpClient the httpclient
*/
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.networkInterceptors.add(networkInterceptor);
return this;
}
/**
* Add network layer interceptors to Http request pipeline.
*
* This replaces all previously-set interceptors.
*
* @param networkInterceptors the interceptors to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptors(List<Interceptor> networkInterceptors) {
this.networkInterceptors = Objects.requireNonNull(networkInterceptors);
return this;
}
/**
* Sets the read timeout.
*
* The default read timeout is 120 seconds.
*
* @param readTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Sets the connection timeout.
*
* The default read timeout is 60 seconds.
*
* @param connectionTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.proxyAuthenticator = Objects.requireNonNull(proxyAuthenticator, "proxyAuthenticator == null");
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
public HttpClient build() {
OkHttpClient.Builder httpClientBuilder = this.okHttpClient == null
? new OkHttpClient.Builder()
: this.okHttpClient.newBuilder();
for (Interceptor interceptor : this.networkInterceptors) {
httpClientBuilder = httpClientBuilder.addNetworkInterceptor(interceptor);
}
if (this.readTimeout != null) {
httpClientBuilder = httpClientBuilder.readTimeout(this.readTimeout);
} else {
httpClientBuilder = httpClientBuilder.readTimeout(DEFAULT_READ_TIMEOUT);
}
if (this.connectionTimeout != null) {
httpClientBuilder = httpClientBuilder.connectTimeout(this.connectionTimeout);
} else {
httpClientBuilder = httpClientBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT);
}
if (this.connectionPool != null) {
httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (this.dispatcher != null) {
httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
httpClientBuilder = httpClientBuilder.proxy(this.proxy);
if (this.proxyAuthenticator != null) {
httpClientBuilder = httpClientBuilder.authenticator(this.proxyAuthenticator);
}
return new OkHttpAsyncHttpClient(httpClientBuilder.build());
}
} | class OkHttpAsyncHttpClientBuilder {
private final ClientLogger logger = new ClientLogger(OkHttpAsyncHttpClientBuilder.class);
private final okhttp3.OkHttpClient okHttpClient;
private final static Duration DEFAULT_READ_TIMEOUT = Duration.ofSeconds(120);
private final static Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(60);
private List<Interceptor> networkInterceptors = new ArrayList<>();
private Duration readTimeout;
private Duration connectionTimeout;
private ConnectionPool connectionPool;
private Dispatcher dispatcher;
private java.net.Proxy proxy;
private Authenticator proxyAuthenticator;
/**
* Creates OkHttpAsyncHttpClientBuilder.
*/
public OkHttpAsyncHttpClientBuilder() {
this.okHttpClient = null;
}
/**
* Creates OkHttpAsyncHttpClientBuilder from the builder of an existing OkHttpClient.
*
* @param okHttpClient the httpclient
*/
/**
* Add a network layer interceptor to Http request pipeline.
*
* @param networkInterceptor the interceptor to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptor(Interceptor networkInterceptor) {
Objects.requireNonNull(networkInterceptor);
this.networkInterceptors.add(networkInterceptor);
return this;
}
/**
* Add network layer interceptors to Http request pipeline.
*
* This replaces all previously-set interceptors.
*
* @param networkInterceptors the interceptors to add
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder networkInterceptors(List<Interceptor> networkInterceptors) {
this.networkInterceptors = Objects.requireNonNull(networkInterceptors);
return this;
}
/**
* Sets the read timeout.
*
* The default read timeout is 120 seconds.
*
* @param readTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder readTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Sets the connection timeout.
*
* The default read timeout is 60 seconds.
*
* @param connectionTimeout the timeout
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionTimeout(Duration connectionTimeout) {
this.connectionTimeout = connectionTimeout;
return this;
}
/**
* Sets the Http connection pool.
*
* @param connectionPool the OkHttp connection pool to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder connectionPool(ConnectionPool connectionPool) {
this.connectionPool = Objects.requireNonNull(connectionPool, "connectionPool == null");
return this;
}
/**
* Sets the dispatcher that also composes the thread pool for executing HTTP requests.
*
* @param dispatcher the dispatcher to use
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder dispatcher(Dispatcher dispatcher) {
this.dispatcher = Objects.requireNonNull(dispatcher, "dispatcher == null");
return this;
}
/**
* Sets the proxy.
*
* @param proxy the proxy
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxy(java.net.Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Sets the proxy authenticator.
*
* @param proxyAuthenticator the proxy authenticator
* @return the builder
*/
public OkHttpAsyncHttpClientBuilder proxyAuthenticator(Authenticator proxyAuthenticator) {
this.proxyAuthenticator = Objects.requireNonNull(proxyAuthenticator, "proxyAuthenticator == null");
return this;
}
/**
* Build a HttpClient with current configurations.
*
* @return a {@link HttpClient}.
*/
public HttpClient build() {
OkHttpClient.Builder httpClientBuilder = this.okHttpClient == null
? new OkHttpClient.Builder()
: this.okHttpClient.newBuilder();
for (Interceptor interceptor : this.networkInterceptors) {
httpClientBuilder = httpClientBuilder.addNetworkInterceptor(interceptor);
}
if (this.readTimeout != null) {
httpClientBuilder = httpClientBuilder.readTimeout(this.readTimeout);
} else {
httpClientBuilder = httpClientBuilder.readTimeout(DEFAULT_READ_TIMEOUT);
}
if (this.connectionTimeout != null) {
httpClientBuilder = httpClientBuilder.connectTimeout(this.connectionTimeout);
} else {
httpClientBuilder = httpClientBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT);
}
if (this.connectionPool != null) {
httpClientBuilder = httpClientBuilder.connectionPool(connectionPool);
}
if (this.dispatcher != null) {
httpClientBuilder = httpClientBuilder.dispatcher(dispatcher);
}
httpClientBuilder = httpClientBuilder.proxy(this.proxy);
if (this.proxyAuthenticator != null) {
httpClientBuilder = httpClientBuilder.authenticator(this.proxyAuthenticator);
}
return new OkHttpAsyncHttpClient(httpClientBuilder.build());
}
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.