From 40e5ed0aebaa99d40bcd9e820f9a17270f573114 Mon Sep 17 00:00:00 2001 From: eblondel Date: Thu, 3 Sep 2020 09:21:16 +0200 Subject: [PATCH] improve messaging at connection --- R/ZenodoManager.R | 71 +++++++++++++++++++++++--------------------- man/ZenodoManager.Rd | 2 +- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/R/ZenodoManager.R b/R/ZenodoManager.R index 53c4e63..dd68bf4 100644 --- a/R/ZenodoManager.R +++ b/R/ZenodoManager.R @@ -52,7 +52,7 @@ #' \item{\code{getFunderById(id)}}{ #' Get funder by Id #' } -#' \item{\code{getDepositions(q, size, all_versions, exact)}}{ +#' \item{\code{getDepositions(q, size, all_versions, exact, quiet)}}{ #' Get the list of Zenodo records deposited in your Zenodo workspace. By defaut #' the list of depositions will be returned by page with a size of 10 results per #' page (default size of the Zenodo API). The parameter \code{q} allows to specify @@ -230,13 +230,15 @@ ZenodoManager <- R6Class("ZenodoManager", if(is.null(token)) token <- "" keyring::key_set_with_value(private$keyring_service, username = "zen4R", password = token) if(nzchar(token)){ - zenodo_manager <- self - zenodo_manager$loggerType <- NULL - deps <- zenodo_manager$getDepositions(size = 1) - if(!is.null(deps$status)) if(deps$status == 401){ - errMsg <- "Cannot connect to your Zenodo deposit: Invalid token" - self$ERROR(errMsg) - stop(errMsg) + deps <- self$getDepositions(size = 1, quiet = TRUE) + if(!is.null(deps$status)) { + if(deps$status == 401){ + errMsg <- "Cannot connect to your Zenodo deposit: Invalid token" + self$ERROR(errMsg) + stop(errMsg) + } + }else{ + self$INFO("Successfully connected to Zenodo with user token") } } }, @@ -270,7 +272,7 @@ ZenodoManager <- R6Class("ZenodoManager", return(rec) })) } - self$INFO("Successfuly fetched list of licenses") + self$INFO("Successfully fetched list of licenses") }else{ self$ERROR(sprintf("Error while fetching licenses: %s", out$message)) } @@ -285,7 +287,7 @@ ZenodoManager <- R6Class("ZenodoManager", zenReq$execute() out <- zenReq$getResponse() if(zenReq$getStatus() == 200){ - self$INFO(sprintf("Successfuly fetched license '%s'",id)) + self$INFO(sprintf("Successfully fetched license '%s'",id)) }else{ self$ERROR(sprintf("Error while fetching license '%s': %s", id, out$message)) } @@ -319,7 +321,7 @@ ZenodoManager <- R6Class("ZenodoManager", return(rec) })) } - self$INFO("Successfuly fetched list of communities") + self$INFO("Successfully fetched list of communities") }else{ self$ERROR(sprintf("Error while fetching communities: %s", out$message)) } @@ -334,7 +336,7 @@ ZenodoManager <- R6Class("ZenodoManager", zenReq$execute() out <- zenReq$getResponse() if(zenReq$getStatus() == 200){ - self$INFO(sprintf("Successfuly fetched community '%s'",id)) + self$INFO(sprintf("Successfully fetched community '%s'",id)) }else{ self$ERROR(sprintf("Error while fetching community '%s': %s", id, out$message)) out <- NULL @@ -362,7 +364,7 @@ ZenodoManager <- R6Class("ZenodoManager", while(hasGrants){ out <- c(out, grants) if(!is.null(grants)){ - self$INFO(sprintf("Successfuly fetched list of grants - page %s", page)) + self$INFO(sprintf("Successfully fetched list of grants - page %s", page)) page <- page+1 #next }else{ lastPage <- TRUE @@ -383,7 +385,7 @@ ZenodoManager <- R6Class("ZenodoManager", } if(lastPage) break; } - self$INFO("Successfuly fetched list of grants!") + self$INFO("Successfully fetched list of grants!") }else{ out <- zenReq$getResponse() self$ERROR(sprintf("Error while fetching grants: %s", out$message)) @@ -429,7 +431,7 @@ ZenodoManager <- R6Class("ZenodoManager", zenReq$execute() out <- zenReq$getResponse() if(zenReq$getStatus() == 200){ - self$INFO(sprintf("Successfuly fetched grant '%s'",id)) + self$INFO(sprintf("Successfully fetched grant '%s'",id)) }else{ self$ERROR(sprintf("Error while fetching grant '%s': %s", id, out$message)) out <- NULL @@ -457,7 +459,7 @@ ZenodoManager <- R6Class("ZenodoManager", while(hasFunders){ out <- c(out, funders) if(!is.null(funders)){ - self$INFO(sprintf("Successfuly fetched list of funders - page %s", page)) + self$INFO(sprintf("Successfully fetched list of funders - page %s", page)) page <- page+1 #next }else{ lastPage <- TRUE @@ -478,7 +480,7 @@ ZenodoManager <- R6Class("ZenodoManager", } if(lastPage) break; } - self$INFO("Successfuly fetched list of funders!") + self$INFO("Successfully fetched list of funders!") }else{ out <- zenReq$getResponse() self$ERROR(sprintf("Error while fetching funders: %s", out$message)) @@ -514,7 +516,7 @@ ZenodoManager <- R6Class("ZenodoManager", zenReq$execute() out <- zenReq$getResponse() if(zenReq$getStatus() == 200){ - self$INFO(sprintf("Successfuly fetched funder '%s'",id)) + self$INFO(sprintf("Successfully fetched funder '%s'",id)) }else{ self$ERROR(sprintf("Error while fetching funder '%s': %s", id, out$message)) out <- NULL @@ -526,13 +528,14 @@ ZenodoManager <- R6Class("ZenodoManager", #------------------------------------------------------------------------------------------ #getDepositions - getDepositions = function(q = "", size = 10, all_versions = FALSE, exact = TRUE){ + getDepositions = function(q = "", size = 10, all_versions = FALSE, exact = TRUE, + quiet = FALSE){ page <- 1 req <- sprintf("deposit/depositions?q=%s&size=%s&page=%s", q, size, page) if(all_versions) req <- paste0(req, "&all_versions=1") zenReq <- ZenodoRequest$new(private$url, "GET", req, token = self$getToken(), - logger = self$loggerType) + logger = if(quiet) NULL else self$loggerType) zenReq$execute() out <- NULL if(zenReq$getStatus() == 200){ @@ -540,7 +543,7 @@ ZenodoManager <- R6Class("ZenodoManager", hasRecords <- length(resp)>0 while(hasRecords){ out <- c(out, lapply(resp, ZenodoRecord$new)) - self$INFO(sprintf("Successfuly fetched list of depositions - page %s", page)) + if(!quiet) self$INFO(sprintf("Successfully fetched list of depositions - page %s", page)) if(exact){ hasRecords <- FALSE @@ -551,16 +554,16 @@ ZenodoManager <- R6Class("ZenodoManager", if(all_versions) nextreq <- paste0(nextreq, "&all_versions=1") zenReq <- ZenodoRequest$new(private$url, "GET", nextreq, token = self$getToken(), - logger = self$loggerType) + logger = if(quiet) NULL else self$loggerType) zenReq$execute() resp <- zenReq$getResponse() hasRecords <- length(resp)>0 } } - self$INFO("Successfuly fetched list of depositions!") + if(!quiet) self$INFO("Successfully fetched list of depositions!") }else{ out <- zenReq$getResponse() - self$ERROR(sprintf("Error while fetching depositions: %s", out$message)) + if(!quiet) self$ERROR(sprintf("Error while fetching depositions: %s", out$message)) for(error in out$errors){ self$ERROR(sprintf("Error: %s - %s", error$field, error$message)) } @@ -575,7 +578,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$conceptdoi == conceptdoi){ - self$INFO(sprintf("Successfuly fetched record for concept DOI '%s'!", conceptdoi)) + self$INFO(sprintf("Successfully fetched record for concept DOI '%s'!", conceptdoi)) }else{ result <- NULL } @@ -603,7 +606,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$doi == doi){ - self$INFO(sprintf("Successfuly fetched record for DOI '%s'!",doi)) + self$INFO(sprintf("Successfully fetched record for DOI '%s'!",doi)) }else{ result <- NULL } @@ -629,7 +632,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$id == recid){ - self$INFO(sprintf("Successfuly fetched record for id '%s'!",recid)) + self$INFO(sprintf("Successfully fetched record for id '%s'!",recid)) }else{ result <- NULL } @@ -647,7 +650,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$conceptrecid == conceptrecid){ - self$INFO(sprintf("Successfuly fetched record for concept id '%s'!",conceptrecid)) + self$INFO(sprintf("Successfully fetched record for concept id '%s'!",conceptrecid)) }else{ result <- NULL } @@ -919,7 +922,7 @@ ZenodoManager <- R6Class("ZenodoManager", hasRecords <- length(resp)>0 while(hasRecords){ out <- c(out, lapply(resp, ZenodoRecord$new)) - self$INFO(sprintf("Successfuly fetched list of published records - page %s", page)) + self$INFO(sprintf("Successfully fetched list of published records - page %s", page)) if(exact){ hasRecords <- FALSE @@ -936,7 +939,7 @@ ZenodoManager <- R6Class("ZenodoManager", hasRecords <- length(resp)>0 } } - self$INFO("Successfuly fetched list of published records!") + self$INFO("Successfully fetched list of published records!") }else{ out <- zenReq$getResponse() self$ERROR(sprintf("Error while fetching published records: %s", out$message)) @@ -957,7 +960,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$conceptdoi == conceptdoi){ - self$INFO(sprintf("Successfuly fetched published record for concept DOI '%s'!", conceptdoi)) + self$INFO(sprintf("Successfully fetched published record for concept DOI '%s'!", conceptdoi)) }else{ result <- NULL } @@ -988,7 +991,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$doi == doi){ - self$INFO(sprintf("Successfuly fetched record for DOI '%s'!",doi)) + self$INFO(sprintf("Successfully fetched record for DOI '%s'!",doi)) }else{ result <- NULL } @@ -1014,7 +1017,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$id == recid){ - self$INFO(sprintf("Successfuly fetched record for id '%s'!",recid)) + self$INFO(sprintf("Successfully fetched record for id '%s'!",recid)) }else{ result <- NULL } @@ -1032,7 +1035,7 @@ ZenodoManager <- R6Class("ZenodoManager", if(length(result)>0){ result <- result[[1]] if(result$conceptrecid == conceptrecid){ - self$INFO(sprintf("Successfuly fetched record for concept id '%s'!",conceptrecid)) + self$INFO(sprintf("Successfully fetched record for concept id '%s'!",conceptrecid)) }else{ result <- NULL } diff --git a/man/ZenodoManager.Rd b/man/ZenodoManager.Rd index 9f025b1..4e41258 100644 --- a/man/ZenodoManager.Rd +++ b/man/ZenodoManager.Rd @@ -65,7 +65,7 @@ Main user class to be used with \pkg{zen4R} \item{\code{getFunderById(id)}}{ Get funder by Id } - \item{\code{getDepositions(q, size, all_versions, exact)}}{ + \item{\code{getDepositions(q, size, all_versions, exact, quiet)}}{ Get the list of Zenodo records deposited in your Zenodo workspace. By defaut the list of depositions will be returned by page with a size of 10 results per page (default size of the Zenodo API). The parameter \code{q} allows to specify