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+ */
113package io .kubernetes .client .pager ;
214
315import com .squareup .okhttp .Call ;
416import io .kubernetes .client .ApiClient ;
517import io .kubernetes .client .ApiException ;
618import io .kubernetes .client .models .V1ListMeta ;
7- import io .kubernetes .client .util .PagerParams ;
819import io .kubernetes .client .util .Reflect ;
920import io .kubernetes .client .util .exception .ObjectMetaReflectException ;
1021import java .io .IOException ;
1122import java .lang .reflect .Type ;
1223import java .util .function .Function ;
1324
14- public class Pager {
15- private String _continue ;
25+ public class Pager < ApiType , ApiListType > {
26+ private String continueToken ;
1627 private Integer limit ;
1728 private ApiClient client ;
1829 private Call call ;
@@ -22,10 +33,11 @@ public class Pager {
2233 /**
2334 * Pagination in kubernetes list call depends on continue and limit variable
2435 *
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
2941 */
3042 public Pager (
3143 Function <PagerParams , Call > listFunc , ApiClient client , Integer limit , Type listType ) {
@@ -41,7 +53,7 @@ public Pager(
4153 * @return
4254 */
4355 public Boolean hasNext () {
44- if (_continue == null && call != null ) {
56+ if (continueToken == null && call != null ) {
4557 return Boolean .FALSE ;
4658 }
4759 return Boolean .TRUE ;
@@ -52,56 +64,43 @@ public Boolean hasNext() {
5264 *
5365 * @return Object
5466 */
55- public <T > T next () {
67+ public <ApiType > ApiListType next () {
5668 return next (null );
5769 }
5870
5971 /**
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.
6173 *
6274 * @param nextLimit
6375 * @return
6476 */
65- public <T > T next (Integer nextLimit ) {
77+ public <ApiType > ApiListType next (Integer nextLimit ) {
6678 try {
6779 call = getNextCall (nextLimit );
68- return executeRequest (client , call , listType );
80+ return executeRequest (call );
6981 } catch (Exception e ) {
82+ if (e instanceof ApiException ) {
83+ throw new RuntimeException (((ApiException ) e ).getResponseBody ());
84+ }
7085 throw new RuntimeException (e );
7186 }
7287 }
7388
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 */
8090 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 );
8494 }
85- params .setLimit ((nextLimit != null ) ? nextLimit : limit );
8695 return listFunc .apply (params );
8796 }
8897
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 )
101100 throws IOException , ApiException , ObjectMetaReflectException {
102- T data = client .handleResponse (call .execute (), listType );
101+ ApiListType data = client .handleResponse (call .execute (), listType );
103102 V1ListMeta listMetaData = Reflect .listMetadata (data );
104- _continue = listMetaData .getContinue ();
103+ continueToken = listMetaData .getContinue ();
105104 return data ;
106105 }
107106}
0 commit comments