Skip to content

Commit 37c086c

Browse files
committed
add request Timeout
1 parent 9191ce2 commit 37c086c

File tree

6 files changed

+58
-22
lines changed

6 files changed

+58
-22
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.avaje.http.api;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
import java.time.temporal.ChronoUnit;
9+
10+
/**
11+
* Overrides global request timeout for this endpoint.
12+
*
13+
* <pre>{@code
14+
* @Client
15+
* interface CustomerApi {
16+
* @Get("/{id}")
17+
* @RequestTimeout(value = 1, ChronoUnit.SECONDS)
18+
* Customer getById(long id);
19+
* }
20+
*
21+
* }</pre>
22+
*/
23+
@Target(METHOD)
24+
@Retention(RUNTIME)
25+
public @interface RequestTimeout {
26+
long value();
27+
28+
ChronoUnit chronoUnit() default ChronoUnit.MILLIS;
29+
}

http-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
<dependency>
8484
<groupId>io.avaje</groupId>
8585
<artifactId>avaje-http-api</artifactId>
86-
<version>1.20</version>
86+
<version>${project.version}</version>
8787
<scope>test</scope>
8888
</dependency>
8989

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.avaje.http.generator.core.*;
44

55
import javax.lang.model.element.TypeElement;
6+
import java.util.Optional;
67
import java.util.Set;
78
import java.util.stream.Collectors;
89

@@ -24,6 +25,7 @@ class ClientMethodWriter {
2425
private MethodParam bodyHandlerParam;
2526
private String methodGenericParams = "";
2627
private final boolean useJsonb;
28+
private final Optional<RequestTimeoutPrism> timeout;
2729

2830
ClientMethodWriter(MethodReader method, Append writer, ProcessingContext ctx, boolean useJsonb) {
2931
this.method = method;
@@ -32,6 +34,7 @@ class ClientMethodWriter {
3234
this.ctx = ctx;
3335
this.returnType = Util.parseType(method.returnType());
3436
this.useJsonb = useJsonb;
37+
this.timeout = method.timeout();
3538
}
3639

3740
void addImportTypes(ControllerReader reader) {
@@ -83,15 +86,21 @@ void write() {
8386

8487
writeHeaders();
8588
writePaths(segments);
89+
timeout.ifPresent(this::writeTimeout);
8690
writeQueryParams(pathSegments);
8791
writeBeanParams(pathSegments);
8892
writeFormParams(pathSegments);
8993
writeBody();
9094
writeEnd();
9195
}
9296

93-
private void writeEnd() {
94-
WebMethod webMethod = method.webMethod();
97+
private void writeTimeout(RequestTimeoutPrism p) {
98+
99+
writer.append(" .requestTimeout(of(%s, %s))", p.value(), p.chronoUnit()).eol();
100+
}
101+
102+
private void writeEnd() {
103+
final var webMethod = method.webMethod();
95104
writer.append(" .%s()", webMethod.name()).eol();
96105
if (returnType == UType.VOID) {
97106
writer.append(" .asVoid();").eol();

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientProcessor.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
import java.io.IOException;
44
import java.io.Writer;
55
import java.util.LinkedHashSet;
6-
import java.util.List;
6+
import java.util.Objects;
77
import java.util.Set;
88

99
import javax.annotation.processing.AbstractProcessor;
1010
import javax.annotation.processing.ProcessingEnvironment;
1111
import javax.annotation.processing.RoundEnvironment;
1212
import javax.annotation.processing.SupportedAnnotationTypes;
1313
import javax.lang.model.SourceVersion;
14-
import javax.lang.model.element.AnnotationMirror;
15-
import javax.lang.model.element.AnnotationValue;
1614
import javax.lang.model.element.Element;
1715
import javax.lang.model.element.TypeElement;
1816
import javax.tools.FileObject;
@@ -84,22 +82,11 @@ private void writeServicesFile() {
8482
}
8583

8684
private void writeForImported(Element importedElement) {
87-
for (AnnotationMirror annotationMirror : importedElement.getAnnotationMirrors()) {
88-
for (AnnotationValue value : annotationMirror.getElementValues().values()) {
89-
for (Object apiClassDef : (List<?>) value.getValue()) {
90-
writeImported(apiClassDef.toString());
91-
}
92-
}
93-
}
94-
}
9585

96-
private void writeImported(String fullName) {
97-
// trim .class suffix
98-
String apiClassName = fullName.substring(0, fullName.length() - 6);
99-
TypeElement typeElement = ctx.typeElement(apiClassName);
100-
if (typeElement != null) {
101-
writeClient(typeElement);
102-
}
86+
ImportPrism.getInstanceOn(importedElement).types().stream()
87+
.map(ctx::asElement)
88+
.filter(Objects::nonNull)
89+
.forEach(this::writeClient);
10390
}
10491

10592
private void writeClient(Element controller) {

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class MethodReader {
3737
private final PathSegments pathSegments;
3838
private final boolean hasValid;
3939
private final List<ExecutableElement> superMethods;
40+
private final Optional<RequestTimeoutPrism> timeout;
4041

4142
private WebMethod webMethod;
4243
private String webMethodPath;
@@ -59,7 +60,12 @@ public class MethodReader {
5960

6061
this.apiResponses = buildApiResponses();
6162
this.javadoc = buildJavadoc(element, ctx);
62-
63+
this.timeout = RequestTimeoutPrism.getOptionalOn(element);
64+
timeout.ifPresent(
65+
p -> {
66+
bean.addStaticImportType("java.time.temporal.ChronoUnit." + p.chronoUnit());
67+
bean.addStaticImportType("java.time.Duration.of");
68+
});
6369
if (isWebMethod()) {
6470
this.hasValid = initValid();
6571
this.pathSegments = PathSegments.parse(Util.combinePath(bean.path(), webMethodPath));
@@ -325,4 +331,8 @@ public String bodyName() {
325331
}
326332
return "body";
327333
}
334+
335+
public Optional<RequestTimeoutPrism> timeout() {
336+
return timeout;
337+
}
328338
}

http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
@GeneratePrism(value = org.jetbrains.annotations.NotNull.class, publicAccess = true)
2828
@GeneratePrism(value = io.avaje.http.api.Client.class, publicAccess = true)
2929
@GeneratePrism(value = io.avaje.http.api.Client.Import.class, publicAccess = true)
30+
@GeneratePrism(value = io.avaje.http.api.RequestTimeout.class, publicAccess = true)
3031
package io.avaje.http.generator.core;
3132

3233
import io.avaje.prism.GeneratePrism;

0 commit comments

Comments
 (0)