@@ -6,6 +6,8 @@ import algoliasearch.internal.interceptor.{AuthInterceptor, RetryStrategy, UserA
66import algoliasearch .internal .{AlgoliaAgent , HttpRequester , StatefulHost }
77import org .json4s .Formats
88
9+ import java .util .concurrent .TimeUnit
10+ import scala .concurrent .duration .Duration
911import scala .util .Try
1012
1113/** Base class for all API clients. It provides a mechanism for request serialization and deserialization. It also
@@ -19,6 +21,12 @@ import scala.util.Try
1921 * the name of the client
2022 * @param defaultHosts
2123 * the default hosts
24+ * @param defaultReadTimeout
25+ * the default read timeout
26+ * @param defaultConnectTimeout
27+ * the default connect timeout
28+ * @param defaultWriteTimeout
29+ * the default write timeout
2230 * @param formats
2331 * the JSON formats
2432 * @param options
@@ -29,6 +37,9 @@ abstract class ApiClient(
2937 apiKey : String ,
3038 clientName : String ,
3139 defaultHosts : Seq [Host ],
40+ defaultReadTimeout : Duration ,
41+ defaultConnectTimeout : Duration ,
42+ defaultWriteTimeout : Duration ,
3243 formats : Formats ,
3344 options : ClientOptions = ClientOptions ()
3445) extends AutoCloseable {
@@ -45,32 +56,50 @@ abstract class ApiClient(
4556 private val requester = options.customRequester match {
4657 case Some (customRequester) => customRequester
4758 case None =>
48- defaultRequester(appId, apiKey, clientName, options, defaultHosts)
59+ defaultRequester(
60+ appId,
61+ apiKey,
62+ clientName,
63+ options,
64+ defaultHosts,
65+ defaultReadTimeout,
66+ defaultConnectTimeout,
67+ defaultWriteTimeout
68+ )
4969 }
5070
5171 private def defaultRequester (
5272 appId : String ,
5373 apiKey : String ,
5474 clientName : String ,
5575 options : ClientOptions ,
56- defaultHosts : Seq [Host ]
76+ defaultHosts : Seq [Host ],
77+ defaultReadTimeout : Duration ,
78+ defaultConnectTimeout : Duration ,
79+ defaultWriteTimeout : Duration
5780 ): Requester = {
81+ val optionsWithDefaultTimeouts = options.copy(
82+ readTimeout = defaultReadTimeout,
83+ connectTimeout = defaultConnectTimeout,
84+ writeTimeout = defaultWriteTimeout
85+ )
86+
5887 val algoliaAgent = AlgoliaAgent (BuildInfo .version)
5988 .addSegment(AgentSegment (clientName, Some (BuildInfo .version)))
60- .addSegments(options .agentSegments)
89+ .addSegments(optionsWithDefaultTimeouts .agentSegments)
6190
62- val hosts = if (options .hosts.isEmpty) defaultHosts else options .hosts
91+ val hosts = if (optionsWithDefaultTimeouts .hosts.isEmpty) defaultHosts else optionsWithDefaultTimeouts .hosts
6392 val statefulHosts = hosts.map(host => StatefulHost (host)).toList
6493
6594 val builder = HttpRequester
66- .builder(options .customFormats.getOrElse(formats))
95+ .builder(optionsWithDefaultTimeouts .customFormats.getOrElse(formats))
6796 .withInterceptor(authInterceptor)
6897 .withInterceptor(new UserAgentInterceptor (algoliaAgent))
6998 .withInterceptor(new RetryStrategy (statefulHosts))
7099
71- options .requesterConfig.foreach(_(builder))
100+ optionsWithDefaultTimeouts .requesterConfig.foreach(_(builder))
72101
73- builder.build(options )
102+ builder.build(optionsWithDefaultTimeouts )
74103 }
75104
76105 /** Executes the given request and returns the response.
0 commit comments