|
| 1 | +package io.kubernetes.client.pager; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.google.gson.JsonParser; |
| 5 | +import com.google.gson.reflect.TypeToken; |
| 6 | +import com.squareup.okhttp.Call; |
| 7 | +import com.squareup.okhttp.HttpUrl; |
| 8 | +import com.squareup.okhttp.Request; |
| 9 | +import com.squareup.okhttp.Response; |
| 10 | +import io.kubernetes.client.ApiClient; |
| 11 | +import io.kubernetes.client.ApiException; |
| 12 | +import io.kubernetes.client.models.V1ConfigMapList; |
| 13 | +import io.kubernetes.client.models.V1ListMeta; |
| 14 | +import java.io.IOException; |
| 15 | +import java.lang.reflect.Field; |
| 16 | +import java.lang.reflect.Type; |
| 17 | + |
| 18 | +public class Pager { |
| 19 | + private Request originalRequest; |
| 20 | + private String _continue; |
| 21 | + private String resourceVersion; |
| 22 | + private int limit; |
| 23 | + private ApiClient client; |
| 24 | + private Call call; |
| 25 | + private Type listType; |
| 26 | + |
| 27 | + public Pager(ApiClient client, Call call, int limit, Type listType) { |
| 28 | + this.client = client; |
| 29 | + this.call = call; |
| 30 | + this.limit = limit; |
| 31 | + this.listType = listType; |
| 32 | + } |
| 33 | + |
| 34 | + public Boolean hasNext() { |
| 35 | + if (_continue == null && originalRequest != null) { |
| 36 | + return Boolean.FALSE; |
| 37 | + } |
| 38 | + return Boolean.TRUE; |
| 39 | + } |
| 40 | + |
| 41 | + public <T> T next() |
| 42 | + throws NoSuchFieldException, SecurityException, IllegalArgumentException, |
| 43 | + IllegalAccessException, IOException, ApiException { |
| 44 | + |
| 45 | + // first call; |
| 46 | + if (_continue == null && originalRequest == null) { |
| 47 | + Class cls = call.getClass(); |
| 48 | + Field field = cls.getDeclaredField("originalRequest"); |
| 49 | + field.setAccessible(true); |
| 50 | + originalRequest = (Request) field.get(call); |
| 51 | + } else if (_continue == null && originalRequest != null) { |
| 52 | + // list was exhausted at server |
| 53 | + V1ConfigMapList list = |
| 54 | + client.getJSON().deserialize("{}", new TypeToken<V1ConfigMapList>() {}.getType()); |
| 55 | + } else { |
| 56 | + // subsequent calls |
| 57 | + Request nextRequest = transFormRequest(); |
| 58 | + call = client.getHttpClient().newCall(nextRequest); |
| 59 | + } |
| 60 | + return executeRequest(client, call, listType); |
| 61 | + } |
| 62 | + |
| 63 | + private Request transFormRequest() { |
| 64 | + HttpUrl url = |
| 65 | + originalRequest |
| 66 | + .httpUrl() |
| 67 | + .newBuilder() |
| 68 | + .addQueryParameter("continue", _continue) |
| 69 | + .setQueryParameter("limit", String.valueOf(limit)) |
| 70 | + // .addQueryParameter("resourceversion", resourceVersion) |
| 71 | + .build(); |
| 72 | + Request request = new Request.Builder().headers(originalRequest.headers()).url(url).build(); |
| 73 | + return request; |
| 74 | + } |
| 75 | + |
| 76 | + private <T> T executeRequest(ApiClient client, Call call, Type listType) |
| 77 | + throws IOException, ApiException { |
| 78 | + Response response = call.execute(); |
| 79 | + |
| 80 | + String line = response.body().source().readUtf8Line(); |
| 81 | + // TODO: handle all responses. |
| 82 | + JsonParser parser = new JsonParser(); |
| 83 | + JsonObject json = (JsonObject) parser.parse(line); |
| 84 | + if (json.has("kind") && json.has("metadata")) { |
| 85 | + String kind = json.get("kind").getAsString(); |
| 86 | + if (kind.contains("List")) { |
| 87 | + V1ListMeta metaList = |
| 88 | + client.getJSON().deserialize(json.get("metadata").toString(), V1ListMeta.class); |
| 89 | + _continue = metaList.getContinue(); |
| 90 | + resourceVersion = metaList.getResourceVersion(); |
| 91 | + } |
| 92 | + } else { |
| 93 | + throw new RuntimeException("Subsequent call failed " + line); |
| 94 | + } |
| 95 | + return client.getJSON().deserialize(line, listType); |
| 96 | + } |
| 97 | +} |
0 commit comments