mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 08:31:53 -05:00
fix(apigateway): paginate GetResources with limit and position (#9571)
This commit is contained in:
1 parent
15a0aacb06
commit
080e968a0c
3 files changed
+100
-6
No files matched your search
@@ -473,14 +473,12 @@ public class ApiGatewayController {
|
||||
@GET
|
||||
@Path("/restapis/{apiId}/resources")
|
||||
public Response getResources(@Context HttpHeaders headers, @PathParam("apiId") String apiId,
|
||||
@QueryParam("embed") List<String> embed) {
|
||||
@QueryParam("embed") List<String> embed,
|
||||
@QueryParam("limit") String limit, @QueryParam("position") String position) {
|
||||
String region = regionResolver.resolveRegion(headers);
|
||||
List<ApiGatewayResource> resources = service.getResources(region, apiId);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("item");
|
||||
boolean embedMethods = embed != null && embed.contains("methods");
|
||||
resources.forEach(r -> items.add(toResourceNode(r, embedMethods)));
|
||||
return Response.ok(root.toString()).type(MediaType.APPLICATION_JSON).build();
|
||||
return pageResponse(service.getResourcePage(region, apiId, pageLimit(limit), position),
|
||||
resource -> toResourceNode(resource, embedMethods));
|
||||
}
|
||||
|
||||
@GET
|
||||
|
||||
@@ -524,6 +524,11 @@ public class ApiGatewayService implements ResourceProvider, Resettable {
|
||||
return resourceStore.scan(k -> k.startsWith(prefix));
|
||||
}
|
||||
|
||||
public PaginatedResult<ApiGatewayResource> getResourcePage(String region, String apiId, Integer limit,
|
||||
String position) {
|
||||
return pageById(getResources(region, apiId), ApiGatewayResource::getId, limit, position);
|
||||
}
|
||||
|
||||
/** The root resource id, with a resource-store fallback for data persisted before it was stored on the API. */
|
||||
public Optional<String> findRootResourceId(String region, String apiId) {
|
||||
Optional<RestApi> api = apiStore.get(apiKey(region, apiId));
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package io.floci.aws.services.apigateway;
|
||||
|
||||
import io.floci.aws.testing.AwsTestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.http.apache5.Apache5HttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.apigateway.ApiGatewayClient;
|
||||
import software.amazon.awssdk.services.apigateway.model.BadRequestException;
|
||||
import software.amazon.awssdk.services.apigateway.model.GetResourcesResponse;
|
||||
import software.amazon.awssdk.services.apigateway.model.NotFoundException;
|
||||
import software.amazon.awssdk.services.apigateway.model.Resource;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
class ApiGatewayResourcePagingIntegrationTest {
|
||||
|
||||
private ApiGatewayClient client() {
|
||||
return AwsTestClients.create(ApiGatewayClient.builder(), "test", Region.US_EAST_1,
|
||||
Apache5HttpClient.builder());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getResourcesWithLimitFollowsPositionToTerminalPage() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String apiId = client.createRestApi(request -> request.name("resource-paging-sdk")).id();
|
||||
try {
|
||||
String rootId = client.getResources(request -> request.restApiId(apiId)).items().get(0).id();
|
||||
Set<String> created = new HashSet<>();
|
||||
created.add(rootId);
|
||||
for (String part : List.of("a", "b")) {
|
||||
created.add(client.createResource(request -> request.restApiId(apiId).parentId(rootId)
|
||||
.pathPart(part)).id());
|
||||
}
|
||||
|
||||
List<String> seen = ApiGatewayPagingWalk.oneItemPerPageIds(
|
||||
position -> client.getResources(
|
||||
request -> request.restApiId(apiId).limit(1).position(position)),
|
||||
GetResourcesResponse::items, GetResourcesResponse::position, Resource::id);
|
||||
assertEquals(created, new HashSet<>(seen));
|
||||
|
||||
GetResourcesResponse all = client.getResources(request -> request.restApiId(apiId));
|
||||
assertEquals(3, all.items().size());
|
||||
assertNull(all.position());
|
||||
|
||||
assertThrows(BadRequestException.class, () -> client.getResources(
|
||||
request -> request.restApiId(apiId).limit(1).position("missing")));
|
||||
assertThrows(BadRequestException.class, () -> client.getResources(
|
||||
request -> request.restApiId(apiId).limit(0)));
|
||||
assertThrows(NotFoundException.class, () -> client.getResources(
|
||||
request -> request.restApiId("missing").limit(1)));
|
||||
} finally {
|
||||
client.deleteRestApi(request -> request.restApiId(apiId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getResourcesKeepsEmbeddedMethodsOnEachPage() {
|
||||
try (ApiGatewayClient client = client()) {
|
||||
String apiId = client.createRestApi(request -> request.name("resource-paging-embed")).id();
|
||||
try {
|
||||
String rootId = client.getResources(request -> request.restApiId(apiId)).items().get(0).id();
|
||||
String childId = client.createResource(request -> request.restApiId(apiId).parentId(rootId)
|
||||
.pathPart("a")).id();
|
||||
client.putMethod(request -> request.restApiId(apiId).resourceId(childId).httpMethod("GET")
|
||||
.authorizationType("NONE"));
|
||||
|
||||
GetResourcesResponse first = client.getResources(
|
||||
request -> request.restApiId(apiId).embed("methods").limit(1));
|
||||
assertEquals(1, first.items().size());
|
||||
assertNotNull(first.position());
|
||||
GetResourcesResponse second = client.getResources(
|
||||
request -> request.restApiId(apiId).embed("methods").limit(1).position(first.position()));
|
||||
assertNull(second.position());
|
||||
Resource child = (first.items().get(0).id().equals(childId) ? first : second).items().get(0);
|
||||
assertEquals(Set.of("GET"), child.resourceMethods().keySet());
|
||||
} finally {
|
||||
client.deleteRestApi(request -> request.restApiId(apiId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user