@@ -140,10 +140,15 @@ private WebServiceClient(Builder builder) {
140140 .build ();
141141
142142 requestTimeout = builder .requestTimeout ;
143- httpClient = HttpClient .newBuilder ()
144- .connectTimeout (builder .connectTimeout )
145- .proxy (builder .proxy )
146- .build ();
143+
144+ if (builder .httpClient != null ) {
145+ httpClient = builder .httpClient ;
146+ } else {
147+ httpClient = HttpClient .newBuilder ()
148+ .connectTimeout (builder .connectTimeout )
149+ .proxy (builder .proxy )
150+ .build ();
151+ }
147152 }
148153
149154 /**
@@ -171,11 +176,12 @@ public static final class Builder {
171176 int port = 443 ;
172177 boolean useHttps = true ;
173178
174- Duration connectTimeout = Duration . ofSeconds ( 3 ) ;
179+ Duration connectTimeout = null ;
175180 Duration requestTimeout = Duration .ofSeconds (20 );
176181
177182 List <String > locales = Collections .singletonList ("en" );
178- private ProxySelector proxy = ProxySelector .getDefault ();
183+ private ProxySelector proxy = null ;
184+ private HttpClient httpClient = null ;
179185
180186 /**
181187 * @param accountId Your MaxMind account ID.
@@ -296,11 +302,43 @@ public Builder proxy(ProxySelector val) {
296302 return this ;
297303 }
298304
305+ /**
306+ * @param val the custom HttpClient to use for requests. When providing a
307+ * custom HttpClient, you cannot also set connectTimeout or proxy
308+ * parameters as these should be configured on the provided client.
309+ * @return Builder object
310+ */
311+ public Builder httpClient (HttpClient val ) {
312+ this .httpClient = val ;
313+ return this ;
314+ }
315+
299316 /**
300317 * @return an instance of {@code WebServiceClient} created from the
301318 * fields set on this builder.
319+ * @throws IllegalArgumentException if httpClient is provided along with
320+ * connectTimeout or proxy settings
302321 */
303322 public WebServiceClient build () {
323+ if (httpClient != null ) {
324+ if (connectTimeout != null ) {
325+ throw new IllegalArgumentException (
326+ "Cannot set both httpClient and connectTimeout. "
327+ + "Configure timeout on the provided HttpClient instead." );
328+ }
329+ if (proxy != null ) {
330+ throw new IllegalArgumentException (
331+ "Cannot set both httpClient and proxy. "
332+ + "Configure proxy on the provided HttpClient instead." );
333+ }
334+ } else {
335+ if (connectTimeout == null ) {
336+ connectTimeout = Duration .ofSeconds (3 );
337+ }
338+ if (proxy == null ) {
339+ proxy = ProxySelector .getDefault ();
340+ }
341+ }
304342 return new WebServiceClient (this );
305343 }
306344 }
0 commit comments