|
| 1 | +/* |
| 2 | + * Copyright 2025 https://dnation.cloud |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package cloud.dnation.hetznerclient; |
| 17 | + |
| 18 | +import com.google.common.base.Preconditions; |
| 19 | +import lombok.experimental.UtilityClass; |
| 20 | +import retrofit2.Call; |
| 21 | +import retrofit2.Response; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Objects; |
| 27 | +import java.util.function.BiFunction; |
| 28 | +import java.util.function.Function; |
| 29 | + |
| 30 | +@UtilityClass |
| 31 | +public class PagedResourceHelper { |
| 32 | + /** |
| 33 | + * Consume all items from paginated REST endpoint. |
| 34 | + * |
| 35 | + * @param labelSelector label selector to restrict items only to those that match selector |
| 36 | + * @param pageSupplier {@link BiFunction} that takes page index (zero based) and selector and produces {@link Response} |
| 37 | + * @param itemsGetter {@link Function} that takes response from pageSupplier and extracts list of items |
| 38 | + * @return all items combined to single list |
| 39 | + * @param <T> item type |
| 40 | + * @param <X> REST endpoint response type |
| 41 | + */ |
| 42 | + public static <T extends IdentifiableResource, X extends AbstractSearchResponse> List<T> fetchItems( |
| 43 | + String labelSelector, |
| 44 | + BiFunction<Integer, String, Call<X>> pageSupplier, Function<X, List<T>> itemsGetter) throws IOException { |
| 45 | + final List<T> result = new ArrayList<>(); |
| 46 | + for (int i = 0; ; i++) { |
| 47 | + final Response<X> page = pageSupplier.apply(i, labelSelector).execute(); |
| 48 | + assertValidResponse(page); |
| 49 | + final X body = page.body(); |
| 50 | + Objects.requireNonNull(body, "Missing response body"); |
| 51 | + final Meta meta = body.getMeta(); |
| 52 | + result.addAll(itemsGetter.apply(body)); |
| 53 | + Objects.requireNonNull(meta, "Missing meta response object"); |
| 54 | + Objects.requireNonNull(meta.getPagination(), "Missing pagination inside meta response object"); |
| 55 | + if (meta.getPagination().getNextPage() == null) { |
| 56 | + // last page |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + public static List<PrimaryIpDetail> getAllPrimaryIps(HetznerApi api, String labelSelector) throws IOException { |
| 64 | + return fetchItems(labelSelector, (pageId, sel) -> |
| 65 | + api.getPrimaryIpsBySelector(sel, pageId, 25), GetAllPrimaryIpsResponse::getPrimaryIps); |
| 66 | + } |
| 67 | + |
| 68 | + public static List<ServerDetail> getAllServers(HetznerApi api, String labelSelector) throws IOException { |
| 69 | + return fetchItems(labelSelector, (pageId, sel) -> |
| 70 | + api.getServersBySelector(sel, pageId, 25), GetServersBySelectorResponse::getServers); |
| 71 | + } |
| 72 | + |
| 73 | + private static <X extends AbstractSearchResponse> void assertValidResponse(Response<X> response) { |
| 74 | + Preconditions.checkArgument(response.isSuccessful(), |
| 75 | + "Invalid response code: %d", response.code()); |
| 76 | + } |
| 77 | +} |
0 commit comments