Skip to content

Commit 60effd3

Browse files
feat(client): support proxy authentication
1 parent 92128db commit 60effd3

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
@@ -723,6 +723,21 @@ ImageKitClient client = ImageKitOkHttpClient.builder()
723723
.build();
724724
```
725725

726+
If the proxy responds with `407 Proxy Authentication Required`, supply credentials by also configuring `proxyAuthenticator`:
727+
728+
```java
729+
import io.imagekit.client.ImageKitClient;
730+
import io.imagekit.client.okhttp.ImageKitOkHttpClient;
731+
import io.imagekit.core.http.ProxyAuthenticator;
732+
733+
ImageKitClient client = ImageKitOkHttpClient.builder()
734+
.fromEnv()
735+
.proxy(...)
736+
// Or a custom implementation of `ProxyAuthenticator`.
737+
.proxyAuthenticator(ProxyAuthenticator.basic("username", "password"))
738+
.build();
739+
```
740+
726741
### Connection pooling
727742

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

image-kit-java-client-okhttp/src/main/kotlin/io/imagekit/client/okhttp/ImageKitOkHttpClient.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.imagekit.core.Sleeper
1010
import io.imagekit.core.Timeout
1111
import io.imagekit.core.http.Headers
1212
import io.imagekit.core.http.HttpClient
13+
import io.imagekit.core.http.ProxyAuthenticator
1314
import io.imagekit.core.http.QueryParams
1415
import io.imagekit.core.jsonMapper
1516
import java.net.Proxy
@@ -47,6 +48,7 @@ class ImageKitOkHttpClient private constructor() {
4748
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
4849
private var dispatcherExecutorService: ExecutorService? = null
4950
private var proxy: Proxy? = null
51+
private var proxyAuthenticator: ProxyAuthenticator? = null
5052
private var maxIdleConnections: Int? = null
5153
private var keepAliveDuration: Duration? = null
5254
private var sslSocketFactory: SSLSocketFactory? = null
@@ -77,6 +79,20 @@ class ImageKitOkHttpClient private constructor() {
7779
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
7880
fun proxy(proxy: Optional<Proxy>) = proxy(proxy.getOrNull())
7981

82+
/**
83+
* Provides credentials when an HTTP proxy responds with `407 Proxy Authentication
84+
* Required`.
85+
*/
86+
fun proxyAuthenticator(proxyAuthenticator: ProxyAuthenticator?) = apply {
87+
this.proxyAuthenticator = proxyAuthenticator
88+
}
89+
90+
/**
91+
* Alias for calling [Builder.proxyAuthenticator] with `proxyAuthenticator.orElse(null)`.
92+
*/
93+
fun proxyAuthenticator(proxyAuthenticator: Optional<ProxyAuthenticator>) =
94+
proxyAuthenticator(proxyAuthenticator.getOrNull())
95+
8096
/**
8197
* The maximum number of idle connections kept by the underlying OkHttp connection pool.
8298
*
@@ -391,6 +407,7 @@ class ImageKitOkHttpClient private constructor() {
391407
OkHttpClient.builder()
392408
.timeout(clientOptions.timeout())
393409
.proxy(proxy)
410+
.proxyAuthenticator(proxyAuthenticator)
394411
.maxIdleConnections(maxIdleConnections)
395412
.keepAliveDuration(keepAliveDuration)
396413
.dispatcherExecutorService(dispatcherExecutorService)

image-kit-java-client-okhttp/src/main/kotlin/io/imagekit/client/okhttp/ImageKitOkHttpClientAsync.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.imagekit.core.Sleeper
1010
import io.imagekit.core.Timeout
1111
import io.imagekit.core.http.Headers
1212
import io.imagekit.core.http.HttpClient
13+
import io.imagekit.core.http.ProxyAuthenticator
1314
import io.imagekit.core.http.QueryParams
1415
import io.imagekit.core.jsonMapper
1516
import java.net.Proxy
@@ -47,6 +48,7 @@ class ImageKitOkHttpClientAsync private constructor() {
4748
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
4849
private var dispatcherExecutorService: ExecutorService? = null
4950
private var proxy: Proxy? = null
51+
private var proxyAuthenticator: ProxyAuthenticator? = null
5052
private var maxIdleConnections: Int? = null
5153
private var keepAliveDuration: Duration? = null
5254
private var sslSocketFactory: SSLSocketFactory? = null
@@ -77,6 +79,20 @@ class ImageKitOkHttpClientAsync private constructor() {
7779
/** Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
7880
fun proxy(proxy: Optional<Proxy>) = proxy(proxy.getOrNull())
7981

82+
/**
83+
* Provides credentials when an HTTP proxy responds with `407 Proxy Authentication
84+
* Required`.
85+
*/
86+
fun proxyAuthenticator(proxyAuthenticator: ProxyAuthenticator?) = apply {
87+
this.proxyAuthenticator = proxyAuthenticator
88+
}
89+
90+
/**
91+
* Alias for calling [Builder.proxyAuthenticator] with `proxyAuthenticator.orElse(null)`.
92+
*/
93+
fun proxyAuthenticator(proxyAuthenticator: Optional<ProxyAuthenticator>) =
94+
proxyAuthenticator(proxyAuthenticator.getOrNull())
95+
8096
/**
8197
* The maximum number of idle connections kept by the underlying OkHttp connection pool.
8298
*
@@ -391,6 +407,7 @@ class ImageKitOkHttpClientAsync private constructor() {
391407
OkHttpClient.builder()
392408
.timeout(clientOptions.timeout())
393409
.proxy(proxy)
410+
.proxyAuthenticator(proxyAuthenticator)
394411
.maxIdleConnections(maxIdleConnections)
395412
.keepAliveDuration(keepAliveDuration)
396413
.dispatcherExecutorService(dispatcherExecutorService)

0 commit comments

Comments
 (0)