diff --git a/R/INSPIREMetadataValidator.R b/R/INSPIREMetadataValidator.R index 189e1c95..19a1266b 100644 --- a/R/INSPIREMetadataValidator.R +++ b/R/INSPIREMetadataValidator.R @@ -9,10 +9,11 @@ #' #' @section Methods: #' \describe{ -#' \item{\code{new(apiKey)}}{ +#' \item{\code{new(url, apiKey)}}{ #' This method is used to instantiate an INSPIRE Metadata validator. To check #' metadata with the INSPIRE metadata validator, a user API key is now required, -#' and should be specified with the \code{apiKey}. +#' and should be specified with the \code{apiKey}. By default, the \code{url} will be +#' the INSPIRE production service \url{https://inspire.ec.europa.eu/validator/v2} #' #' The \code{keyring_backend} can be set to use a different backend for storing #' the INSPIRE metadata validator API key with \pkg{keyring} (Default value is 'env'). @@ -21,6 +22,9 @@ #' Upload a XML metadata file to INSPIRE web-service. Method called internally through #' \code{getValidationReport}. #' } +#' \item{\code{getAPIKey()}}{ +#' Get the API user key +#' } #' \item{\code{getValidationReport(obj, file, raw)}}{ #' Get validation report for a metadata specified either as R object of class #' \code{ISOMetadata} (from \pkg{geometa} package) or \code{XMLInternalNode} @@ -48,20 +52,18 @@ INSPIREMetadataValidator <- R6Class("INSPIREMetadataValidator", inherit = geometaLogger, private = list( - host = "https://inspire.ec.europa.eu", - endpoint = "validator/v2", keyring_backend = NULL, keyring_service = NULL ), public = list( - url = NULL, + url = "https://inspire.ec.europa.eu/validator/v2", running = FALSE, - initialize = function(apiKey = NULL, keyring_backend = 'env'){ + initialize = function(url = "https://inspire.ec.europa.eu/validator/v2", + apiKey, keyring_backend = 'env'){ if(!require("httr")){ stop("The INSPIRE metadata validator requires the installation of 'httr' package") } - self$url <- paste(private$host, private$endpoint, sep = "/") - + self$url <- url private$keyring_backend <- keyring:::known_backends[[keyring_backend]]$new() private$keyring_service <- paste0("geometa@", self$url) if(!is.null(apiKey)) private$keyring_backend$set_with_value(private$keyring_service, username = "geometa_inspire_validator", password = apiKey) @@ -94,6 +96,13 @@ INSPIREMetadataValidator <- R6Class("INSPIREMetadataValidator", return(out) }, + #getAPIKey + getAPIKey = function(){ + apiKey <- try(private$keyring_backend$get(service = private$keyring_service, username = "geometa_inspire_validator"), silent = TRUE) + if(is(apiKey, "try-error")) apiKey <- NULL + return(apiKey) + }, + #getValidationReport getValidationReport = function(obj = NULL, file = NULL, raw = FALSE){ @@ -127,14 +136,13 @@ INSPIREMetadataValidator <- R6Class("INSPIREMetadataValidator", #post metadata XML to INSPIRE web-service self$INFO("Sending metadata file to INSPIRE metadata validation web-service...") - apiKey <- try(private$keyring_backend$get(service = private$keyring_service, username = "geometa_inspire_validator"), silent = TRUE) - if(is(apiKey, "try-error")) apiKey <- NULL + req <- httr::POST( url = sprintf("%s/TestRuns", self$url), httr::add_headers( "User-Agent" = paste("geometa/",as.character(packageVersion("geometa")),sep=""), "Content-Type" = "application/json", - "X-API-key" = apiKey + "X-API-key" = self$getAPIKey() ), body = jsonlite::toJSON(list( label = jsonlite::unbox("Test run for ISO/TC 19139:2007 based INSPIRE metadata records."), diff --git a/R/ISOAbstractObject.R b/R/ISOAbstractObject.R index d87807ad..dd3fbb93 100644 --- a/R/ISOAbstractObject.R +++ b/R/ISOAbstractObject.R @@ -56,7 +56,7 @@ #' \item{\code{decode(xml)}}{ #' Decodes a ISOMetadata* R6 object from XML representation #' } -#' \item{\code{encode(addNS, validate, strict, inspire, inspireApiKey, resetSerialID, setSerialID, encoding)}}{ +#' \item{\code{encode(addNS, validate, strict, inspire, inspireValidator, resetSerialID, setSerialID, encoding)}}{ #' Encodes a ISOMetadata* R6 object to XML representation. By default, namespace #' definition will be added to XML root (\code{addNS = TRUE}), and validation #' of object will be performed (\code{validate = TRUE}) prior to its XML encoding. @@ -69,16 +69,18 @@ #' (recommended value). #' Setting \code{inspire} to TRUE (default FALSE), the metadata will be checked with #' the INSPIRE metadata validator (online web-service provided by INSPIRE). To check -#' metadata with the INSPIRE metadata validator, a user API key is now required, and should -#' be specified with the \code{inspireApiKey}. +#' metadata with the INSPIRE metadata validator, setting an INSPIRE metadata validator +#' is now required, and should be specified with the \code{inspireValidator}. See +#' \code{\link{INSPIREMetadataValidator}} for more details #' } -#' \item{\code{validate(xml, strict, inspire, inspireApiKey)}}{ +#' \item{\code{validate(xml, strict, inspire, inspireValidator)}}{ #' Validates the encoded XML against ISO 19139 XML schemas. If \code{strict} is #' \code{TRUE}, a error will be raised. Default is \code{FALSE}. #' Setting \code{inspire} to\code{TRUE} (default \code{FALSE}), the metadata will be #' checked with the INSPIRE metadata validator (online web-service provided by INSPIRE). -#' To check metadata with the INSPIRE metadata validator, a user API key is now required, -#' and should be specified with the \code{inspireApiKey}. +#' To check metadata with the INSPIRE metadata validator, setting an INSPIRE metadata validator +#' is now required, and should be specified with the \code{inspireValidator}. See +#' \code{\link{INSPIREMetadataValidator}} for more details #' } #' \item{\code{save(file, ...)}}{ #' Saves the current metadata object XML representation to a file. This utility @@ -651,7 +653,7 @@ ISOAbstractObject <- R6Class("ISOAbstractObject", }, #encode - encode = function(addNS = TRUE, validate = TRUE, strict = FALSE, inspire = FALSE, inspireApiKey = NULL, + encode = function(addNS = TRUE, validate = TRUE, strict = FALSE, inspire = FALSE, inspireValidator = NULL, resetSerialID = TRUE, setSerialID = TRUE, encoding = "UTF-8"){ @@ -973,7 +975,7 @@ ISOAbstractObject <- R6Class("ISOAbstractObject", #validation vs. ISO 19139 XML schemas + eventually INSPIRE compliant <- NA if(validate){ - compliant <- self$validate(xml = out, strict = strict, inspire = inspire, inspireApiKey = inspireApiKey) + compliant <- self$validate(xml = out, strict = strict, inspire = inspire, inspireValidator = inspireValidator) } if(self$isDocument()){ if(!inspire){ @@ -1005,7 +1007,7 @@ ISOAbstractObject <- R6Class("ISOAbstractObject", }, #validate - validate = function(xml = NULL, strict = FALSE, inspire = FALSE, inspireApiKey = NULL){ + validate = function(xml = NULL, strict = FALSE, inspire = FALSE, inspireValidator = NULL){ #xml schemaNamespaceId <- NULL @@ -1045,15 +1047,14 @@ ISOAbstractObject <- R6Class("ISOAbstractObject", } if(inspire){ - if(!is.null(inspireApiKey) && nzchar(inspireApiKey)){ - inspireValidator <- INSPIREMetadataValidator$new(apiKey = inspireApiKey) + if(!is.null(inspireValidator) && is(inspireValidator, "INSPIREMetadataValidator")){ inspireReport <- inspireValidator$getValidationReport(obj = self) isValid <- list( ISO = isValid, INSPIRE = inspireReport ) }else{ - self$WARN("No API key specified for INSPIRE Metadata validator, aborting INSPIRE metadata validation!") + self$WARN("No INSPIRE Metadata validator set, aborting INSPIRE metadata validation!") } } diff --git a/man/INSPIREMetadataValidator.Rd b/man/INSPIREMetadataValidator.Rd index d8fd0a49..c81562ad 100644 --- a/man/INSPIREMetadataValidator.Rd +++ b/man/INSPIREMetadataValidator.Rd @@ -16,10 +16,11 @@ INSPIREMetadataValidator \section{Methods}{ \describe{ - \item{\code{new(apiKey)}}{ + \item{\code{new(url, apiKey)}}{ This method is used to instantiate an INSPIRE Metadata validator. To check metadata with the INSPIRE metadata validator, a user API key is now required, - and should be specified with the \code{apiKey}. + and should be specified with the \code{apiKey}. By default, the \code{url} will be + the INSPIRE production service \url{https://inspire.ec.europa.eu/validator/v2} The \code{keyring_backend} can be set to use a different backend for storing the INSPIRE metadata validator API key with \pkg{keyring} (Default value is 'env'). @@ -28,6 +29,9 @@ INSPIREMetadataValidator Upload a XML metadata file to INSPIRE web-service. Method called internally through \code{getValidationReport}. } + \item{\code{getAPIKey()}}{ + Get the API user key + } \item{\code{getValidationReport(obj, file, raw)}}{ Get validation report for a metadata specified either as R object of class \code{ISOMetadata} (from \pkg{geometa} package) or \code{XMLInternalNode} diff --git a/man/ISOAbstractObject.Rd b/man/ISOAbstractObject.Rd index 85e78c4b..d5f4bf0c 100644 --- a/man/ISOAbstractObject.Rd +++ b/man/ISOAbstractObject.Rd @@ -60,7 +60,7 @@ Abstract ISO Metadata class used internally by geometa \item{\code{decode(xml)}}{ Decodes a ISOMetadata* R6 object from XML representation } - \item{\code{encode(addNS, validate, strict, inspire, inspireApiKey, resetSerialID, setSerialID, encoding)}}{ + \item{\code{encode(addNS, validate, strict, inspire, inspireValidator, resetSerialID, setSerialID, encoding)}}{ Encodes a ISOMetadata* R6 object to XML representation. By default, namespace definition will be added to XML root (\code{addNS = TRUE}), and validation of object will be performed (\code{validate = TRUE}) prior to its XML encoding. @@ -73,16 +73,18 @@ Abstract ISO Metadata class used internally by geometa (recommended value). Setting \code{inspire} to TRUE (default FALSE), the metadata will be checked with the INSPIRE metadata validator (online web-service provided by INSPIRE). To check - metadata with the INSPIRE metadata validator, a user API key is now required, and should - be specified with the \code{inspireApiKey}. + metadata with the INSPIRE metadata validator, setting an INSPIRE metadata validator + is now required, and should be specified with the \code{inspireValidator}. See + \code{\link{INSPIREMetadataValidator}} for more details } - \item{\code{validate(xml, strict, inspire, inspireApiKey)}}{ + \item{\code{validate(xml, strict, inspire, inspireValidator)}}{ Validates the encoded XML against ISO 19139 XML schemas. If \code{strict} is \code{TRUE}, a error will be raised. Default is \code{FALSE}. Setting \code{inspire} to\code{TRUE} (default \code{FALSE}), the metadata will be checked with the INSPIRE metadata validator (online web-service provided by INSPIRE). - To check metadata with the INSPIRE metadata validator, a user API key is now required, - and should be specified with the \code{inspireApiKey}. + To check metadata with the INSPIRE metadata validator, setting an INSPIRE metadata validator + is now required, and should be specified with the \code{inspireValidator}. See + \code{\link{INSPIREMetadataValidator}} for more details } \item{\code{save(file, ...)}}{ Saves the current metadata object XML representation to a file. This utility diff --git a/tests/testthat/test_INSPIREMetadataValidator.R b/tests/testthat/test_INSPIREMetadataValidator.R index e28e20bc..468ded45 100644 --- a/tests/testthat/test_INSPIREMetadataValidator.R +++ b/tests/testthat/test_INSPIREMetadataValidator.R @@ -34,10 +34,18 @@ test_that("inspire - metadata validator",{ test_that("inspire - metadata validator 'encode' shortcut",{ testthat::skip_on_cran() - xml <- md$encode(inspire = TRUE, inspireApiKey = Sys.getenv("INSPIRE_API_KEY")) + apiKey <- Sys.getenv("INSPIRE_API_KEY") + if(nzchar(apiKey)){ + inspireValidator <- INSPIREMetadataValidator$new(apiKey = apiKey) + xml <- md$encode(inspire = TRUE, inspireValidator = inspireValidator) + } }) test_that("inspire - metadata validator 'save' shortcut",{ testthat::skip_on_cran() - md$save("my-metadata.xml", inspire = TRUE, inspireApiKey = Sys.getenv("INSPIRE_API_KEY")) + apiKey <- Sys.getenv("INSPIRE_API_KEY") + if(nzchar(apiKey)){ + inspireValidator <- INSPIREMetadataValidator$new(apiKey = apiKey) + md$save("my-metadata.xml", inspire = TRUE, inspireValidator = inspireValidator) + } }) \ No newline at end of file