Skip to content

Commit

Permalink
fix: java request timeout config now works with idempotency headers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Mar 20, 2024
1 parent a5a7bdb commit c4b78da
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 36 deletions.
4 changes: 4 additions & 0 deletions generators/java/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.8.6] - 2024-03-20

- Fix: the SDK now generates RequestOptions functions for timeouts with IdempotentRequestOptions correctly, previously timeout functions were only taking in regular RequestOptions. This also addresses a JavaPoet issue where fields were being initialized twice across RequestOptions and IdempotentRequestOptions classes, preventing the SDK from generating at all.

## [0.8.5] - 2024-03-18

- Feat: add in publishing config that allows for signing published artifacts, this is required for publishing to Maven Central.
Expand Down
2 changes: 1 addition & 1 deletion generators/java/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.5
0.8.6
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,31 @@ public GeneratedClientOptions generateFile() {
.addMethod(environmentGetter)
.addMethod(headersFromRequestOptions);

TypeName requestOptionsClassName = clientGeneratorContext.getPoetClassNameFactory().getRequestOptionsClassName();
if (headersFromIdempotentRequestOptions.isPresent()) {
clientOptionsBuilder.addMethod(headersFromIdempotentRequestOptions.get());
MethodSpec httpClientWithTimeoutGetter = MethodSpec.methodBuilder("httpClientWithTimeout")
.addModifiers(Modifier.PUBLIC)
.addParameter(
clientGeneratorContext.getPoetClassNameFactory().getIdempotentRequestOptionsClassName(),
REQUEST_OPTIONS_PARAMETER_NAME)
.returns(OKHTTP_CLIENT_FIELD.type)
.beginControlFlow("if ($L == null)", REQUEST_OPTIONS_PARAMETER_NAME)
.addStatement("return this.$L", OKHTTP_CLIENT_FIELD.name)
.endControlFlow()
.addStatement(
"return this.$L.newBuilder().callTimeout($N.getTimeout().get(), $N.getTimeoutTimeUnit())" +
".connectTimeout(0, $T.SECONDS)" +
".writeTimeout(0, $T.SECONDS)" +
".readTimeout(0, $T.SECONDS).build()",
OKHTTP_CLIENT_FIELD.name,
REQUEST_OPTIONS_PARAMETER_NAME,
REQUEST_OPTIONS_PARAMETER_NAME,
TimeUnit.class,
TimeUnit.class,
TimeUnit.class)
.build();
clientOptionsBuilder.addMethod(httpClientWithTimeoutGetter);
}

MethodSpec httpClientWithTimeoutGetter = MethodSpec.methodBuilder("httpClientWithTimeout")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,6 @@ public final class RequestOptionsGenerator extends AbstractFileGenerator {
Modifier.FINAL)
.build();

private static final FieldSpec.Builder TIMEOUT_FIELD_BUILDER = FieldSpec.builder(
ParameterizedTypeName.get(ClassName.get(Optional.class), TypeName.get(Integer.class)),
"timeout",
Modifier.PRIVATE);

private static final FieldSpec.Builder TIMEOUT_TIME_UNIT_FIELD_BUILDER = FieldSpec.builder(
ParameterizedTypeName.get(TimeUnit.class),
"timeoutTimeUnit",
Modifier.PRIVATE);

private final List<HttpHeader> additionalHeaders;
private final ClassName builderClassName;

Expand Down Expand Up @@ -116,26 +106,36 @@ public GeneratedJavaFile generateFile() {
fields.add(headerHandler.visitHeader(httpHeader));
}

FieldSpec.Builder timeoutFieldBuilder = FieldSpec.builder(
ParameterizedTypeName.get(ClassName.get(Optional.class), TypeName.get(Integer.class)),
"timeout",
Modifier.PRIVATE);

FieldSpec.Builder timeoutTimeUnitFieldBuilder = FieldSpec.builder(
ParameterizedTypeName.get(TimeUnit.class),
"timeoutTimeUnit",
Modifier.PRIVATE);

// Add in the other (static) fields for request options
createRequestOptionField(
"getTimeout",
TIMEOUT_FIELD_BUILDER,
timeoutFieldBuilder,
"null",
requestOptionsTypeSpec,
builderTypeSpec,
fields
);
createRequestOptionField(
"getTimeoutTimeUnit",
TIMEOUT_TIME_UNIT_FIELD_BUILDER,
timeoutTimeUnitFieldBuilder,
"TimeUnit.SECONDS",
requestOptionsTypeSpec,
builderTypeSpec,
fields
);

FieldSpec timeoutField = TIMEOUT_FIELD_BUILDER.build();
FieldSpec timeUnitField = TIMEOUT_TIME_UNIT_FIELD_BUILDER.build();
FieldSpec timeoutField = timeoutFieldBuilder.build();
FieldSpec timeUnitField = timeoutTimeUnitFieldBuilder.build();
builderTypeSpec.addMethod(MethodSpec.methodBuilder(timeoutField.name)
.addModifiers(Modifier.PUBLIC)
.addParameter(Integer.class, timeoutField.name)
Expand Down
23 changes: 23 additions & 0 deletions seed/java-sdk/idempotency-headers/.github/workflows/ci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions seed/java-sdk/idempotency-headers/.mock/definition/api.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions seed/java-sdk/idempotency-headers/.mock/definition/payment.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions seed/java-sdk/idempotency-headers/.mock/fern.config.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions seed/java-sdk/idempotency-headers/build.gradle

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions seed/java-sdk/idempotency-headers/gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c4b78da

Please sign in to comment.