Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
Signed-off-by: Violeta Georgieva <[email protected]>
  • Loading branch information
violetagg committed Jan 22, 2025
1 parent 4b49d36 commit b16beb9
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,31 @@ public class Application {
public static void main(String[] args) {
HttpClient client =
HttpClient.create()
.proxyWhen((httpClientConfig, spec) -> { // only applied
if (httpClientConfig.uri().startsWith("https://example.com")) {
return Mono.justOrEmpty(
spec.type(ProxyProvider.Proxy.HTTP)
.host("proxy")
.port(8080)
.nonProxyHosts("localhost")
.connectTimeoutMillis(20_000)
);
}
.proxyWhen((httpClientConfig, spec) -> { // only applied
if (httpClientConfig.uri().startsWith("https://example.com")) {
return Mono.justOrEmpty(
spec.type(ProxyProvider.Proxy.HTTP)
.host("proxy")
.port(8080)
.nonProxyHosts("localhost")
.connectTimeoutMillis(20_000));
}

return Mono.empty();
})
.proxy(
spec -> spec.type(ProxyProvider.Proxy.HTTP)
.host("ignored-proxy-domain")
.port(9000)
.connectTimeoutMillis(20_000)
) // ignored
.noProxy(); // ignored
return Mono.empty();
})
.proxy(spec -> spec.type(ProxyProvider.Proxy.HTTP)
.host("ignored-proxy-domain")
.port(9000)
.connectTimeoutMillis(20_000)) // ignored
.noProxy(); // ignored

String response =
client.get()
.uri("https://example.com/")
.responseContent()
.aggregate()
.asString()
.block();
.uri("https://example.com/")
.responseContent()
.aggregate()
.asString()
.block();

System.out.println("Response " + response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1647,29 +1647,25 @@ public Mono<Void> warmup() {
* <p>When proxyWhen(...) is set, calls to proxy(...) and noProxy() methods are ignored.</p>
* <p>This method allows dynamic determination of proxy settings, applying or skipping the proxy based on the configured conditions.</p>
*
* @param proxyBuilder a consumer for proxy configuration
* @param proxyBuilder a deferred builder for proxy configuration
* @return a new {@link HttpClient} reference
* @since 1.2.3
*/
public final HttpClient proxyWhen(
BiFunction<HttpClientConfig, ? super ProxyProvider.TypeSpec, Mono<? extends ProxyProvider.Builder>> proxyBuilder) {
Objects.requireNonNull(proxyBuilder, "proxyBuilder");
HttpClient dup = duplicate();
dup.configuration()
.deferredConf(
config -> {
Mono<? extends ProxyProvider.Builder> mono = proxyBuilder.apply(config, ProxyProvider.builder());
if (mono == null || mono == Mono.<ProxyProvider.Builder>empty()) {
return Mono.just(config);
}

return mono.map(
builder -> {
config.proxyProvider(builder.build());
return config;
}
);
}
);
dup.configuration().deferredConf(config -> {
Mono<? extends ProxyProvider.Builder> mono = proxyBuilder.apply(config, ProxyProvider.builder());
if (mono == null || mono == Mono.<ProxyProvider.Builder>empty()) {
return Mono.just(config);
}

return mono.map(builder -> {
config.proxyProvider(builder.build());
return config;
});
});
return dup;
}

Expand Down
Loading

0 comments on commit b16beb9

Please sign in to comment.