1
1
package io .kubernetes .client .pager ;
2
2
3
+ import java .io .IOException ;
4
+ import java .lang .reflect .Type ;
5
+ import java .util .function .Function ;
3
6
import com .google .gson .JsonObject ;
4
7
import com .google .gson .JsonParser ;
5
- import com .google .gson .reflect .TypeToken ;
6
8
import com .squareup .okhttp .Call ;
7
- import com .squareup .okhttp .HttpUrl ;
8
9
import com .squareup .okhttp .Request ;
9
10
import com .squareup .okhttp .Response ;
11
+ import com .squareup .okhttp .ResponseBody ;
10
12
import io .kubernetes .client .ApiClient ;
11
13
import io .kubernetes .client .ApiException ;
12
- import io .kubernetes .client .models .V1ConfigMapList ;
13
14
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 ;
18
16
19
17
public class Pager {
20
- private Request originalRequest ;
21
18
private String _continue ;
22
- private String resourceVersion ;
23
19
private Integer limit ;
24
20
private ApiClient client ;
25
21
private Call call ;
26
22
private Type listType ;
23
+ private Function <PagerParams , Call > listFunc ;
27
24
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 ;
29
27
this .client = client ;
30
- this .call = call ;
31
28
this .limit = limit ;
32
29
this .listType = listType ;
33
30
}
34
31
35
32
public Boolean hasNext () {
36
- if (_continue == null && originalRequest != null ) {
33
+ if (_continue == null && call != null ) {
37
34
return Boolean .FALSE ;
38
35
}
39
36
return Boolean .TRUE ;
@@ -42,43 +39,27 @@ public Boolean hasNext() {
42
39
public <T > T next ()
43
40
throws NoSuchFieldException , SecurityException , IllegalArgumentException ,
44
41
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 ();
59
43
return executeRequest (client , call , listType );
60
44
}
61
45
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 );
74
53
}
75
54
76
55
private <T > T executeRequest (ApiClient client , Call call , Type listType )
77
56
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
+ }
79
61
80
- String line = response .body ().source ().readUtf8Line ();
81
- // TODO: handle all responses.
62
+ private void setNextContinue (String line ) {
82
63
JsonParser parser = new JsonParser ();
83
64
JsonObject json = (JsonObject ) parser .parse (line );
84
65
if (json .has ("kind" ) && json .has ("metadata" )) {
@@ -87,11 +68,12 @@ private <T> T executeRequest(ApiClient client, Call call, Type listType)
87
68
V1ListMeta metaList =
88
69
client .getJSON ().deserialize (json .get ("metadata" ).toString (), V1ListMeta .class );
89
70
_continue = metaList .getContinue ();
90
- resourceVersion = metaList .getResourceVersion ();
71
+ }else {
72
+ throw new RuntimeException ("Subsequent call failed " + line );
91
73
}
92
74
} else {
93
75
throw new RuntimeException ("Subsequent call failed " + line );
94
76
}
95
- return client .getJSON ().deserialize (line , listType );
96
77
}
78
+
97
79
}
0 commit comments