|
| 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 | +import com.squareup.okhttp.Call; |
| 16 | +import io.kubernetes.client.ApiClient; |
| 17 | +import io.kubernetes.client.ApiException; |
| 18 | +import io.kubernetes.client.models.V1ListMeta; |
| 19 | +import io.kubernetes.client.util.Reflect; |
| 20 | +import io.kubernetes.client.util.exception.ObjectMetaReflectException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.lang.reflect.Type; |
| 23 | +import java.util.function.Function; |
| 24 | + |
| 25 | +public class Pager<ApiType, ApiListType> { |
| 26 | + private String continueToken; |
| 27 | + private Integer limit; |
| 28 | + private ApiClient client; |
| 29 | + private Call call; |
| 30 | + private Type listType; |
| 31 | + private Function<PagerParams, Call> listFunc; |
| 32 | + |
| 33 | + /** |
| 34 | + * Pagination in kubernetes list call depends on continue and limit variable |
| 35 | + * |
| 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 |
| 41 | + */ |
| 42 | + public Pager( |
| 43 | + Function<PagerParams, Call> listFunc, ApiClient client, Integer limit, Type listType) { |
| 44 | + this.listFunc = listFunc; |
| 45 | + this.client = client; |
| 46 | + this.limit = limit; |
| 47 | + this.listType = listType; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * returns false if kubernetes server has exhausted List. |
| 52 | + * |
| 53 | + * @return |
| 54 | + */ |
| 55 | + public Boolean hasNext() { |
| 56 | + if (continueToken == null && call != null) { |
| 57 | + return Boolean.FALSE; |
| 58 | + } |
| 59 | + return Boolean.TRUE; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * returns next chunk of List. size of list depends on limit set in constructor. |
| 64 | + * |
| 65 | + * @return Object |
| 66 | + */ |
| 67 | + public <ApiType> ApiListType next() { |
| 68 | + return next(null); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * returns next chunk of list. size of list depends on limit set in constructor or nextLimit. |
| 73 | + * |
| 74 | + * @param nextLimit |
| 75 | + * @return |
| 76 | + */ |
| 77 | + public <ApiType> ApiListType next(Integer nextLimit) { |
| 78 | + try { |
| 79 | + call = getNextCall(nextLimit); |
| 80 | + return executeRequest(call); |
| 81 | + } catch (Exception e) { |
| 82 | + if (e instanceof ApiException) { |
| 83 | + throw new RuntimeException(((ApiException) e).getResponseBody()); |
| 84 | + } |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** returns next list call by setting continue variable and limit */ |
| 90 | + private Call getNextCall(Integer nextLimit) { |
| 91 | + PagerParams params = new PagerParams((nextLimit != null) ? nextLimit : limit); |
| 92 | + if (continueToken != null) { |
| 93 | + params.setContinue(continueToken); |
| 94 | + } |
| 95 | + return listFunc.apply(params); |
| 96 | + } |
| 97 | + |
| 98 | + /** executes the list call and sets the continue variable for next list call */ |
| 99 | + private <ApiType> ApiListType executeRequest(Call call) |
| 100 | + throws IOException, ApiException, ObjectMetaReflectException { |
| 101 | + ApiListType data = client.handleResponse(call.execute(), listType); |
| 102 | + V1ListMeta listMetaData = Reflect.listMetadata(data); |
| 103 | + continueToken = listMetaData.getContinue(); |
| 104 | + return data; |
| 105 | + } |
| 106 | +} |
0 commit comments