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 675c65e commit c04eb50
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.abtesting.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class AbtestingClient(
override val appId: String,
Expand All @@ -21,7 +22,7 @@ public class AbtestingClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Abtesting", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Abtesting", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
val allowedRegions = listOf("de", "us")
require(region == null || region in allowedRegions) { "`region` must be one of the following: ${allowedRegions.joinToString()}" }
val url = if (region == null) "analytics.algolia.com" else "analytics.$region.algolia.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.analytics.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class AnalyticsClient(
override val appId: String,
Expand All @@ -21,7 +22,7 @@ public class AnalyticsClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Analytics", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Analytics", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
val allowedRegions = listOf("de", "us")
require(region == null || region in allowedRegions) { "`region` must be one of the following: ${allowedRegions.joinToString()}" }
val url = if (region == null) "analytics.algolia.com" else "analytics.$region.algolia.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.ingestion.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class IngestionClient(
override val appId: String,
Expand All @@ -21,7 +22,7 @@ public class IngestionClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Ingestion", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Ingestion", appId = appId, apiKey = apiKey, connectTimeout = 25000.milliseconds, readTimeout = 25000.milliseconds, writeTimeout = 25000.milliseconds, options = options) {
val allowedRegions = listOf("eu", "us")
require(region in allowedRegions) { "`region` is required and must be one of the following: ${allowedRegions.joinToString()}" }
val url = "data.$region.algolia.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.insights.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class InsightsClient(
override val appId: String,
Expand All @@ -21,7 +22,7 @@ public class InsightsClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Insights", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Insights", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
val allowedRegions = listOf("de", "us")
require(region == null || region in allowedRegions) { "`region` must be one of the following: ${allowedRegions.joinToString()}" }
val url = if (region == null) "insights.algolia.io" else "insights.$region.algolia.io"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.monitoring.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class MonitoringClient(
override val appId: String,
Expand All @@ -20,7 +21,7 @@ public class MonitoringClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Monitoring", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Monitoring", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
listOf(
Host("status.algolia.com"),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.personalization.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class PersonalizationClient(
override val appId: String,
Expand All @@ -21,7 +22,7 @@ public class PersonalizationClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Personalization", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Personalization", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
val allowedRegions = listOf("eu", "us")
require(region in allowedRegions) { "`region` is required and must be one of the following: ${allowedRegions.joinToString()}" }
val url = "personalization.$region.algolia.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.querysuggestions.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class QuerySuggestionsClient(
override val appId: String,
Expand All @@ -21,7 +22,7 @@ public class QuerySuggestionsClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "QuerySuggestions", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "QuerySuggestions", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
val allowedRegions = listOf("eu", "us")
require(region in allowedRegions) { "`region` is required and must be one of the following: ${allowedRegions.joinToString()}" }
val url = "query-suggestions.$region.algolia.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.recommend.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class RecommendClient(
override val appId: String,
Expand All @@ -20,7 +21,7 @@ public class RecommendClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Recommend", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Recommend", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
listOf(
Host("$appId-dsn.algolia.net", CallType.Read),
Host("$appId.algolia.net", CallType.Write),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.algolia.client.model.search.*
import com.algolia.client.transport.*
import com.algolia.client.transport.internal.*
import kotlinx.serialization.json.*
import kotlin.time.Duration.Companion.milliseconds

public class SearchClient(
override val appId: String,
Expand All @@ -20,7 +21,7 @@ public class SearchClient(
require(apiKey.isNotBlank()) { "`apiKey` is missing." }
}

override val requester: Requester = requesterOf(clientName = "Search", appId = appId, apiKey = apiKey, options = options) {
override val requester: Requester = requesterOf(clientName = "Search", appId = appId, apiKey = apiKey, connectTimeout = 2000.milliseconds, readTimeout = 5000.milliseconds, writeTimeout = 30000.milliseconds, options = options) {
listOf(
Host("$appId-dsn.algolia.net", CallType.Read),
Host("$appId.algolia.net", CallType.Write),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import io.ktor.client.plugins.logging.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonBuilder
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

public expect class ClientOptions(
connectTimeout: Duration = 2.seconds,
writeTimeout: Duration = 30.seconds,
readTimeout: Duration = 5.seconds,
connectTimeout: Duration? = null,
writeTimeout: Duration? = null,
readTimeout: Duration? = null,
logLevel: LogLevel = LogLevel.NONE,
logger: Logger = Logger.DEFAULT,
hosts: List<Host>? = null,
Expand All @@ -25,13 +24,13 @@ public expect class ClientOptions(
) {

/** Connect timeout for each request */
public val connectTimeout: Duration
public val connectTimeout: Duration?

/** The timeout for each request when performing write operations. */
public val writeTimeout: Duration
public val writeTimeout: Duration?

/** The timeout for each request when performing read operations. */
public val readTimeout: Duration
public val readTimeout: Duration?

/** [LogLevel] to display in the console. */
public val logLevel: LogLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.algolia.client.transport.RequestConfig
import com.algolia.client.transport.RequestOptions
import com.algolia.client.transport.Requester
import io.ktor.util.reflect.*
import kotlin.time.Duration

/**
* Executes a network request with the specified configuration and options, then returns the
Expand Down Expand Up @@ -40,6 +41,9 @@ internal fun requesterOf(
clientName: String,
appId: String,
apiKey: String,
connectTimeout: Duration,
readTimeout: Duration,
writeTimeout: Duration,
options: ClientOptions,
defaultHosts: () -> List<Host>,
) = options.requester ?: KtorRequester(
Expand All @@ -52,8 +56,8 @@ internal fun requesterOf(
add(AgentSegment(clientName, BuildConfig.VERSION))
},
),
connectTimeout = options.connectTimeout,
readTimeout = options.readTimeout,
writeTimeout = options.writeTimeout,
connectTimeout = options.connectTimeout ?: connectTimeout,
readTimeout = options.readTimeout ?: readTimeout,
writeTimeout = options.writeTimeout ?: writeTimeout,
hosts = options.hosts ?: defaultHosts(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import io.ktor.client.plugins.logging.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonBuilder
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

public actual class ClientOptions(
public actual val connectTimeout: Duration = 2.seconds,
public actual val writeTimeout: Duration = 30.seconds,
public actual val readTimeout: Duration = 5.seconds,
public actual val connectTimeout: Duration? = null,
public actual val writeTimeout: Duration? = null,
public actual val readTimeout: Duration? = null,
public actual val logLevel: LogLevel = LogLevel.NONE,
public actual val logger: Logger = Logger.DEFAULT,
public actual val hosts: List<Host>? = null,
Expand All @@ -29,9 +28,9 @@ public actual class ClientOptions(
public actual val json: Json = buildJson(jsonConfig)

public actual constructor(
connectTimeout: Duration,
writeTimeout: Duration,
readTimeout: Duration,
connectTimeout: Duration?,
writeTimeout: Duration?,
readTimeout: Duration?,
logLevel: LogLevel,
logger: Logger,
hosts: List<Host>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import kotlinx.serialization.json.JsonBuilder
import kotlin.time.Duration

public actual class ClientOptions actual constructor(
public actual val connectTimeout: Duration,
public actual val writeTimeout: Duration,
public actual val readTimeout: Duration,
public actual val connectTimeout: Duration?,
public actual val writeTimeout: Duration?,
public actual val readTimeout: Duration?,
public actual val logLevel: LogLevel,
public actual val logger: Logger,
public actual val hosts: List<Host>?,
Expand Down

0 comments on commit c04eb50

Please sign in to comment.