11package io .kubernetes .client .pager ;
22
3+ import java .io .IOException ;
4+ import java .lang .reflect .Type ;
5+ import java .util .function .Function ;
36import com .google .gson .JsonObject ;
47import com .google .gson .JsonParser ;
5- import com .google .gson .reflect .TypeToken ;
68import com .squareup .okhttp .Call ;
7- import com .squareup .okhttp .HttpUrl ;
89import com .squareup .okhttp .Request ;
910import com .squareup .okhttp .Response ;
11+ import com .squareup .okhttp .ResponseBody ;
1012import io .kubernetes .client .ApiClient ;
1113import io .kubernetes .client .ApiException ;
12- import io .kubernetes .client .models .V1ConfigMapList ;
1314import 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
1917public 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}
0 commit comments