Skip to content

Commit

Permalink
#2 OpenAPI getMetadataByUUID, deleteMetadata, switch to 3.2 as versio…
Browse files Browse the repository at this point in the history
…n starting open API support
  • Loading branch information
eblondel committed Sep 13, 2021
1 parent 3cb7c2d commit 90be1f6
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 59 deletions.
20 changes: 16 additions & 4 deletions R/GNManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @description The function \code{GNManager$new} will set-up the right Geonetwork
#' manager depending on the GeoNetwork version specified by the user. For the time-being,
#' GeoNetwork with version < 4 will be interfaced with the GeoNetwork legacy API (see detailed
#' documentation at \link{GNLegacyAPIManager}), while starting with GeoNetwork 4, the new
#' documentation at \link{GNLegacyAPIManager}), while starting with GeoNetwork 3.2, the new
#' GeoNetwork OpenAPI will be used.
#' @return Object of \code{\link{R6Class}} with methods for communication with
#' the API of a GeoNetwork instance.
Expand All @@ -40,9 +40,21 @@ GNManager <- R6Class("GNManager",

GNManager$new <- function(url, user = NULL, pwd = NULL, version, logger = NULL){
gn_version <- GNVersion$new(version = version)
if(gn_version$value$major < 4){ #to be refine to some 3.x versions if needed
return(GNLegacyAPIManager$new(url, user = user, pwd = pwd, version = version, logger = logger))
useOpenAPI <- FALSE
if(gn_version$value$major >= 3){
if(gn_version$value$major == 3){
if(gn_version$value$minor >= 2) useOpenAPI <- TRUE
}else{
useOpenAPI <- TRUE
}
}
manager <- NULL
if(useOpenAPI){
manager <- GNOpenAPIManager$new(url, user = user, pwd = pwd, version = version, logger = logger)
manager$INFO("GN version >= 3.2 - Set-up GeoNetwork Open API manager")
}else{
return(GNOpenAPIManager$new(url, user = user, pwd = pwd, version = version, logger = logger))
manager <- GNLegacyAPIManager$new(url, user = user, pwd = pwd, version = version, logger = logger)
manager$INFO("GN version < 3.2 - Set-up GeoNetwork Legacy API manager")
}
return(manager)
}
103 changes: 62 additions & 41 deletions R/GNOpenAPIManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
#' Extra parameters related to \pkg{geometa} objects: \code{geometa_validate} (TRUE by default) and \code{geometa_inspire}
#' (FALSE by default) can be used to perform ISO and INSPIRE validation respectively.
#' }
#' \item{\code{getMetadataByUUID(uuid)}}{
#' Get a metadata by UUID. Returns an object of class \code{ISOMetadata} (ISO 19115)
#' or \code{ISOFeatureCatalogue} (ISO 19110) (from \pkg{geometa} package)
#' }
#' \item{\code{updateMetadata(xml, file, geometa, metadataType,
#' group, category, rejectIfInvalid, publishToAll,
#' transformWith, schema, extra,
Expand All @@ -83,6 +87,9 @@
#' by default) and \code{geometa_inspire} (FALSE by default) can be used with geometa objects
#' for perform ISO and INSPIRE validation respectively.
#' }
#' \item{\code{deleteMetadata(id, withBackup)}}{
#' Deletes a metadata
#' }
#' }
#'
#' @author Emmanuel Blondel <emmanuel.blondel1@@gmail.com>
Expand Down Expand Up @@ -257,7 +264,7 @@ GNOpenAPIManager <- R6Class("GNOpenAPIManager",

if(is.null(category)) category <- "_none_"

self$INFO("Inserting metadata ...")
self$INFO("Inserting/updating metadata ...")
out <- NULL
data <- NULL
isTempFile <- FALSE
Expand Down Expand Up @@ -321,41 +328,43 @@ GNOpenAPIManager <- R6Class("GNOpenAPIManager",
return(out)
},

#setPrivConfiguration
#---------------------------------------------------------------------------
setPrivConfiguration = function(id, config){
#TODO
},


#get
#---------------------------------------------------------------------------
get = function(id, by, output){
#TODO
},

#getMetadataByID
#---------------------------------------------------------------------------
getMetadataByID = function(id){
#TODO
},

#getMetadataByUUID
#---------------------------------------------------------------------------
getMetadataByUUID = function(uuid){
#TODO
},

#getInfoByID
#---------------------------------------------------------------------------
getInfoByID = function(id){
#TODO
},
getMetadataByUUID = function(uuid,
addSchemaLocation = TRUE, increasePopularity = TRUE, approved = TRUE){
addSchemaLocation <- tolower(as.character(addSchemaLocation))
increasePopularity <- tolower(as.character(increasePopularity))
approved <- tolower(as.character(approved))

#getInfoByUUID
#---------------------------------------------------------------------------
getInfoByUUID = function(uuid){
#TODO
self$INFO(sprintf("Fetching metadata for uuid = '%s'", uuid))
out <- NULL
req <- GNUtils$GET(
url = self$getUrl(),
path = sprintf("/api/records/%s/formatters/xml?addSchemaLocation=%s&increasePopularity=%s&approved=%s",
uuid, addSchemaLocation, increasePopularity, approved),
token = private$getToken(), cookies = private$cookies,
user = private$user,
pwd = private$getPwd(),
accept = "application/xml", contentType = "application/xml",
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully fetched metadata!")
xml <- GNUtils$parseResponseXML(req, "UTF-8")

#bridge to geometa package once geometa XML decoding supported
isoClass <- xmlName(xmlRoot(xml))
out <- NULL
if(isoClass=="MD_Metadata"){
out <- geometa::ISOMetadata$new(xml = xml)
}else if(isoClass=="FC_FeatureCatalogue"){
out <- geometa::ISOFeatureCatalogue$new(xml = xml)
}
}else{
self$ERROR(sprintf("Error while fetching metadata - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
},

#updateMetadata
Expand All @@ -374,14 +383,26 @@ GNOpenAPIManager <- R6Class("GNOpenAPIManager",

#deleteMetadata
#---------------------------------------------------------------------------
deleteMetadata = function(id){
#TODO
},

#deleteMetadataAll
#---------------------------------------------------------------------------
deleteMetadataAll = function(){
#TODO
deleteMetadata = function(id, withBackup = TRUE){
self$INFO(sprintf("Deleting metadata id = %s ...", id))
out <- NULL
req <- GNUtils$DELETE(
url = self$getUrl(),
path = sprintf("/api/records?uuids=%s&withBackup=%s", id, tolower(as.character(withBackup))),
token = private$getToken(), cookies = private$cookies,
user = private$user,
pwd = private$getPwd(),
verbose = self$verbose.debug
)
if(status_code(req) == 200){
self$INFO("Successfully deleted metadata!")
response = content(req)
out <- response
}else{
self$ERROR(sprintf("Error while deleting metadata - %s", message_for_status(status_code(req))))
self$ERROR(content(req))
}
return(out)
}

)
Expand Down
10 changes: 8 additions & 2 deletions R/GNUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ GNUtils$getUserAgent <- function(){

GNUtils$GET <- function(url, path = NULL, token = NULL, cookies = NULL,
user = NULL, pwd = NULL,
query = NULL, verbose = FALSE){
query = NULL,
accept = "application/json", contentType = "application/json",
verbose = FALSE){
if(verbose){
req <- with_verbose(GNUtils$GET(url, path, token, cookies, user, pwd, query))
req <- with_verbose(GNUtils$GET(url, path, token, cookies, user, pwd, query, accept, contentType))
}else{
if(!is.null(path)){
if(!grepl("^/", path)) path = paste0("/", path)
Expand All @@ -66,6 +68,8 @@ GNUtils$GET <- function(url, path = NULL, token = NULL, cookies = NULL,
url = url,
query = query,
add_headers(
"Accept" = accept,
"Content-Type" = contentType,
"User-Agent" = GNUtils$getUserAgent(),
"Authorization" = paste("Basic", GNUtils$getUserToken(user, pwd)),
"X-XSRF-TOKEN" = token,
Expand All @@ -77,6 +81,8 @@ GNUtils$GET <- function(url, path = NULL, token = NULL, cookies = NULL,
url = url,
query = query,
add_headers(
"Accept" = accept,
"Content-Type" = contentType,
"User-Agent" = GNUtils$getUserAgent(),
"X-XSRF-TOKEN" = token,
"Set-Cookie" = cookies
Expand Down
2 changes: 1 addition & 1 deletion man/GNManager.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions man/GNOpenAPIManager.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 28 additions & 11 deletions tests/testthat/test_GNManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ test_that("GET tags/categories",{
test_that("CREATE metadata",{
mdfile <- system.file("extdata/examples", "metadata.xml", package = "geonapi")
md <- geometa::readISO19139(file = mdfile)
created = GN$insertMetadata(geometa = md, group = "1", category = "datasets")
config <- GNPrivConfiguration$new()
config$setPrivileges("all", c("view","dynamic","featured"))
GN$setPrivConfiguration(id = created, config = config)
expect_true(!is.null(created))
if(GN$getClassName() == "GNLegacyAPIManager"){
created = GN$insertMetadata(geometa = md, group = "1", category = ifelse(GN$getClassName() == "GNOpenAPIManager", "1", "datasets"))
config <- GNPrivConfiguration$new()
config$setPrivileges("all", c("view","dynamic","featured"))
GN$setPrivConfiguration(id = created, config = config)
expect_true(!is.null(created))
}else if(GN$getClassName() == "GNOpenAPIManager"){
created = GN$insertMetadata(geometa = md, group = "1", category = "1")
expect_equal(created$numberOfRecordsProcessed, 1L)
}

})

test_that("READ metadata",{
Expand All @@ -53,14 +59,25 @@ test_that("UPDATE metadata",{
id <- "my-metadata-identifier"
md <- GN$getMetadataByUUID(id)
md$setDataSetURI("new-dataset-uri")
metaId <- GN$get(md$fileIdentifier, by = "uuid", output = "id")
updated <- GN$updateMetadata(id = metaId, geometa = md)
expect_equal(updated, metaId)
md$identificationInfo[[1]]$setAbstract("new abstract with additional information")
if(GN$getClassName() == "GNLegacyAPIManager"){
metaId <- GN$get(md$fileIdentifier, by = "uuid", output = "id")
updated <- GN$updateMetadata(id = metaId, geometa = md)
expect_equal(updated, metaId)
}else if(GN$getClassName() == "GNOpenAPIManager"){
updated = GN$updateMetadata(geometa = md, group = "1", category = "1")
expect_equal(updated$numberOfRecordsProcessed, 1L)
}
})

test_that("DELETE metadata",{
id <- "my-metadata-identifier"
metaId <- GN$get(id, by = "uuid", output = "id")
deleted <- GN$deleteMetadata(id = metaId)
expect_equal(deleted, metaId)
if(GN$getClassName() == "GNLegacyAPIManager"){
metaId <- GN$get(id, by = "uuid", output = "id")
deleted <- GN$deleteMetadata(id = metaId)
expect_equal(deleted, metaId)
}else if(GN$getClassName() == "GNOpenAPIManager"){
deleted <- GN$deleteMetadata(id = id)
expect_equal(deleted$numberOfRecordsProcessed, 1L)
}
})

0 comments on commit 90be1f6

Please sign in to comment.