Skip to content

Commit 164fca2

Browse files
feat(client): support proxy authentication
1 parent fa5904f commit 164fca2

5 files changed

Lines changed: 258 additions & 85 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,22 @@ FinchClient client = FinchOkHttpClient.builder()
471471
.build();
472472
```
473473

474+
If the proxy responds with `407 Proxy Authentication Required`, supply credentials by also configuring `proxyAuthenticator`:
475+
476+
```java
477+
import com.tryfinch.api.client.FinchClient;
478+
import com.tryfinch.api.client.okhttp.FinchOkHttpClient;
479+
import com.tryfinch.api.core.http.ProxyAuthenticator;
480+
481+
FinchClient client = FinchOkHttpClient.builder()
482+
.fromEnv()
483+
.proxy(...)
484+
// Or a custom implementation of `ProxyAuthenticator`.
485+
.proxyAuthenticator(ProxyAuthenticator.basic("username", "password"))
486+
.accessToken("My Access Token")
487+
.build();
488+
```
489+
474490
### Connection pooling
475491

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

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/FinchOkHttpClient.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.tryfinch.api.core.Timeout
1111
import com.tryfinch.api.core.http.AsyncStreamResponse
1212
import com.tryfinch.api.core.http.Headers
1313
import com.tryfinch.api.core.http.HttpClient
14+
import com.tryfinch.api.core.http.ProxyAuthenticator
1415
import com.tryfinch.api.core.http.QueryParams
1516
import com.tryfinch.api.core.jsonMapper
1617
import java.net.Proxy
@@ -49,6 +50,7 @@ class FinchOkHttpClient 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 FinchOkHttpClient 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
*
@@ -396,6 +412,7 @@ class FinchOkHttpClient private constructor() {
396412
OkHttpClient.builder()
397413
.timeout(clientOptions.timeout())
398414
.proxy(proxy)
415+
.proxyAuthenticator(proxyAuthenticator)
399416
.maxIdleConnections(maxIdleConnections)
400417
.keepAliveDuration(keepAliveDuration)
401418
.dispatcherExecutorService(dispatcherExecutorService)

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/FinchOkHttpClientAsync.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.tryfinch.api.core.Timeout
1111
import com.tryfinch.api.core.http.AsyncStreamResponse
1212
import com.tryfinch.api.core.http.Headers
1313
import com.tryfinch.api.core.http.HttpClient
14+
import com.tryfinch.api.core.http.ProxyAuthenticator
1415
import com.tryfinch.api.core.http.QueryParams
1516
import com.tryfinch.api.core.jsonMapper
1617
import java.net.Proxy
@@ -49,6 +50,7 @@ class FinchOkHttpClientAsync 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 FinchOkHttpClientAsync 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
*
@@ -396,6 +412,7 @@ class FinchOkHttpClientAsync private constructor() {
396412
OkHttpClient.builder()
397413
.timeout(clientOptions.timeout())
398414
.proxy(proxy)
415+
.proxyAuthenticator(proxyAuthenticator)
399416
.maxIdleConnections(maxIdleConnections)
400417
.keepAliveDuration(keepAliveDuration)
401418
.dispatcherExecutorService(dispatcherExecutorService)

0 commit comments

Comments
 (0)