Skip to content

Commit 93ca773

Browse files
feat(client): support proxy authentication
1 parent be74413 commit 93ca773

5 files changed

Lines changed: 257 additions & 85 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,21 @@ ArcadeClient client = ArcadeOkHttpClient.builder()
480480
.build();
481481
```
482482

483+
If the proxy responds with `407 Proxy Authentication Required`, supply credentials by also configuring `proxyAuthenticator`:
484+
485+
```java
486+
import dev.arcade.client.ArcadeClient;
487+
import dev.arcade.client.okhttp.ArcadeOkHttpClient;
488+
import dev.arcade.core.http.ProxyAuthenticator;
489+
490+
ArcadeClient client = ArcadeOkHttpClient.builder()
491+
.fromEnv()
492+
.proxy(...)
493+
// Or a custom implementation of `ProxyAuthenticator`.
494+
.proxyAuthenticator(ProxyAuthenticator.basic("username", "password"))
495+
.build();
496+
```
497+
483498
### Connection pooling
484499

485500
To customize the underlying OkHttp connection pool, configure the client using the `maxIdleConnections` and `keepAliveDuration` methods:

‎arcade-java-client-okhttp/src/main/kotlin/dev/arcade/client/okhttp/ArcadeOkHttpClient.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dev.arcade.core.Timeout
1111
import dev.arcade.core.http.AsyncStreamResponse
1212
import dev.arcade.core.http.Headers
1313
import dev.arcade.core.http.HttpClient
14+
import dev.arcade.core.http.ProxyAuthenticator
1415
import dev.arcade.core.http.QueryParams
1516
import dev.arcade.core.jsonMapper
1617
import java.net.Proxy
@@ -49,6 +50,7 @@ class ArcadeOkHttpClient private constructor() {
4950
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
5051
private var dispatcherExecutorService: ExecutorService? = null
5152
private var proxy: Proxy? = null
53+
private var proxyAuthenticator: ProxyAuthenticator? = null
5254
private var maxIdleConnections: Int? = null
5355
private var keepAliveDuration: Duration? = null
5456
private var sslSocketFactory: SSLSocketFactory? = null
@@ -79,6 +81,20 @@ class ArcadeOkHttpClient private constructor() {
7981
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
8082
fun proxy(proxy: Optional<Proxy>) = proxy(proxy.getOrNull())
8183

84+
/**
85+
* Provides credentials when an HTTP proxy responds with `407 Proxy Authentication
86+
* Required`.
87+
*/
88+
fun proxyAuthenticator(proxyAuthenticator: ProxyAuthenticator?) = apply {
89+
this.proxyAuthenticator = proxyAuthenticator
90+
}
91+
92+
/**
93+
* Alias for calling [Builder.proxyAuthenticator] with `proxyAuthenticator.orElse(null)`.
94+
*/
95+
fun proxyAuthenticator(proxyAuthenticator: Optional<ProxyAuthenticator>) =
96+
proxyAuthenticator(proxyAuthenticator.getOrNull())
97+
8298
/**
8399
* The maximum number of idle connections kept by the underlying OkHttp connection pool.
84100
*
@@ -376,6 +392,7 @@ class ArcadeOkHttpClient private constructor() {
376392
OkHttpClient.builder()
377393
.timeout(clientOptions.timeout())
378394
.proxy(proxy)
395+
.proxyAuthenticator(proxyAuthenticator)
379396
.maxIdleConnections(maxIdleConnections)
380397
.keepAliveDuration(keepAliveDuration)
381398
.dispatcherExecutorService(dispatcherExecutorService)

‎arcade-java-client-okhttp/src/main/kotlin/dev/arcade/client/okhttp/ArcadeOkHttpClientAsync.kt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dev.arcade.core.Timeout
1111
import dev.arcade.core.http.AsyncStreamResponse
1212
import dev.arcade.core.http.Headers
1313
import dev.arcade.core.http.HttpClient
14+
import dev.arcade.core.http.ProxyAuthenticator
1415
import dev.arcade.core.http.QueryParams
1516
import dev.arcade.core.jsonMapper
1617
import java.net.Proxy
@@ -49,6 +50,7 @@ class ArcadeOkHttpClientAsync private constructor() {
4950
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
5051
private var dispatcherExecutorService: ExecutorService? = null
5152
private var proxy: Proxy? = null
53+
private var proxyAuthenticator: ProxyAuthenticator? = null
5254
private var maxIdleConnections: Int? = null
5355
private var keepAliveDuration: Duration? = null
5456
private var sslSocketFactory: SSLSocketFactory? = null
@@ -79,6 +81,20 @@ class ArcadeOkHttpClientAsync private constructor() {
7981
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
8082
fun proxy(proxy: Optional<Proxy>) = proxy(proxy.getOrNull())
8183

84+
/**
85+
* Provides credentials when an HTTP proxy responds with `407 Proxy Authentication
86+
* Required`.
87+
*/
88+
fun proxyAuthenticator(proxyAuthenticator: ProxyAuthenticator?) = apply {
89+
this.proxyAuthenticator = proxyAuthenticator
90+
}
91+
92+
/**
93+
* Alias for calling [Builder.proxyAuthenticator] with `proxyAuthenticator.orElse(null)`.
94+
*/
95+
fun proxyAuthenticator(proxyAuthenticator: Optional<ProxyAuthenticator>) =
96+
proxyAuthenticator(proxyAuthenticator.getOrNull())
97+
8298
/**
8399
* The maximum number of idle connections kept by the underlying OkHttp connection pool.
84100
*
@@ -376,6 +392,7 @@ class ArcadeOkHttpClientAsync private constructor() {
376392
OkHttpClient.builder()
377393
.timeout(clientOptions.timeout())
378394
.proxy(proxy)
395+
.proxyAuthenticator(proxyAuthenticator)
379396
.maxIdleConnections(maxIdleConnections)
380397
.keepAliveDuration(keepAliveDuration)
381398
.dispatcherExecutorService(dispatcherExecutorService)

0 commit comments

Comments
 (0)