Skip to content

Commit

Permalink
#184 inspireValidator argument
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Jan 24, 2022
1 parent b9332c4 commit 4aa37b5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
30 changes: 19 additions & 11 deletions R/INSPIREMetadataValidator.R
Original file line number Diff line number Diff line change
Expand Up @@ -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').
Expand All @@ -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}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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){

Expand Down Expand Up @@ -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."),
Expand Down
25 changes: 13 additions & 12 deletions R/ISOAbstractObject.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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"){

Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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!")
}
}

Expand Down
8 changes: 6 additions & 2 deletions man/INSPIREMetadataValidator.Rd

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

14 changes: 8 additions & 6 deletions man/ISOAbstractObject.Rd

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

12 changes: 10 additions & 2 deletions tests/testthat/test_INSPIREMetadataValidator.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

0 comments on commit 4aa37b5

Please sign in to comment.