1
+ /*
2
+ Copyright 2019 The Kubernetes Authors.
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
12
+ */
1
13
package io .kubernetes .client .pager ;
2
14
3
15
import com .squareup .okhttp .Call ;
4
16
import io .kubernetes .client .ApiClient ;
5
17
import io .kubernetes .client .ApiException ;
6
18
import io .kubernetes .client .models .V1ListMeta ;
7
- import io .kubernetes .client .util .PagerParams ;
8
19
import io .kubernetes .client .util .Reflect ;
9
20
import io .kubernetes .client .util .exception .ObjectMetaReflectException ;
10
21
import java .io .IOException ;
11
22
import java .lang .reflect .Type ;
12
23
import java .util .function .Function ;
13
24
14
- public class Pager {
15
- private String _continue ;
25
+ public class Pager < ApiType , ApiListType > {
26
+ private String continueToken ;
16
27
private Integer limit ;
17
28
private ApiClient client ;
18
29
private Call call ;
@@ -22,10 +33,11 @@ public class Pager {
22
33
/**
23
34
* Pagination in kubernetes list call depends on continue and limit variable
24
35
*
25
- * @param listFunc
26
- * @param client
27
- * @param limit
28
- * @param listType
36
+ * @param listFunc lambda of type: (PagerParams p)->{return
37
+ * list<*>[namespace[s|d]]*<*>Call(...p.getContinue(),...p.getLimit()...);}
38
+ * @param client instance of {@link ApiClient}
39
+ * @param limit size of list to be fetched
40
+ * @param listType Type of list to be fetched
29
41
*/
30
42
public Pager (
31
43
Function <PagerParams , Call > listFunc , ApiClient client , Integer limit , Type listType ) {
@@ -41,7 +53,7 @@ public Pager(
41
53
* @return
42
54
*/
43
55
public Boolean hasNext () {
44
- if (_continue == null && call != null ) {
56
+ if (continueToken == null && call != null ) {
45
57
return Boolean .FALSE ;
46
58
}
47
59
return Boolean .TRUE ;
@@ -52,56 +64,43 @@ public Boolean hasNext() {
52
64
*
53
65
* @return Object
54
66
*/
55
- public <T > T next () {
67
+ public <ApiType > ApiListType next () {
56
68
return next (null );
57
69
}
58
70
59
71
/**
60
- * returns next chunk of List . size of list depends on limit set in constructor or nextLimit.
72
+ * returns next chunk of list . size of list depends on limit set in constructor or nextLimit.
61
73
*
62
74
* @param nextLimit
63
75
* @return
64
76
*/
65
- public <T > T next (Integer nextLimit ) {
77
+ public <ApiType > ApiListType next (Integer nextLimit ) {
66
78
try {
67
79
call = getNextCall (nextLimit );
68
- return executeRequest (client , call , listType );
80
+ return executeRequest (call );
69
81
} catch (Exception e ) {
82
+ if (e instanceof ApiException ) {
83
+ throw new RuntimeException (((ApiException ) e ).getResponseBody ());
84
+ }
70
85
throw new RuntimeException (e );
71
86
}
72
87
}
73
88
74
- /**
75
- * returns next list call by setting continue variable and limit
76
- *
77
- * @param nextLimit
78
- * @return Object
79
- */
89
+ /** returns next list call by setting continue variable and limit */
80
90
private Call getNextCall (Integer nextLimit ) {
81
- PagerParams params = new PagerParams ();
82
- if (_continue != null ) {
83
- params .setContinue (_continue );
91
+ PagerParams params = new PagerParams (( nextLimit != null ) ? nextLimit : limit );
92
+ if (continueToken != null ) {
93
+ params .setContinue (continueToken );
84
94
}
85
- params .setLimit ((nextLimit != null ) ? nextLimit : limit );
86
95
return listFunc .apply (params );
87
96
}
88
97
89
- /**
90
- * executes the list call and sets the continue variable for next list call
91
- *
92
- * @param client
93
- * @param call
94
- * @param listType
95
- * @return
96
- * @throws IOException
97
- * @throws ApiException
98
- * @throws ObjectMetaReflectException
99
- */
100
- private <T > T executeRequest (ApiClient client , Call call , Type listType )
98
+ /** executes the list call and sets the continue variable for next list call */
99
+ private <ApiType > ApiListType executeRequest (Call call )
101
100
throws IOException , ApiException , ObjectMetaReflectException {
102
- T data = client .handleResponse (call .execute (), listType );
101
+ ApiListType data = client .handleResponse (call .execute (), listType );
103
102
V1ListMeta listMetaData = Reflect .listMetadata (data );
104
- _continue = listMetaData .getContinue ();
103
+ continueToken = listMetaData .getContinue ();
105
104
return data ;
106
105
}
107
106
}
0 commit comments