From 9cb4074d3984b63e3c64c4825afe740f3d5abbab Mon Sep 17 00:00:00 2001 From: Steven Choi Date: Wed, 13 Sep 2023 16:16:16 +1000 Subject: [PATCH] #777 Add API to list collections --- .../org/ala/profile/api/ApiController.groovy | 42 ++++++++++++++++++- .../org/ala/profile/api/ApiInterceptor.groovy | 2 + .../au/org/ala/profile/hub/UrlMappings.groovy | 1 + .../org/ala/profile/hub/ProfileService.groovy | 5 +++ .../ala/profile/api/ApiControllerSpec.groovy | 11 +++++ 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/grails-app/controllers/au/org/ala/profile/api/ApiController.groovy b/grails-app/controllers/au/org/ala/profile/api/ApiController.groovy index 7b9951d2..333a5c24 100644 --- a/grails-app/controllers/au/org/ala/profile/api/ApiController.groovy +++ b/grails-app/controllers/au/org/ala/profile/api/ApiController.groovy @@ -24,7 +24,7 @@ import au.org.ala.plugins.openapi.Path type = SecuritySchemeType.HTTP, scheme = "bearer" ) -@RequireApiKey() +//@RequireApiKey() class ApiController extends BaseController { static namespace = "v1" static allowedSortFields = ['scientificNameLower', 'lastUpdated', 'dateCreated'] @@ -34,6 +34,7 @@ class ApiController extends BaseController { MapService mapService ApiService apiService + @RequireApiKey() @Path("/api/opus/{opusId}") @Operation( summary = "Get collection (opus) details", @@ -106,6 +107,41 @@ class ApiController extends BaseController { } } + @Path("/api/opus/list") + @Operation( + summary = "Get all collection (opus) details", + operationId = "/api/opus/list", + method = "GET", + responses = [ + @ApiResponse( + responseCode = "200", + content = @Content( + mediaType = "application/json", + array = @ArraySchema( + schema = @Schema( + implementation = OpusResponse.class + ) + ) + ) + ), + @ApiResponse(responseCode = "400", + description = "opusId is a required parameter"), + @ApiResponse(responseCode = "403", + description = "You do not have the necessary permissions to perform this action."), + @ApiResponse(responseCode = "405", + description = "An unexpected error has occurred while processing your request."), + @ApiResponse(responseCode = "404", + description = "Collection not found"), + @ApiResponse(responseCode = "500", + description = "An unexpected error has occurred while processing your request.") + ] + ) + def getListCollections () { + List opus = profileService.getOpusList() as List + render opus as JSON + } + + @RequireApiKey() @Path("/api/opus/{opusId}/profile") @Operation( summary = "List profiles in a collection", @@ -245,6 +281,7 @@ class ApiController extends BaseController { } } + @RequireApiKey() @Path("/api/opus/{opusId}/profile/{profileId}") @Operation( summary = "Get a profile in a collection", @@ -333,6 +370,7 @@ class ApiController extends BaseController { } } + @RequireApiKey() @Path("/api/opus/{opusId}/profile/{profileId}/draft") @Operation( summary = "Get a draft profile in a collection", @@ -416,6 +454,7 @@ class ApiController extends BaseController { } } + @RequireApiKey() @Path("/api/opus/{opusId}/profile/{profileId}/image") @Operation( summary = "Get images associated with a profile", @@ -510,6 +549,7 @@ class ApiController extends BaseController { } } + @RequireApiKey() @Path("/api/opus/{opusId}/profile/{profileId}/attribute/{attributeId}") @Operation( summary = "Get attributes of a profile in a collection", diff --git a/grails-app/controllers/au/org/ala/profile/api/ApiInterceptor.groovy b/grails-app/controllers/au/org/ala/profile/api/ApiInterceptor.groovy index 00220bff..f0387c28 100644 --- a/grails-app/controllers/au/org/ala/profile/api/ApiInterceptor.groovy +++ b/grails-app/controllers/au/org/ala/profile/api/ApiInterceptor.groovy @@ -26,6 +26,8 @@ class ApiInterceptor { Class controllerClass = controller?.clazz def method = controllerClass?.getMethod(actionName, [] as Class[]) + if (method.name == "getListCollections") return true + if (authorization) { if (params.opusId && (opus = profileService.getOpus(params.opusId))) { params.isOpusPrivate = opus.privateCollection diff --git a/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy b/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy index 971755e6..8d855743 100644 --- a/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy +++ b/grails-app/controllers/au/org/ala/profile/hub/UrlMappings.groovy @@ -263,6 +263,7 @@ class UrlMappings { get "/opus/$opusId/profile/$profileId/image" (version: "1.0", controller: "api", action: "getImages", namespace: "v1") get "/opus/$opusId/profile/$profileId/attribute/$attributeId" (version: "1.0", controller: "api", action: "getAttributes", namespace: "v1") get "/opus/$opusId/profile/$profileId/draft" (version: "1.0", controller: "api", action: "getDraftProfile", namespace: "v1") + get "/opus/list" (version: "1.0", controller: "api", action: "getListCollections", namespace: "v1") } "/openapi/$action?/$id?(.$format)?"(controller: "openApi") diff --git a/grails-app/services/au/org/ala/profile/hub/ProfileService.groovy b/grails-app/services/au/org/ala/profile/hub/ProfileService.groovy index d18e685b..d996e042 100644 --- a/grails-app/services/au/org/ala/profile/hub/ProfileService.groovy +++ b/grails-app/services/au/org/ala/profile/hub/ProfileService.groovy @@ -45,6 +45,11 @@ class ProfileService { webServiceWrapperService.get("${grailsApplication.config.profile.service.url}/opus/${encPath(opusId)}", [:], ContentType.APPLICATION_JSON, true, false, getCustomHeaderWithUserId())?.resp } + def getOpusList() { + webServiceWrapperService.get("${grailsApplication.config.profile.service.url}/opus/list", [:], ContentType.APPLICATION_JSON, false, false, null)?.resp + } + + def updateOpus(String opusId, Map json) { webService.post("${grailsApplication.config.profile.service.url}/opus/${encPath(opusId)}", json, [:], ContentType.APPLICATION_JSON, true, false, getCustomHeaderWithUserId()) } diff --git a/src/test/groovy/au/org/ala/profile/api/ApiControllerSpec.groovy b/src/test/groovy/au/org/ala/profile/api/ApiControllerSpec.groovy index ee560ec6..270a4dbd 100644 --- a/src/test/groovy/au/org/ala/profile/api/ApiControllerSpec.groovy +++ b/src/test/groovy/au/org/ala/profile/api/ApiControllerSpec.groovy @@ -149,4 +149,15 @@ class ApiControllerSpec extends Specification implements ControllerUnitTest> [[uuid: 'abc',shortName:'alatest',title:'title1',desciption:'desc1',thubnailUrl:'test.png']] + + when: + controller.getListCollections() + + then: + response.status == 200 + } }