Skip to content

Commit

Permalink
convert all to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jan 10, 2025
1 parent 4c73157 commit e2db756
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ public void AddCustomQueryParameters(IDictionary<string, object> values)
public object Data { get; set; }

/// <summary>
/// Request read timeout in seconds
/// Request read timeout
/// </summary>
public TimeSpan? ReadTimeout { get; set; }

/// <summary>
/// Request write timeout in seconds
/// Request write timeout
/// </summary>
public TimeSpan? WriteTimeout { get; set; }

/// <summary>
/// Request connect timeout in seconds
/// Request connect timeout
/// </summary>
public TimeSpan? ConnectTimeout { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public class RequestOptions
public IDictionary<string, object> QueryParameters { get; set; }

/// <summary>
/// Request timeout in seconds
/// Request timeout
/// </summary>
public TimeSpan? ReadTimeout { get; set; }

/// <summary>
/// Request timeout in seconds
/// Request timeout
/// </summary>
public TimeSpan? WriteTimeout { get; set; }

/// <summary>
/// Request timeout in seconds
/// Request timeout
/// </summary>
public TimeSpan? ConnectTimeout { get; set; }

Expand All @@ -42,7 +42,4 @@ public RequestOptions()
QueryParameters = new Dictionary<string, object>();
Headers = new Dictionary<string, string>();
}

// overload + operator

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/// Represents options for configuring a request to an endpoint.
final class RequestOptions {
/// The write timeout for the request in milliseconds.
/// The write timeout for the request.
final Duration? writeTimeout;

/// The read timeout for the request in milliseconds.
/// The read timeout for the request.
final Duration? readTimeout;

/// The connect timeout for the request in milliseconds.
/// The connect timeout for the request.
final Duration? connectTimeout;

/// Header names to their respective values to be sent with the request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.Nullable;

/**
* Represents a base client for making API requests. The client uses a {@link Requester} for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.Nullable;
import javax.annotation.Nullable;

/**
* Request options are used to pass extra parameters, headers, timeout to the request. Parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public suspend fun SearchClient.waitForApiKey(
* @param indexName The index in which to perform the request.
* @param taskID The ID of the task to wait for.
* @param timeout If specified, the method will throw a
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value in milliseconds is
* elapsed.
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value is elapsed.
* @param maxRetries maximum number of retry attempts.
* @param requestOptions additional request configuration.
*/
Expand Down Expand Up @@ -136,8 +135,7 @@ public suspend fun SearchClient.waitTask(
*
* @param taskID The ID of the task to wait for.
* @param timeout If specified, the method will throw a
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value in milliseconds is
* elapsed.
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value is elapsed.
* @param maxRetries maximum number of retry attempts.
* @param requestOptions additional request configuration.
*/
Expand Down Expand Up @@ -188,8 +186,7 @@ public suspend fun SearchClient.waitAppTask(
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of
* the response with it.
* @param timeout If specified, the method will throw a
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value in milliseconds is
* elapsed.
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value is elapsed.
* @param maxRetries Maximum number of retry attempts.
* @param requestOptions Additional request configuration.
*/
Expand Down Expand Up @@ -228,8 +225,7 @@ public suspend fun SearchClient.waitKeyUpdate(
* Wait on an API key creation operation.
*
* @param timeout If specified, the method will throw a
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value in milliseconds is
* elapsed.
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value is elapsed.
* @param maxRetries Maximum number of retry attempts.
* @param requestOptions Additional request configuration.
*/
Expand Down Expand Up @@ -263,8 +259,7 @@ public suspend fun SearchClient.waitKeyCreation(
*
* @param maxRetries Maximum number of retry attempts.
* @param timeout If specified, the method will throw a
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value in milliseconds is
* elapsed.
* [kotlinx.coroutines.TimeoutCancellationException] after the timeout value is elapsed.
* @param requestOptions Additional request configuration.
*/
public suspend fun SearchClient.waitKeyDelete(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import kotlin.time.Duration
/**
* Represents options for configuring a request to an endpoint.
*
* @property writeTimeout The write timeout for the request in milliseconds.
* @property readTimeout The read timeout for the request in milliseconds.
* @property connectTimeout The connect timeout for the request in milliseconds.
* @property writeTimeout The write timeout for the request.
* @property readTimeout The read timeout for the request.
* @property connectTimeout The connect timeout for the request.
* @property headers A mutable map of header names to their respective values to be sent with the request.
* @property urlParameters A mutable map of URL parameter names to their respective values to be appended to the request URL.
* @property body A JSON object representing the request body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.apache.commons.lang3.StringUtils.*;

import com.algolia.codegen.lambda.ToSecondsLambda;
import com.algolia.codegen.utils.*;
import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
Expand Down Expand Up @@ -114,7 +113,7 @@ public void processOpts() {

@Override
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
return super.addMustacheLambdas().put("toSeconds", new ToSecondsLambda());
return super.addMustacheLambdas();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.algolia.codegen.exceptions.*;
import com.algolia.codegen.lambda.ScreamingSnakeCaseLambda;
import com.algolia.codegen.lambda.ToSecondsLambda;
import com.algolia.codegen.utils.*;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -70,7 +69,6 @@ protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
ImmutableMap.Builder<String, Mustache.Lambda> lambdas = super.addMustacheLambdas();

lambdas.put("screamingSnakeCase", new ScreamingSnakeCaseLambda());
lambdas.put("toSeconds", new ToSecondsLambda());

return lambdas;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import com.algolia.codegen.utils.*;
import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Mustache.Lambda;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import java.time.temporal.ChronoUnit;
import java.util.*;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.SupportingFile;
Expand Down Expand Up @@ -71,18 +69,14 @@ public void processOpts() {

@Override
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
ImmutableMap.Builder<String, Lambda> lambdas = super.addMustacheLambdas();

lambdas.put("toSeconds", new ToSecondsLambda());

return lambdas;
return super.addMustacheLambdas().put("toSeconds", new ToSecondsLambda());
}

@Override
public void processOpenAPI(OpenAPI openAPI) {
super.processOpenAPI(openAPI);
Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties);
Timeouts.enrichBundle(openAPI, additionalProperties, ChronoUnit.SECONDS);
Timeouts.enrichBundle(openAPI, additionalProperties);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.algolia.codegen;

import com.algolia.codegen.exceptions.GeneratorException;
import com.algolia.codegen.lambda.ToSecondsLambda;
import com.algolia.codegen.utils.*;
import com.google.common.collect.ImmutableMap;
import com.samskivert.mustache.Mustache;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.servers.Server;
import java.io.File;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -95,7 +93,7 @@ public void processOpts() {

@Override
protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
return super.addMustacheLambdas().put("toSeconds", new ToSecondsLambda());
return super.addMustacheLambdas();
}

/** Convert a Seq type to a valid class name. */
Expand All @@ -107,7 +105,7 @@ private String typeToName(String content) {
public void processOpenAPI(OpenAPI openAPI) {
super.processOpenAPI(openAPI);
Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties);
Timeouts.enrichBundle(openAPI, additionalProperties, ChronoUnit.SECONDS);
Timeouts.enrichBundle(openAPI, additionalProperties);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.swagger.v3.oas.models.servers.Server;
import java.io.File;
import java.io.IOException;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -298,7 +297,7 @@ protected ImmutableMap.Builder<String, Mustache.Lambda> addMustacheLambdas() {
public void processOpenAPI(OpenAPI openAPI) {
super.processOpenAPI(openAPI);
Helpers.generateServers(super.fromServers(openAPI.getServers()), additionalProperties);
Timeouts.enrichBundle(openAPI, additionalProperties, ChronoUnit.SECONDS);
Timeouts.enrichBundle(openAPI, additionalProperties);
}

@Override
Expand Down
22 changes: 1 addition & 21 deletions generators/src/main/java/com/algolia/codegen/utils/Timeouts.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@
import com.algolia.codegen.exceptions.*;
import com.fasterxml.jackson.databind.*;
import io.swagger.v3.oas.models.OpenAPI;
import java.time.temporal.ChronoUnit;
import java.util.*;

class TimeoutsValues {

private ChronoUnit unit = ChronoUnit.MILLIS;
public long connect;
public long read;
public long write;

void toUnit(ChronoUnit unit) {
if (this.unit == ChronoUnit.MILLIS && unit == ChronoUnit.SECONDS) {
connect /= 1000;
read /= 1000;
write /= 1000;
}
}
}

class TimeoutsBundle {
Expand All @@ -30,15 +20,8 @@ class TimeoutsBundle {

public class Timeouts {

/** Inject timeouts in miliseconds into the given bundle, under the x-timeouts property * */
public static void enrichBundle(OpenAPI spec, Map<String, Object> bundle) throws ConfigException {
enrichBundle(spec, bundle, ChronoUnit.MILLIS);
}

/**
* Inject timeouts (in miliseconds / divider) into the given bundle, under the x-timeouts property
* *
*/
public static void enrichBundle(OpenAPI spec, Map<String, Object> bundle, ChronoUnit unit) throws ConfigException {
TimeoutsBundle defaults = new TimeoutsBundle();
// the default below are what the search API expect, which was previously used for any client
defaults.browser.connect = 1000;
Expand All @@ -62,9 +45,6 @@ public static void enrichBundle(OpenAPI spec, Map<String, Object> bundle, Chrono
specTimeouts.server = defaults.server;
}

specTimeouts.browser.toUnit(unit);
specTimeouts.server.toUnit(unit);

bundle.put("x-timeouts", specTimeouts);
}
}
6 changes: 3 additions & 3 deletions templates/dart/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ final class {{classname}} implements ApiClient {
{{#returnType}}final response = {{/returnType}}await _retryStrategy.execute(
request: request,
options: {{#vendorExtensions.x-timeouts}}new RequestOptions(
writeTimeout: Duration(seconds: {{#lambda.toSeconds}}{{{write}}}{{/lambda.toSeconds}}),
readTimeout: Duration(seconds: {{#lambda.toSeconds}}{{{read}}}{{/lambda.toSeconds}}),
connectTimeout: Duration(seconds: {{#lambda.toSeconds}}{{{connect}}}{{/lambda.toSeconds}}),
writeTimeout: Duration(milliseconds: {{write}}),
readTimeout: Duration(milliseconds: {{read}}),
connectTimeout: Duration(milliseconds: {{connect}}),
) + {{/vendorExtensions.x-timeouts}}requestOptions,
);
{{#returnType}}
Expand Down
2 changes: 1 addition & 1 deletion templates/java/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {{invokerPackage}}.ApiClient;
import {{invokerPackage}}.config.ClientOptions;

import com.fasterxml.jackson.core.type.TypeReference;
import org.jetbrains.annotations.Nullable;
import javax.annotation.Nullable;

import okhttp3.Call;
import okhttp3.Request;
Expand Down
6 changes: 3 additions & 3 deletions templates/php/client_config.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class {{configClassname}} extends {{#hasRegionalHost}}ConfigWithRegion{{/hasRegi
'apiKey' => '',
'hosts' => null,
'hasFullHosts' => false,
'readTimeout' => {{x-timeouts.server.read}},
'writeTimeout' => {{x-timeouts.server.write}},
'connectTimeout' => {{x-timeouts.server.connect}},
'readTimeout' => {{#lambda.toSeconds}}{{x-timeouts.server.read}}{{/lambda.toSeconds}},
'writeTimeout' => {{#lambda.toSeconds}}{{x-timeouts.server.write}}{{/lambda.toSeconds}},
'connectTimeout' => {{#lambda.toSeconds}}{{x-timeouts.server.connect}}{{/lambda.toSeconds}},
'defaultHeaders' => [],
{{#isSearchClient}}
'waitTaskTimeBeforeRetry' => $this->defaultWaitTaskTimeBeforeRetry,
Expand Down
8 changes: 4 additions & 4 deletions templates/scala/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ object {{classname}} {
)

private def readTimeout(): Duration = {
Duration({{#x-timeouts.server.read}}{{{.}}}{{/x-timeouts.server.read}}, TimeUnit.SECONDS)
Duration({{x-timeouts.server.read}}, TimeUnit.MILLISECONDS)
}

private def connectTimeout(): Duration = {
Duration({{#x-timeouts.server.connect}}{{{.}}}{{/x-timeouts.server.connect}}, TimeUnit.SECONDS)
Duration({{x-timeouts.server.connect}}, TimeUnit.MILLISECONDS)
}

private def writeTimeout(): Duration = {
Duration({{#x-timeouts.server.write}}{{{.}}}{{/x-timeouts.server.write}}, TimeUnit.SECONDS)
Duration({{x-timeouts.server.write}}, TimeUnit.MILLISECONDS)
}

{{#hasRegionalHost}}
Expand Down Expand Up @@ -151,7 +151,7 @@ class {{classname}}(
{{#vendorExtensions.x-use-read-transporter}}.withRead(true){{/vendorExtensions.x-use-read-transporter}}
{{#vendorExtensions}}{{#queryParams}}{{^x-is-custom-request}}.withQueryParameter("{{baseName}}", {{paramName}}){{/x-is-custom-request}}{{#x-is-custom-request}}.withQueryParameters(parameters){{/x-is-custom-request}}{{/queryParams}}{{/vendorExtensions}}
.build()
execute[{{#vendorExtensions}}{{#x-is-custom-request}}T{{/x-is-custom-request}}{{^x-is-custom-request}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/x-is-custom-request}}{{/vendorExtensions}}](request, {{#vendorExtensions.x-timeouts}}Some(RequestOptions(writeTimeout = Some(Duration({{#lambda.toSeconds}}{{{write}}}{{/lambda.toSeconds}}, TimeUnit.SECONDS)), readTimeout = Some(Duration({{#lambda.toSeconds}}{{{read}}}{{/lambda.toSeconds}}, TimeUnit.SECONDS)), connectTimeout = Some(Duration({{#lambda.toSeconds}}{{{connect}}}{{/lambda.toSeconds}}, TimeUnit.SECONDS))) + {{/vendorExtensions.x-timeouts}}requestOptions{{#vendorExtensions.x-timeouts}}){{/vendorExtensions.x-timeouts}})
execute[{{#vendorExtensions}}{{#x-is-custom-request}}T{{/x-is-custom-request}}{{^x-is-custom-request}}{{{returnType}}}{{^returnType}}Unit{{/returnType}}{{/x-is-custom-request}}{{/vendorExtensions}}](request, {{#vendorExtensions.x-timeouts}}Some(RequestOptions(writeTimeout = Some(Duration({{write}}, TimeUnit.MILLISECONDS)), readTimeout = Some(Duration({{read}}, TimeUnit.MILLISECONDS)), connectTimeout = Some(Duration({{connect}}, TimeUnit.MILLISECONDS))) + {{/vendorExtensions.x-timeouts}}requestOptions{{#vendorExtensions.x-timeouts}}){{/vendorExtensions.x-timeouts}})
}

{{/operation}}
Expand Down
4 changes: 2 additions & 2 deletions templates/swift/client_configuration.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public struct {{#lambda.client-to-name}}{{{client}}}{{/lambda.client-to-name}}Cl
public init(appID: String,
apiKey: String,{{#hasRegionalHost}}
region: Region{{#fallbackToAliasHost}}? = nil{{/fallbackToAliasHost}},{{/hasRegionalHost}}
writeTimeout: TimeInterval = {{x-timeouts.server.write}},
readTimeout: TimeInterval = {{x-timeouts.server.read}},
writeTimeout: TimeInterval = {{#lambda.toSeconds}}{{x-timeouts.server.write}}{{/lambda.toSeconds}},
readTimeout: TimeInterval = {{#lambda.toSeconds}}{{x-timeouts.server.read}}{{/lambda.toSeconds}},
logLevel: LogLevel = DefaultConfiguration.default.logLevel,
defaultHeaders: [String: String]? = DefaultConfiguration.default.defaultHeaders,
hosts: [RetryableHost]? = nil{{#isSearchClient}},
Expand Down

0 comments on commit e2db756

Please sign in to comment.