Skip to content

Commit

Permalink
Merge branch 'main' into feat/scala-e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Feb 14, 2024
2 parents c034dc9 + ca31513 commit c74ae79
Show file tree
Hide file tree
Showing 155 changed files with 5,400 additions and 1,080 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,50 @@ final class Host {
/// Host url.
final String url;

// url port
final int? port;

/// Host protocol (i.e. `http`, `https`)
final String scheme;

/// Whether this host should be used for [CallType.read] or [CallType.write] requests.
final CallType? callType;

/// Constructs a [Host] instance with the provided parameters.
const Host({required this.url, this.scheme = 'https', this.callType});
const Host(
{required this.url, this.port, this.scheme = 'https', this.callType});

factory Host.create(
{required String url, String scheme = 'https', CallType? callType}) {
if (url.contains(':')) {
final parts = url.split(':');
return Host(
url: parts[0],
port: int.parse(parts[1]),
scheme: scheme,
callType: callType);
}

return Host(url: url, scheme: scheme, callType: callType);
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Host &&
runtimeType == other.runtimeType &&
url == other.url &&
port == other.port &&
scheme == other.scheme &&
callType == other.callType;

@override
int get hashCode => url.hashCode ^ scheme.hashCode ^ callType.hashCode;
int get hashCode =>
url.hashCode ^ (port ?? 1) ^ scheme.hashCode ^ callType.hashCode;

@override
String toString() {
return 'Host{url: $url, scheme: $scheme, callType: $callType}';
return 'Host{url: $url, port: $port, scheme: $scheme, callType: $callType}';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class DioRequester implements Requester {
Uri uri = Uri(
scheme: request.host.scheme,
host: request.host.url,
port: request.host.port,
path: request.path,
);
if (request.queryParameters.isNotEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public final class Host {

private final String url;
private final int port;

private final Set<CallType> callTypes;

Expand All @@ -16,7 +17,12 @@ public Host(@Nonnull String url, @Nonnull Set<CallType> callType) {
}

public Host(String url, Set<CallType> callType, String scheme) {
this(url, callType, scheme, -1);
}

public Host(String url, Set<CallType> callType, String scheme, int port) {
this.url = url;
this.port = port;
this.callTypes = callType;
this.scheme = scheme;
}
Expand All @@ -26,6 +32,10 @@ public String getUrl() {
return url;
}

public int getPort() {
return port;
}

@Nonnull
public Set<CallType> getCallTypes() {
return callTypes;
Expand All @@ -44,13 +54,15 @@ public boolean equals(Object o) {
Host host = (Host) o;

if (!url.equals(host.url)) return false;
if (port != host.port) return false;
if (!callTypes.equals(host.callTypes)) return false;
return scheme.equals(host.scheme);
}

@Override
public int hashCode() {
int result = url.hashCode();
result = 31 * result + port;
result = 31 * result + callTypes.hashCode();
result = 31 * result + scheme.hashCode();
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public String getHost() {
return host.getUrl();
}

public int getPort() {
return host.getPort();
}

public String getScheme() {
return this.host.getScheme();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public Response intercept(@Nonnull Chain chain) {
/** Processes the request for a given host. */
@Nonnull
private Response processRequest(@Nonnull Chain chain, @Nonnull Request request, StatefulHost host) throws IOException {
HttpUrl newUrl = request.url().newBuilder().scheme(host.getScheme()).host(host.getHost()).build();
HttpUrl.Builder urlBuilder = request.url().newBuilder().scheme(host.getScheme()).host(host.getHost());
if (host.getPort() != -1) {
urlBuilder.port(host.getPort());
}
HttpUrl newUrl = urlBuilder.build();
Request newRequest = request.newBuilder().url(newUrl).build();
chain.withConnectTimeout(chain.connectTimeoutMillis() * (host.getRetryCount() + 1), TimeUnit.MILLISECONDS);
Response response = chain.proceed(newRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function serializeUrl(
queryParameters: QueryParameters
): string {
const queryParametersAsString = serializeQueryParameters(queryParameters);
let url = `${host.protocol}://${host.url}/${
let url = `${host.protocol}://${host.url}${host.port ? `:${host.port}` : ''}/${
path.charAt(0) === '/' ? path.substr(1) : path
}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export type Host = {
* The protocol of the host URL.
*/
protocol: 'http' | 'https';

/**
* The port of the host URL.
*/
port?: number;
};

export type StatefulHost = Host & {
Expand Down
23 changes: 23 additions & 0 deletions clients/algoliasearch-client-kotlin/client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,28 @@ kotlin {
implementation(libs.kotlin.datetime)
}
}

val commonTest by getting {
dependencies {
implementation(libs.kotlin.test.common)
implementation(libs.kotlin.test.annotations.common)
implementation(libs.ktor.client.mock)
}
}

val jvmTest by getting {
dependencies {
implementation(libs.kotlin.test.junit)
implementation(libs.ktor.client.okhttp)
}
}

if (HostManager.hostIsMac) {
val appleTest by getting {
dependencies {
implementation(libs.ktor.client.darwin)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.algolia.client.transport.Requester
import io.ktor.client.*
import io.ktor.client.engine.*
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
Expand All @@ -20,7 +21,7 @@ public expect class ClientOptions(
httpClientConfig: ((HttpClientConfig<*>) -> Unit)? = null,
jsonConfig: ((JsonBuilder) -> Unit)? = null,
requester: Requester? = null,
algoliaAgentSegments: List<AgentSegment> = emptyList()
algoliaAgentSegments: List<AgentSegment> = emptyList(),
) {

/** Connect timeout for each request */
Expand Down Expand Up @@ -50,8 +51,8 @@ public expect class ClientOptions(
/** An optional [HttpClientConfig] used by Ktor for advanced HttpClient configuration. */
public val httpClientConfig: ((HttpClientConfig<*>) -> Unit)?

/** An optional [JsonBuilder] configuration. */
public val jsonConfig: ((JsonBuilder) -> Unit)?
/** Json serialization instance */
public val json: Json

/** Custom Http Requester. */
public val requester: Requester?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ package com.algolia.client.configuration
public data class Host(
public val url: String,
public val callType: CallType? = null,
public val protocol: String = "https",
public val port: Int? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json

private const val HEADER_APPLICATION_ID = "x-algolia-application-id"
private const val HEADER_APIKEY = "x-algolia-api-key"
Expand All @@ -33,15 +32,7 @@ internal fun HttpClientConfig<*>.configure(

// Content negotiation and serialization
install(ContentNegotiation) {
json(
Json {
options.jsonConfig?.invoke(this)
isLenient = true
ignoreUnknownKeys = true
allowSpecialFloatingPointValues = true
coerceInputValues = true
},
)
json(options.json)
}

// Logging
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.algolia.client.configuration.internal

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonBuilder

/**
* Creates default [Json] instance.
*
* @param jsonConfig custom json configuration
*/
internal fun buildJson(
jsonConfig: ((JsonBuilder) -> Unit)? = null,
) = Json {
jsonConfig?.invoke(this)
isLenient = true
ignoreUnknownKeys = true
allowSpecialFloatingPointValues = true
coerceInputValues = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.algolia.client.extensions

/**
* Enum for the operations that can be performed.
*/
public enum class ApiKeyOperation {
Create,
Update,
Delete
}
Loading

0 comments on commit c74ae79

Please sign in to comment.