Skip to content

Commit 2749d5b

Browse files
address code review comments
1 parent 97e4a57 commit 2749d5b

File tree

8 files changed

+235
-76
lines changed

8 files changed

+235
-76
lines changed

examples/src/main/java/io/kubernetes/client/examples/PagerExample.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
12-
*/
12+
*/
1313
package io.kubernetes.client.examples;
1414

1515
import io.kubernetes.client.ApiClient;
@@ -18,8 +18,8 @@
1818
import io.kubernetes.client.models.V1Namespace;
1919
import io.kubernetes.client.models.V1NamespaceList;
2020
import io.kubernetes.client.pager.Pager;
21+
import io.kubernetes.client.pager.PagerParams;
2122
import io.kubernetes.client.util.Config;
22-
import io.kubernetes.client.util.PagerParams;
2323
import java.io.IOException;
2424
import java.util.List;
2525
import java.util.concurrent.TimeUnit;
@@ -41,7 +41,7 @@ public static void main(String[] args) throws IOException {
4141
CoreV1Api api = new CoreV1Api();
4242
int i = 0;
4343
Pager pager =
44-
new Pager(
44+
new Pager<V1Namespace, V1NamespaceList>(
4545
(PagerParams param) -> {
4646
try {
4747
return api.listNamespaceCall(
@@ -52,7 +52,7 @@ public static void main(String[] args) throws IOException {
5252
null,
5353
param.getLimit(),
5454
null,
55-
null,
55+
1,
5656
null,
5757
null,
5858
null);
@@ -61,10 +61,10 @@ public static void main(String[] args) throws IOException {
6161
}
6262
},
6363
client,
64-
3,
64+
10,
6565
V1NamespaceList.class);
6666
while (pager.hasNext()) {
67-
V1NamespaceList list = pager.next();
67+
V1NamespaceList list = (V1NamespaceList) pager.next();
6868
List<V1Namespace> items = list.getItems();
6969
System.out.println("count:" + items.size());
7070
for (V1Namespace namespace : items) {
Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
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+
*/
113
package io.kubernetes.client.pager;
214

315
import com.squareup.okhttp.Call;
416
import io.kubernetes.client.ApiClient;
517
import io.kubernetes.client.ApiException;
618
import io.kubernetes.client.models.V1ListMeta;
7-
import io.kubernetes.client.util.PagerParams;
819
import io.kubernetes.client.util.Reflect;
920
import io.kubernetes.client.util.exception.ObjectMetaReflectException;
1021
import java.io.IOException;
1122
import java.lang.reflect.Type;
1223
import 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
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
*/
13+
package io.kubernetes.client.pager;
14+
15+
public class PagerParams {
16+
17+
private Integer limit;
18+
private String continueToken;
19+
20+
public PagerParams(Integer limit) {
21+
this.limit = limit;
22+
}
23+
24+
public PagerParams(Integer limit, String continueToken) {
25+
this.limit = limit;
26+
this.continueToken = continueToken;
27+
}
28+
29+
public Integer getLimit() {
30+
return limit;
31+
}
32+
33+
public String getContinue() {
34+
return continueToken;
35+
}
36+
37+
public void setContinue(String continueToken) {
38+
this.continueToken = continueToken;
39+
}
40+
}

util/src/main/java/io/kubernetes/client/util/PagerParams.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)