Skip to content

Commit 2d49703

Browse files
add PagerParams ResponseUtils
1 parent 46233de commit 2d49703

File tree

4 files changed

+92
-46
lines changed

4 files changed

+92
-46
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.kubernetes.client.models.V1Namespace;
99
import io.kubernetes.client.models.V1NamespaceList;
1010
import io.kubernetes.client.pager.Pager;
11+
import io.kubernetes.client.pager.PagerParams;
1112
import io.kubernetes.client.util.Config;
1213
import java.util.List;
1314
import java.util.concurrent.TimeUnit;
@@ -28,9 +29,23 @@ public static void main(String[] args) {
2829
api.listNamespaceCall(
2930
null, "pretty", null, null, null, 20, null, null, null, null, null);
3031

31-
Pager pager = new Pager(client, call, 1, new TypeToken<V1NamespaceList>() {}.getType());
32-
while (pager.hasNextPage()) {
33-
V1NamespaceList list = pager.getnextPage();
32+
//Pager pager = new Pager(client, call, 1, new TypeToken<V1NamespaceList>() {}.getType());
33+
Pager pager = new Pager(
34+
(PagerParams param)-> {
35+
try {
36+
return api.listNamespaceCall(
37+
null, "pretty", param.getContinue(), null, null, param.getLimit(), null, null, null, null, null);
38+
}
39+
catch(Exception e) {
40+
throw new RuntimeException(e);
41+
}
42+
},
43+
client,
44+
20,
45+
new TypeToken<V1NamespaceList>() {}.getType()
46+
);
47+
while (pager.hasNext()) {
48+
V1NamespaceList list = pager.next();
3449
List<V1Namespace> items = list.getItems();
3550
System.out.println("count:"+items.size());
3651
for (V1Namespace namespace : items) {
Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
package io.kubernetes.client.pager;
22

3+
import java.io.IOException;
4+
import java.lang.reflect.Type;
5+
import java.util.function.Function;
36
import com.google.gson.JsonObject;
47
import com.google.gson.JsonParser;
5-
import com.google.gson.reflect.TypeToken;
68
import com.squareup.okhttp.Call;
7-
import com.squareup.okhttp.HttpUrl;
89
import com.squareup.okhttp.Request;
910
import com.squareup.okhttp.Response;
11+
import com.squareup.okhttp.ResponseBody;
1012
import io.kubernetes.client.ApiClient;
1113
import io.kubernetes.client.ApiException;
12-
import io.kubernetes.client.models.V1ConfigMapList;
1314
import io.kubernetes.client.models.V1ListMeta;
14-
import java.io.IOException;
15-
import java.lang.reflect.Field;
16-
import java.lang.reflect.Type;
17-
import java.util.function.Function;
15+
import io.kubernetes.client.util.ResponseUtils;
1816

1917
public class Pager {
20-
private Request originalRequest;
2118
private String _continue;
22-
private String resourceVersion;
2319
private Integer limit;
2420
private ApiClient client;
2521
private Call call;
2622
private Type listType;
23+
private Function<PagerParams, Call> listFunc;
2724

28-
public Pager(ApiClient client, Call call, Integer limit, Type listType) {
25+
public Pager(Function<PagerParams, Call> listFunc, ApiClient client, Integer limit, Type listType) {
26+
this.listFunc = listFunc;
2927
this.client = client;
30-
this.call = call;
3128
this.limit = limit;
3229
this.listType = listType;
3330
}
3431

3532
public Boolean hasNext() {
36-
if (_continue == null && originalRequest != null) {
33+
if (_continue == null && call != null) {
3734
return Boolean.FALSE;
3835
}
3936
return Boolean.TRUE;
@@ -42,43 +39,27 @@ public Boolean hasNext() {
4239
public <T> T next()
4340
throws NoSuchFieldException, SecurityException, IllegalArgumentException,
4441
IllegalAccessException, IOException, ApiException {
45-
46-
// first call;
47-
if (_continue == null && originalRequest == null) {
48-
Class cls = call.getClass();
49-
Field field = cls.getDeclaredField("originalRequest");
50-
field.setAccessible(true);
51-
originalRequest = (Request) field.get(call);
52-
} else if (_continue == null && originalRequest != null) {
53-
// list was exhausted at server
54-
V1ConfigMapList list =
55-
client.getJSON().deserialize("{}", listType);
56-
}
57-
Request nextRequest = transFormRequest();
58-
call = client.getHttpClient().newCall(nextRequest);
42+
call = getNextCall();
5943
return executeRequest(client, call, listType);
6044
}
6145

62-
private Request transFormRequest() {
63-
HttpUrl url =
64-
originalRequest
65-
.httpUrl()
66-
.newBuilder()
67-
.setQueryParameter("continue", _continue)
68-
.setQueryParameter("limit", (limit==null)?"-1":String.valueOf(limit))
69-
// .addQueryParameter("resourceversion", resourceVersion)
70-
.build();
71-
System.out.println(url);
72-
Request request = new Request.Builder().headers(originalRequest.headers()).url(url).build();
73-
return request;
46+
private Call getNextCall() {
47+
PagerParams params = new PagerParams();
48+
if(_continue != null) {
49+
params.setContinue(_continue);
50+
}
51+
params.setLimit(limit);
52+
return listFunc.apply(params);
7453
}
7554

7655
private <T> T executeRequest(ApiClient client, Call call, Type listType)
7756
throws IOException, ApiException {
78-
Response response = call.execute();
57+
String line = ResponseUtils.getResponseString(call.execute());
58+
setNextContinue(line);
59+
return client.getJSON().deserialize(line, listType);
60+
}
7961

80-
String line = response.body().source().readUtf8Line();
81-
// TODO: handle all responses.
62+
private void setNextContinue(String line) {
8263
JsonParser parser = new JsonParser();
8364
JsonObject json = (JsonObject) parser.parse(line);
8465
if (json.has("kind") && json.has("metadata")) {
@@ -87,11 +68,12 @@ private <T> T executeRequest(ApiClient client, Call call, Type listType)
8768
V1ListMeta metaList =
8869
client.getJSON().deserialize(json.get("metadata").toString(), V1ListMeta.class);
8970
_continue = metaList.getContinue();
90-
resourceVersion = metaList.getResourceVersion();
71+
}else {
72+
throw new RuntimeException("Subsequent call failed " + line);
9173
}
9274
} else {
9375
throw new RuntimeException("Subsequent call failed " + line);
9476
}
95-
return client.getJSON().deserialize(line, listType);
9677
}
78+
9779
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.kubernetes.client.pager;
2+
3+
public class PagerParams {
4+
5+
private Integer limit;
6+
private String _continue;
7+
8+
public Integer getLimit() {
9+
return limit;
10+
}
11+
public void setLimit(Integer limit) {
12+
this.limit = limit;
13+
}
14+
public String getContinue() {
15+
return _continue;
16+
}
17+
public void setContinue(String _continue) {
18+
this._continue = _continue;
19+
}
20+
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.kubernetes.client.util;
2+
3+
import java.io.IOException;
4+
import com.squareup.okhttp.Response;
5+
import com.squareup.okhttp.ResponseBody;
6+
import io.kubernetes.client.ApiException;
7+
8+
public class ResponseUtils {
9+
10+
public static String getResponseString(Response response) throws ApiException {
11+
String respBody = null;
12+
if (response.isSuccessful()) {
13+
try (ResponseBody body = response.body()) {
14+
if (body != null) {
15+
respBody = response.body().source().readUtf8Line();
16+
}
17+
} catch (IOException e) {
18+
throw new ApiException(
19+
response.message(), e, response.code(), response.headers().toMultimap());
20+
}
21+
throw new ApiException(
22+
response.message(), response.code(), response.headers().toMultimap(), respBody);
23+
}
24+
25+
return respBody;
26+
}
27+
28+
}

0 commit comments

Comments
 (0)