Skip to content

Commit

Permalink
wip: change non-generated files dir
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Mar 14, 2023
1 parent 7bd2d16 commit d5d1dc6
Show file tree
Hide file tree
Showing 24 changed files with 164 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.configuration
package com.algolia.client.configuration

/**
* Indicate whether the HTTP call performed is of type [Read] (GET) or [Write] (POST, PUT ..).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.algolia.search.configuration
package com.algolia.client.configuration

import com.algolia.search.logging.LogLevel
import com.algolia.search.logging.Logger
import com.algolia.search.transport.RequestOptions
import com.algolia.client.logging.LogLevel
import com.algolia.client.logging.Logger
import io.ktor.client.HttpClient
import io.ktor.client.HttpClientConfig
import io.ktor.client.engine.HttpClientEngine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.algolia.search.configuration
package com.algolia.client.configuration

import com.algolia.search.model.APIKey
import com.algolia.search.model.ApplicationID
import com.algolia.client.model.APIKey
import com.algolia.client.model.ApplicationID

/**
* Configuration used by a client for authenticated request.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.configuration
package com.algolia.client.configuration

/**
* @param url The url to target.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.configuration
package com.algolia.client.configuration

/**
* Get algolia client user agent.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.exception
package com.algolia.client.exception

/**
* Algolia runtime exception.
Expand All @@ -7,8 +7,8 @@ package com.algolia.search.exception
* @param cause the cause of the exception
*/
public sealed class AlgoliaRuntimeException(
message: String? = null,
cause: Throwable? = null
message: String? = null,
cause: Throwable? = null
) : RuntimeException(message, cause)

/**
Expand All @@ -18,8 +18,8 @@ public sealed class AlgoliaRuntimeException(
* @param cause the cause of the exception
*/
public class AlgoliaClientException(
message: String? = null,
cause: Throwable? = null
message: String? = null,
cause: Throwable? = null
) : AlgoliaRuntimeException(message, cause)

/**
Expand All @@ -30,9 +30,9 @@ public class AlgoliaClientException(
* @param httpErrorCode
*/
public class AlgoliaApiException(
message: String? = null,
cause: Throwable? = null,
public val httpErrorCode: Int? = null
message: String? = null,
cause: Throwable? = null,
public val httpErrorCode: Int? = null
) : AlgoliaRuntimeException(message, cause)

/**
Expand All @@ -42,5 +42,5 @@ public class AlgoliaApiException(
* @param exceptions list of thrown exceptions
*/
public class UnreachableHostsException(
public val exceptions: List<Throwable>,
public val exceptions: List<Throwable>,
) : AlgoliaRuntimeException("Error(s) while processing the retry strategy", exceptions.last())
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.exception
package com.algolia.client.exception

/**
* This exception is thrown when an illegal empty [List] is encountered.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.exception
package com.algolia.client.exception

/**
* This exception is thrown when an illegal empty [String] is encountered.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.algolia.client.exception.internal

import com.algolia.client.exception.AlgoliaApiException
import com.algolia.client.exception.AlgoliaClientException
import com.algolia.client.exception.AlgoliaRuntimeException
import io.ktor.client.plugins.ResponseException

/**
* Coerce a Throwable to a [AlgoliaClientException].
*/
internal fun Throwable.asClientException(): com.algolia.client.exception.AlgoliaClientException {
return com.algolia.client.exception.AlgoliaClientException(message = message, cause = this)
}

/**
* Coerce a [ResponseException] to a [AlgoliaRuntimeException].
*/
internal fun ResponseException.asApiException(): com.algolia.client.exception.AlgoliaApiException {
return com.algolia.client.exception.AlgoliaApiException(
message = message,
cause = this,
httpErrorCode = response.status.value
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.logging
package com.algolia.client.logging

/**
* Http client logging log level.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.algolia.search.logging
package com.algolia.client.logging

import com.algolia.search.logging.internal.MessageLengthLimitingLogger
import com.algolia.client.logging.internal.MessageLengthLimitingLogger

/**
* Client Logger.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.algolia.search.logging.internal
package com.algolia.client.logging.internal

import com.algolia.search.logging.Logger
import com.algolia.client.logging.Logger

/**
* A [Logger] that breaks up log messages into multiple logs no longer than [maxLength].
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.algolia.search.model
package com.algolia.client.model

import com.algolia.search.exception.EmptyStringException
import com.algolia.client.exception.EmptyStringException
import kotlinx.serialization.Serializable
import kotlin.jvm.JvmInline

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.algolia.search.model
package com.algolia.client.model

import com.algolia.search.exception.EmptyStringException
import com.algolia.client.exception.EmptyStringException
import kotlin.jvm.JvmInline

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.algolia.client.transport

import com.algolia.client.configuration.CallType
import io.ktor.http.*
import io.ktor.util.reflect.*

public interface CustomRequester {

/**
* Perform a custom request.
*
* @param method represents an HTTP method
* @param callType indicate whether the HTTP call performed is of type Read (GET) or Write (POST, PUT..)
* @param path request path
* @param requestOptions configure request locally
* @param body request body
* @param responseType return value type
*
* @return request's response; type [T] must be serializable
*/
public suspend fun <T> customRequest(
method: HttpMethod,
callType: CallType,
path: String,
responseType: TypeInfo,
body: String? = null,
requestOptions: RequestOptions? = null,
): T
}

/**
* Perform a custom request.
*
* @param method represents an HTTP method
* @param callType indicate whether the HTTP call performed is of type Read (GET) or Write (POST, PUT..)
* @param path request path
* @param requestOptions configure request locally
* @param body request body
*
* @return request's response; type [T] must be serializable
*/
public suspend inline fun <reified T> CustomRequester.customRequest(
method: HttpMethod,
callType: CallType,
path: String,
body: String? = null,
requestOptions: RequestOptions? = null,
): T = customRequest(
method = method,
callType = callType,
path = path,
requestOptions = requestOptions,
body = body,
responseType = typeInfo<T>()
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.transport
package com.algolia.client.transport

import kotlinx.serialization.json.JsonObject

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.algolia.client.transport.internal

import com.algolia.client.configuration.CallType
import com.algolia.client.configuration.Host
import com.algolia.client.model.ApplicationID

internal val ApplicationID.searchHosts
get() = listOf(
Host("$this-dsn.algolia.net", CallType.Read),
Host("$this.algolia.net", CallType.Write),
) + mutableListOf(
Host("$this-1.algolianet.com"),
Host("$this-2.algolianet.com"),
Host("$this-3.algolianet.com")
).apply { shuffle() }
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.algolia.search.transport.internal
package com.algolia.client.transport.internal

import com.algolia.search.model.APIKey
import com.algolia.search.model.ApplicationID
import com.algolia.search.transport.RequestOptions
import com.algolia.client.model.APIKey
import com.algolia.client.model.ApplicationID
import com.algolia.client.transport.RequestOptions
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.header
import io.ktor.client.request.parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.algolia.search.transport.internal
package com.algolia.client.transport.internal

import com.algolia.search.configuration.Host
import com.algolia.client.configuration.Host

internal data class RetryableHost(
private val host: Host,
private val host: Host,
) {

val url get() = host.url
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.algolia.search.transport.internal
package com.algolia.client.transport.internal

import kotlinx.datetime.Clock

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.algolia.search.transport.internal
package com.algolia.client.transport.internal

import com.algolia.search.configuration.CallType
import com.algolia.search.configuration.Configuration
import com.algolia.search.configuration.Credentials
import com.algolia.search.exception.UnreachableHostsException
import com.algolia.search.exception.internal.asApiException
import com.algolia.search.exception.internal.asClientException
import com.algolia.search.transport.CustomRequester
import com.algolia.search.transport.RequestOptions
import com.algolia.client.configuration.CallType
import com.algolia.client.configuration.Configuration
import com.algolia.client.configuration.Credentials
import com.algolia.client.exception.UnreachableHostsException
import com.algolia.client.exception.internal.asApiException
import com.algolia.client.exception.internal.asClientException
import com.algolia.client.transport.CustomRequester
import com.algolia.client.transport.RequestOptions
import io.ktor.client.call.HttpClientCall
import io.ktor.client.call.body
import io.ktor.client.network.sockets.ConnectTimeoutException
Expand All @@ -30,8 +30,8 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

internal class Transport(
private val configuration: Configuration,
private val credentialsOrNull: Credentials?,
private val configuration: Configuration,
private val credentialsOrNull: Credentials?,
) : CustomRequester {

private val hostStatusExpirationDelayMS: Long = 1000L * 60L * 5L
Expand All @@ -50,11 +50,11 @@ internal class Transport(
* @param body request body
*/
suspend inline fun <reified T> request(
httpMethod: HttpMethod,
callType: CallType,
path: String,
requestOptions: RequestOptions?,
body: String? = null,
httpMethod: HttpMethod,
callType: CallType,
path: String,
requestOptions: RequestOptions?,
body: String? = null,
): T {
return execute(httpMethod, callType, path, requestOptions, body) {
val response = configuration.httpClient.request(it)
Expand All @@ -66,12 +66,12 @@ internal class Transport(
* Execute HTTP request (with retry strategy) and get a result as [T].
*/
private suspend inline fun <T> execute(
httpMethod: HttpMethod,
callType: CallType,
path: String,
requestOptions: RequestOptions?,
body: String? = null,
block: (HttpRequestBuilder) -> T
httpMethod: HttpMethod,
callType: CallType,
path: String,
requestOptions: RequestOptions?,
body: String? = null,
block: (HttpRequestBuilder) -> T
): T {
val hosts = callableHosts(callType)
val errors by lazy(LazyThreadSafetyMode.NONE) { mutableListOf<Throwable>() }
Expand Down Expand Up @@ -148,10 +148,10 @@ internal class Transport(
* Set socket read/write timeout.
*/
private fun setTimeout(
requestBuilder: HttpRequestBuilder,
requestOptions: RequestOptions?,
callType: CallType,
host: RetryableHost,
requestBuilder: HttpRequestBuilder,
requestOptions: RequestOptions?,
callType: CallType,
host: RetryableHost,
) {
requestBuilder.timeout {
val timeout = when (callType) {
Expand All @@ -163,12 +163,12 @@ internal class Transport(
}

override suspend fun <T> customRequest(
method: HttpMethod,
callType: CallType,
path: String,
responseType: TypeInfo,
body: String?,
requestOptions: RequestOptions?
method: HttpMethod,
callType: CallType,
path: String,
responseType: TypeInfo,
body: String?,
requestOptions: RequestOptions?
): T {
val httpResponse = request<HttpResponse>(method, callType, path, requestOptions, body)
return httpResponse.call.bodyAs(responseType)
Expand Down
Loading

0 comments on commit d5d1dc6

Please sign in to comment.