Skip to content

Commit

Permalink
improve messaging at connection
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Sep 3, 2020
1 parent 0c90669 commit 40e5ed0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
71 changes: 37 additions & 34 deletions R/ZenodoManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}
}
},
Expand Down Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand All @@ -526,21 +528,22 @@ 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){
resp <- zenReq$getResponse()
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
Expand All @@ -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))
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion man/ZenodoManager.Rd

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

0 comments on commit 40e5ed0

Please sign in to comment.