-
Notifications
You must be signed in to change notification settings - Fork 796
/
SameTokenSyncResponseClassSpec.java
107 lines (96 loc) · 4.67 KB
/
SameTokenSyncResponseClassSpec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.codegen.poet.paginators.customizations;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import java.util.stream.Stream;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.service.PaginatorDefinition;
import software.amazon.awssdk.codegen.poet.paginators.SyncResponseClassSpec;
import software.amazon.awssdk.core.util.PaginatorUtils;
/**
* Customized response class spec for sync paginated operations that indicate the
* last page by returning the same token passed in the request object.
*
* See Cloudwatch logs GetLogEvents API for example,
* https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_GetLogEvents.html
*/
public class SameTokenSyncResponseClassSpec extends SyncResponseClassSpec {
private static final String LAST_TOKEN_MEMBER = "lastToken";
public SameTokenSyncResponseClassSpec(IntermediateModel model, String c2jOperationName,
PaginatorDefinition paginatorDefinition) {
super(model, c2jOperationName, paginatorDefinition);
}
@Override
protected Stream<FieldSpec> fields() {
return Stream.of(syncClientInterfaceField(), requestClassField());
}
@Override
protected MethodSpec constructor() {
return MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(getClientInterfaceName(), CLIENT_MEMBER)
.addParameter(requestType(), REQUEST_MEMBER)
.addStatement("this.$L = $L", CLIENT_MEMBER, CLIENT_MEMBER)
.addStatement("this.$L = $L", REQUEST_MEMBER, REQUEST_MEMBER)
.build();
}
@Override
protected String nextPageFetcherArgument() {
return String.format("new %s()", nextPageFetcherClassName());
}
@Override
protected TypeSpec.Builder nextPageFetcherClass() {
return super.nextPageFetcherClass()
.addField(FieldSpec.builder(Object.class, LAST_TOKEN_MEMBER, Modifier.PRIVATE)
.build());
}
@Override
protected CodeBlock hasNextPageMethodBody() {
if (paginatorDefinition.getMoreResults() != null) {
return CodeBlock.builder()
.add("return $1N.$2L != null && $1N.$2L.booleanValue()",
PREVIOUS_PAGE_METHOD_ARGUMENT,
fluentGetterMethodForResponseMember(paginatorDefinition.getMoreResults()))
.build();
}
// If there is no more_results token, then output_token will be a single value
return CodeBlock.builder()
.add("return $3T.isOutputTokenAvailable($1N.$2L) && ",
PREVIOUS_PAGE_METHOD_ARGUMENT,
fluentGetterMethodsForOutputToken().get(0),
PaginatorUtils.class)
.add("!$1N.$2L.equals($3L)",
PREVIOUS_PAGE_METHOD_ARGUMENT,
fluentGetterMethodsForOutputToken().get(0),
LAST_TOKEN_MEMBER)
.build();
}
@Override
protected CodeBlock nextPageMethodBody() {
return CodeBlock.builder()
.beginControlFlow("if ($L == null)", PREVIOUS_PAGE_METHOD_ARGUMENT)
.addStatement("$L = null", LAST_TOKEN_MEMBER)
.addStatement("return $L.$L($L)", CLIENT_MEMBER, operationModel.getMethodName(), REQUEST_MEMBER)
.endControlFlow()
.addStatement("$1L = $2L.$3L", LAST_TOKEN_MEMBER, PREVIOUS_PAGE_METHOD_ARGUMENT,
fluentGetterMethodsForOutputToken().get(0))
.addStatement(codeToGetNextPageIfOldResponseIsNotNull())
.build();
}
}