Skip to content

Commit

Permalink
feat(generators): allow per-spec timeouts [skip-bc] (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#4173

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
  • Loading branch information
4 people committed Dec 5, 2024
1 parent 15cda49 commit 0ea2acd
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 16 deletions.
43 changes: 36 additions & 7 deletions src/main/scala/algoliasearch/ApiClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import algoliasearch.internal.interceptor.{AuthInterceptor, RetryStrategy, UserA
import algoliasearch.internal.{AlgoliaAgent, HttpRequester, StatefulHost}
import org.json4s.Formats

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.util.Try

/** Base class for all API clients. It provides a mechanism for request serialization and deserialization. It also
Expand All @@ -19,6 +21,12 @@ import scala.util.Try
* the name of the client
* @param defaultHosts
* the default hosts
* @param defaultReadTimeout
* the default read timeout
* @param defaultConnectTimeout
* the default connect timeout
* @param defaultWriteTimeout
* the default write timeout
* @param formats
* the JSON formats
* @param options
Expand All @@ -29,6 +37,9 @@ abstract class ApiClient(
apiKey: String,
clientName: String,
defaultHosts: Seq[Host],
defaultReadTimeout: Duration,
defaultConnectTimeout: Duration,
defaultWriteTimeout: Duration,
formats: Formats,
options: ClientOptions = ClientOptions()
) extends AutoCloseable {
Expand All @@ -45,32 +56,50 @@ abstract class ApiClient(
private val requester = options.customRequester match {
case Some(customRequester) => customRequester
case None =>
defaultRequester(appId, apiKey, clientName, options, defaultHosts)
defaultRequester(
appId,
apiKey,
clientName,
options,
defaultHosts,
defaultReadTimeout,
defaultConnectTimeout,
defaultWriteTimeout
)
}

private def defaultRequester(
appId: String,
apiKey: String,
clientName: String,
options: ClientOptions,
defaultHosts: Seq[Host]
defaultHosts: Seq[Host],
defaultReadTimeout: Duration,
defaultConnectTimeout: Duration,
defaultWriteTimeout: Duration
): Requester = {
val optionsWithDefaultTimeouts = options.copy(
readTimeout = defaultReadTimeout,
connectTimeout = defaultConnectTimeout,
writeTimeout = defaultWriteTimeout
)

val algoliaAgent = AlgoliaAgent(BuildInfo.version)
.addSegment(AgentSegment(clientName, Some(BuildInfo.version)))
.addSegments(options.agentSegments)
.addSegments(optionsWithDefaultTimeouts.agentSegments)

val hosts = if (options.hosts.isEmpty) defaultHosts else options.hosts
val hosts = if (optionsWithDefaultTimeouts.hosts.isEmpty) defaultHosts else optionsWithDefaultTimeouts.hosts
val statefulHosts = hosts.map(host => StatefulHost(host)).toList

val builder = HttpRequester
.builder(options.customFormats.getOrElse(formats))
.builder(optionsWithDefaultTimeouts.customFormats.getOrElse(formats))
.withInterceptor(authInterceptor)
.withInterceptor(new UserAgentInterceptor(algoliaAgent))
.withInterceptor(new RetryStrategy(statefulHosts))

options.requesterConfig.foreach(_(builder))
optionsWithDefaultTimeouts.requesterConfig.foreach(_(builder))

builder.build(options)
builder.build(optionsWithDefaultTimeouts)
}

/** Executes the given request and returns the response.
Expand Down
22 changes: 21 additions & 1 deletion src/main/scala/algoliasearch/api/AbtestingClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ import algoliasearch.abtesting.ScheduleABTestsRequest
import algoliasearch.abtesting._
import algoliasearch.ApiClient
import algoliasearch.api.AbtestingClient.hosts
import algoliasearch.api.AbtestingClient.readTimeout
import algoliasearch.api.AbtestingClient.writeTimeout
import algoliasearch.api.AbtestingClient.connectTimeout
import algoliasearch.config._
import algoliasearch.internal.util._

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

object AbtestingClient {

/** Creates a new SearchApi instance using default hosts.
/** Creates a new AbtestingClient instance using default hosts.
*
* @param appId
* application ID
Expand All @@ -46,6 +51,18 @@ object AbtestingClient {
clientOptions = clientOptions
)

private def readTimeout(): Duration = {
Duration(5, TimeUnit.SECONDS)
}

private def connectTimeout(): Duration = {
Duration(2, TimeUnit.SECONDS)
}

private def writeTimeout(): Duration = {
Duration(30, TimeUnit.SECONDS)
}

private def hosts(region: Option[String] = None): Seq[Host] = {
val allowedRegions = Seq("de", "us")
if (region.isDefined && !allowedRegions.contains(region.get)) {
Expand All @@ -67,6 +84,9 @@ class AbtestingClient(
apiKey = apiKey,
clientName = "Abtesting",
defaultHosts = hosts(region),
defaultReadTimeout = readTimeout(),
defaultWriteTimeout = writeTimeout(),
defaultConnectTimeout = connectTimeout(),
formats = JsonSupport.format,
options = clientOptions
) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/scala/algoliasearch/api/AnalyticsClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ import algoliasearch.analytics.OrderBy._
import algoliasearch.analytics._
import algoliasearch.ApiClient
import algoliasearch.api.AnalyticsClient.hosts
import algoliasearch.api.AnalyticsClient.readTimeout
import algoliasearch.api.AnalyticsClient.writeTimeout
import algoliasearch.api.AnalyticsClient.connectTimeout
import algoliasearch.config._
import algoliasearch.internal.util._

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

object AnalyticsClient {

/** Creates a new SearchApi instance using default hosts.
/** Creates a new AnalyticsClient instance using default hosts.
*
* @param appId
* application ID
Expand All @@ -60,6 +65,18 @@ object AnalyticsClient {
clientOptions = clientOptions
)

private def readTimeout(): Duration = {
Duration(5, TimeUnit.SECONDS)
}

private def connectTimeout(): Duration = {
Duration(2, TimeUnit.SECONDS)
}

private def writeTimeout(): Duration = {
Duration(30, TimeUnit.SECONDS)
}

private def hosts(region: Option[String] = None): Seq[Host] = {
val allowedRegions = Seq("de", "us")
if (region.isDefined && !allowedRegions.contains(region.get)) {
Expand All @@ -81,6 +98,9 @@ class AnalyticsClient(
apiKey = apiKey,
clientName = "Analytics",
defaultHosts = hosts(region),
defaultReadTimeout = readTimeout(),
defaultWriteTimeout = writeTimeout(),
defaultConnectTimeout = connectTimeout(),
formats = JsonSupport.format,
options = clientOptions
) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/scala/algoliasearch/api/IngestionClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ import algoliasearch.ingestion.TriggerType._
import algoliasearch.ingestion._
import algoliasearch.ApiClient
import algoliasearch.api.IngestionClient.hosts
import algoliasearch.api.IngestionClient.readTimeout
import algoliasearch.api.IngestionClient.writeTimeout
import algoliasearch.api.IngestionClient.connectTimeout
import algoliasearch.config._
import algoliasearch.internal.util._

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

object IngestionClient {

/** Creates a new SearchApi instance using default hosts.
/** Creates a new IngestionClient instance using default hosts.
*
* @param appId
* application ID
Expand All @@ -106,6 +111,18 @@ object IngestionClient {
clientOptions = clientOptions
)

private def readTimeout(): Duration = {
Duration(25, TimeUnit.SECONDS)
}

private def connectTimeout(): Duration = {
Duration(25, TimeUnit.SECONDS)
}

private def writeTimeout(): Duration = {
Duration(25, TimeUnit.SECONDS)
}

private def hosts(region: String): Seq[Host] = {
val allowedRegions = Seq("eu", "us")
if (!allowedRegions.contains(region)) {
Expand All @@ -128,6 +145,9 @@ class IngestionClient(
apiKey = apiKey,
clientName = "Ingestion",
defaultHosts = hosts(region),
defaultReadTimeout = readTimeout(),
defaultWriteTimeout = writeTimeout(),
defaultConnectTimeout = connectTimeout(),
formats = JsonSupport.format,
options = clientOptions
) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/scala/algoliasearch/api/InsightsClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ import algoliasearch.insights.InsightsEvents
import algoliasearch.insights._
import algoliasearch.ApiClient
import algoliasearch.api.InsightsClient.hosts
import algoliasearch.api.InsightsClient.readTimeout
import algoliasearch.api.InsightsClient.writeTimeout
import algoliasearch.api.InsightsClient.connectTimeout
import algoliasearch.config._
import algoliasearch.internal.util._

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

object InsightsClient {

/** Creates a new SearchApi instance using default hosts.
/** Creates a new InsightsClient instance using default hosts.
*
* @param appId
* application ID
Expand All @@ -40,6 +45,18 @@ object InsightsClient {
clientOptions = clientOptions
)

private def readTimeout(): Duration = {
Duration(5, TimeUnit.SECONDS)
}

private def connectTimeout(): Duration = {
Duration(2, TimeUnit.SECONDS)
}

private def writeTimeout(): Duration = {
Duration(30, TimeUnit.SECONDS)
}

private def hosts(region: Option[String] = None): Seq[Host] = {
val allowedRegions = Seq("de", "us")
if (region.isDefined && !allowedRegions.contains(region.get)) {
Expand All @@ -61,6 +78,9 @@ class InsightsClient(
apiKey = apiKey,
clientName = "Insights",
defaultHosts = hosts(region),
defaultReadTimeout = readTimeout(),
defaultWriteTimeout = writeTimeout(),
defaultConnectTimeout = connectTimeout(),
formats = JsonSupport.format,
options = clientOptions
) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/scala/algoliasearch/api/MonitoringClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ import algoliasearch.monitoring.Unauthorized
import algoliasearch.monitoring._
import algoliasearch.ApiClient
import algoliasearch.api.MonitoringClient.hosts
import algoliasearch.api.MonitoringClient.readTimeout
import algoliasearch.api.MonitoringClient.writeTimeout
import algoliasearch.api.MonitoringClient.connectTimeout
import algoliasearch.config._
import algoliasearch.internal.util._

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

object MonitoringClient {

/** Creates a new SearchApi instance using default hosts.
/** Creates a new MonitoringClient instance using default hosts.
*
* @param appId
* application ID
Expand All @@ -46,6 +51,18 @@ object MonitoringClient {
clientOptions = clientOptions
)

private def readTimeout(): Duration = {
Duration(5, TimeUnit.SECONDS)
}

private def connectTimeout(): Duration = {
Duration(2, TimeUnit.SECONDS)
}

private def writeTimeout(): Duration = {
Duration(30, TimeUnit.SECONDS)
}

private def hosts(): Seq[Host] = {
List(
Host("status.algolia.com", Set(CallType.Read, CallType.Write))
Expand All @@ -62,6 +79,9 @@ class MonitoringClient(
apiKey = apiKey,
clientName = "Monitoring",
defaultHosts = hosts(),
defaultReadTimeout = readTimeout(),
defaultWriteTimeout = writeTimeout(),
defaultConnectTimeout = connectTimeout(),
formats = JsonSupport.format,
options = clientOptions
) {
Expand Down
22 changes: 21 additions & 1 deletion src/main/scala/algoliasearch/api/PersonalizationClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import algoliasearch.personalization.SetPersonalizationStrategyResponse
import algoliasearch.personalization._
import algoliasearch.ApiClient
import algoliasearch.api.PersonalizationClient.hosts
import algoliasearch.api.PersonalizationClient.readTimeout
import algoliasearch.api.PersonalizationClient.writeTimeout
import algoliasearch.api.PersonalizationClient.connectTimeout
import algoliasearch.config._
import algoliasearch.internal.util._

import java.util.concurrent.TimeUnit
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
import scala.util.Random

object PersonalizationClient {

/** Creates a new SearchApi instance using default hosts.
/** Creates a new PersonalizationClient instance using default hosts.
*
* @param appId
* application ID
Expand All @@ -42,6 +47,18 @@ object PersonalizationClient {
clientOptions = clientOptions
)

private def readTimeout(): Duration = {
Duration(5, TimeUnit.SECONDS)
}

private def connectTimeout(): Duration = {
Duration(2, TimeUnit.SECONDS)
}

private def writeTimeout(): Duration = {
Duration(30, TimeUnit.SECONDS)
}

private def hosts(region: String): Seq[Host] = {
val allowedRegions = Seq("eu", "us")
if (!allowedRegions.contains(region)) {
Expand All @@ -64,6 +81,9 @@ class PersonalizationClient(
apiKey = apiKey,
clientName = "Personalization",
defaultHosts = hosts(region),
defaultReadTimeout = readTimeout(),
defaultWriteTimeout = writeTimeout(),
defaultConnectTimeout = connectTimeout(),
formats = JsonSupport.format,
options = clientOptions
) {
Expand Down
Loading

0 comments on commit 0ea2acd

Please sign in to comment.