Skip to content

Commit

Permalink
support #152
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed May 22, 2024
1 parent 948a221 commit 06ac68f
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 126 deletions.
79 changes: 78 additions & 1 deletion R/ZenodoManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#' myrec$setLicense("mit")
#' myrec$setAccessRight("open")
#' myrec$setDOI("mydoi") #use this method if your DOI has been assigned elsewhere, outside Zenodo
#' myrec$addCommunity("ecfunded")
#'
#' #deposit the record
#' myrec <- ZENODO$depositRecord(myrec)
Expand Down Expand Up @@ -444,6 +443,84 @@ ZenodoManager <- R6Class("ZenodoManager",
return(out)
},

#'@description Submit a record to one or more community
#'@param record an object of class \link{ZenodoRecord}
#'@param communities communities to which the record will be submitted
#'@param message message to send to the community curator(s), either a text or a named list
#'for each community in case a community-specific message should be sent
#'@return a submission object of class \code{list}, or NULL if nothing was submitted
submitRecordToCommunities = function(record, communities = list(), message = NULL){
out = NULL
if(length(communities)==0){
cli::cli_alert_warning(warnMsg)
self$WARN(warnMsg)
return(out)
}
if(!is.null(message)) if(is.list(message)){
if(!all(communities %in% names(message))){
errMsg = paste0("Message list is inconsistent with the list of communities provided.",
"Please verify that the message is a list named with community names,",
"e.g. ZENODO$dsubmitRecordToCommunities(record, communities = c('com1','com2'), message = list(com1 = 'message to com1', com2 = 'msg to com2'))")
cli::cli_alert_danger(errMsg)
self$ERROR(errMsg)
stop(errMsg)
}
}
coms = lapply(communities, function(x){self$getCommunityById(x)})
if(any(sapply(coms, is.null))){
unk_coms = communities[sapply(coms, is.null)]
warnMsg = sprintf("Communities [%s] do not exist in Zenodo, they will be ignored!", paste(unk_coms, collapse=","))
cli::cli_alert_warning(warnMsg)
self$WARN(warnMsg)
}
#check if community exists
existing_coms = coms[!sapply(coms, is.null)]
existing_com_names = communities[!sapply(coms, is.null)]
if(length(existing_coms)==0){
warnMsg = "No existing community specified! Aborting record submission to community"
cli::cli_alert_warning(warnMsg)
self$WARN(warnMsg)
return(NULL)
}
coms_payload = list(
communities = lapply(existing_coms, function(x){
com_payload = list(
id = x$id
)
if(!is.null(message)){
if(is.character(message)){
com_payload$comment = list(payload = list(content = message, format = "html"))
}else if(is.list(message)){
if(x$slug %in% names(message)){
com_payload$comment = list(payload = list(content = message[[x$slug]], format = "html"))
}
}
}
return(com_payload)
})
)
zenReq <- ZenodoRequest$new(private$url, "POST", sprintf("records/%s/communities",record$id),
accept = "application/json", data = coms_payload,
token= self$getToken(),
logger = self$loggerType)
zenReq$execute()
out <- zenReq$getResponse()
if(zenReq$getStatus() == 200){
infoMsg = sprintf("Successfully submitted request to associate record %s to communities [%s]",
record$id, paste0(existing_com_names, collapse=","))
cli::cli_alert_success(infoMsg)
self$INFO(infoMsg)
}else{
errMsg = sprintf("Error while submitting record %s to communities [%s]:",
record$id, paste0(existing_com_names, collapse=","))
print(out)
cli::cli_alert_danger(errMsg)
self$ERROR(errMsg)
out <- NULL
}
return(out)
},

#Special vocabulary/Awards (former Grants)
#------------------------------------------------------------------------------------------

Expand Down
53 changes: 0 additions & 53 deletions R/ZenodoRecord.R
Original file line number Diff line number Diff line change
Expand Up @@ -977,59 +977,6 @@ ZenodoRecord <- R6Class("ZenodoRecord",
self$metadata$notes <- notes
},

#' @description Set a vector of character strings identifying communities
#' @param communities a vector or list of communities. Values should among known communities. The list of communities can
#' fetched with the \code{ZenodoManager} and the function \code{$getCommunities()}. Each community should be set with
#' the Zenodo id of the community. If not recognized by Zenodo, the function will return an error.
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available communities. Default is \code{FALSE}
setCommunities = function(communities, sandbox = FALSE){
if(is.null(self$metadata$communities)) self$metadata$communities <- list()
for(community in communities){
self$addCommunity(community, sandbox = sandbox)
}
},

#' @description Adds a community to the record metadata.
#' @param community community to add. The community should be set with the Zenodo id of the community.
#' If not recognized by Zenodo, the function will return an error. The list of communities can fetched
#' with the \code{ZenodoManager} and the function \code{$getCommunities()}.
#' @param sandbox Use the Zenodo sandbox infrastructure as basis to control available communities. Default is \code{FALSE}
#' @return \code{TRUE} if added, \code{FALSE} otherwise
addCommunity = function(community, sandbox = FALSE){
added <- FALSE
zen <- ZenodoManager$new(sandbox = sandbox)
if(is.null(self$metadata$communities)) self$metadata$communities <- list()
if(!(community %in% sapply(self$metadata$communities, function(x){x$identifier}))){
zen_community <- zen$getCommunityById(community)
if(is.null(zen_community)){
errorMsg <- sprintf("Community with id '%s' doesn't exist in Zenodo", community)
self$ERROR(errorMsg)
stop(errorMsg)
}
self$metadata$communities[[length(self$metadata$communities)+1]] <- list(identifier = community)
added <- TRUE
}
return(added)
},

#' @description Removes a community from the record metadata.
#' @param community community to remove. The community should be set with the Zenodo id of the community.
#' @return \code{TRUE} if removed, \code{FALSE} otherwise
removeCommunity = function(community){
removed <- FALSE
if(!is.null(self$metadata$communities)){
for(i in 1:length(self$metadata$communities)){
com <- self$metadata$communities[[i]]
if(com == community){
self$metadata$communities[[i]] <- NULL
removed <- TRUE
break;
}
}
}
return(removed)
},

#' @description Set a vector of character strings identifying grants
#' @param grants a vector or list of grants Values should among known grants The list of grants can
#' fetched with the \code{ZenodoManager} and the function \code{$getGrants()}. Each grant should be set with
Expand Down
6 changes: 3 additions & 3 deletions R/ZenodoRequest.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ ZenodoRequest <- R6Class("ZenodoRequest",
return(response)
},

POST = function(url, request, data, file = NULL, progress){
POST = function(url, request, data, file = NULL, progress, accept = "application/vnd.inveniordm.v1+json"){
req <- paste(url, request, sep="/")
if(!is.null(file)){
contentType <- "multipart/form-data"
accept <- "application/json"
data <- list(file = file, filename = data)
}else{
contentType <- "application/json"
accept = "application/vnd.inveniordm.v1+json"
accept = accept
data <- private$prepareData(data)
}

Expand Down Expand Up @@ -235,7 +235,7 @@ ZenodoRequest <- R6Class("ZenodoRequest",
req <- switch(private$type,
"GET" = private$GET(private$url, private$request, private$progress, accept = private$accept),
"GET_WITH_CURL" = private$GET(private$url, private$request, private$progress, use_curl = TRUE, accept = private$accept),
"POST" = private$POST(private$url, private$request, private$data, private$file, private$progress),
"POST" = private$POST(private$url, private$request, private$data, private$file, private$progress, accept = private$accept),
"PUT" = private$PUT(private$url, private$request, private$data, private$progress),
"DELETE" = private$DELETE(private$url, private$request, private$data)
)
Expand Down
31 changes: 30 additions & 1 deletion man/ZenodoManager.Rd

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

68 changes: 0 additions & 68 deletions man/ZenodoRecord.Rd

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

0 comments on commit 06ac68f

Please sign in to comment.