From 06ac68f8187c81fdf6bd3200cdc46323e628ebb9 Mon Sep 17 00:00:00 2001 From: eblondel Date: Wed, 22 May 2024 15:09:43 +0200 Subject: [PATCH] support #152 --- R/ZenodoManager.R | 79 +++++++++++++++++++++++++++++++++++++++++++- R/ZenodoRecord.R | 53 ----------------------------- R/ZenodoRequest.R | 6 ++-- man/ZenodoManager.Rd | 31 ++++++++++++++++- man/ZenodoRecord.Rd | 68 -------------------------------------- 5 files changed, 111 insertions(+), 126 deletions(-) diff --git a/R/ZenodoManager.R b/R/ZenodoManager.R index 0c961c3..c729b3d 100644 --- a/R/ZenodoManager.R +++ b/R/ZenodoManager.R @@ -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) @@ -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) #------------------------------------------------------------------------------------------ diff --git a/R/ZenodoRecord.R b/R/ZenodoRecord.R index 5fb6c3c..246f716 100644 --- a/R/ZenodoRecord.R +++ b/R/ZenodoRecord.R @@ -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 diff --git a/R/ZenodoRequest.R b/R/ZenodoRequest.R index 6c5ce00..dab4cf9 100644 --- a/R/ZenodoRequest.R +++ b/R/ZenodoRequest.R @@ -94,7 +94,7 @@ 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" @@ -102,7 +102,7 @@ ZenodoRequest <- R6Class("ZenodoRequest", data <- list(file = file, filename = data) }else{ contentType <- "application/json" - accept = "application/vnd.inveniordm.v1+json" + accept = accept data <- private$prepareData(data) } @@ -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) ) diff --git a/man/ZenodoManager.Rd b/man/ZenodoManager.Rd index d208af0..0c611af 100644 --- a/man/ZenodoManager.Rd +++ b/man/ZenodoManager.Rd @@ -41,7 +41,6 @@ Main user class to be used with \pkg{zen4R} 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) @@ -100,6 +99,7 @@ Emmanuel Blondel \item \href{#method-ZenodoManager-getResourceTypeById}{\code{ZenodoManager$getResourceTypeById()}} \item \href{#method-ZenodoManager-getCommunities}{\code{ZenodoManager$getCommunities()}} \item \href{#method-ZenodoManager-getCommunityById}{\code{ZenodoManager$getCommunityById()}} +\item \href{#method-ZenodoManager-submitRecordToCommunities}{\code{ZenodoManager$submitRecordToCommunities()}} \item \href{#method-ZenodoManager-getGrants}{\code{ZenodoManager$getGrants()}} \item \href{#method-ZenodoManager-getAwards}{\code{ZenodoManager$getAwards()}} \item \href{#method-ZenodoManager-getGrantsByName}{\code{ZenodoManager$getGrantsByName()}} @@ -376,6 +376,35 @@ the community } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-ZenodoManager-submitRecordToCommunities}{}}} +\subsection{Method \code{submitRecordToCommunities()}}{ +Submit a record to one or more community +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ZenodoManager$submitRecordToCommunities( + record, + communities = list(), + message = NULL +)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{record}}{an object of class \link{ZenodoRecord}} + +\item{\code{communities}}{communities to which the record will be submitted} + +\item{\code{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} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +a submission object of class \code{list}, or NULL if nothing was submitted +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-ZenodoManager-getGrants}{}}} \subsection{Method \code{getGrants()}}{ diff --git a/man/ZenodoRecord.Rd b/man/ZenodoRecord.Rd index 4641852..f0ad963 100644 --- a/man/ZenodoRecord.Rd +++ b/man/ZenodoRecord.Rd @@ -126,9 +126,6 @@ Emmanuel Blondel \item \href{#method-ZenodoRecord-removeSubject}{\code{ZenodoRecord$removeSubject()}} \item \href{#method-ZenodoRecord-removeKeyword}{\code{ZenodoRecord$removeKeyword()}} \item \href{#method-ZenodoRecord-setNotes}{\code{ZenodoRecord$setNotes()}} -\item \href{#method-ZenodoRecord-setCommunities}{\code{ZenodoRecord$setCommunities()}} -\item \href{#method-ZenodoRecord-addCommunity}{\code{ZenodoRecord$addCommunity()}} -\item \href{#method-ZenodoRecord-removeCommunity}{\code{ZenodoRecord$removeCommunity()}} \item \href{#method-ZenodoRecord-setGrants}{\code{ZenodoRecord$setGrants()}} \item \href{#method-ZenodoRecord-addGrant}{\code{ZenodoRecord$addGrant()}} \item \href{#method-ZenodoRecord-removeGrant}{\code{ZenodoRecord$removeGrant()}} @@ -1433,71 +1430,6 @@ Set notes. HTML is not allowed } } \if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ZenodoRecord-setCommunities}{}}} -\subsection{Method \code{setCommunities()}}{ -Set a vector of character strings identifying communities -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{ZenodoRecord$setCommunities(communities, sandbox = FALSE)}\if{html}{\out{
}} -} - -\subsection{Arguments}{ -\if{html}{\out{
}} -\describe{ -\item{\code{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.} - -\item{\code{sandbox}}{Use the Zenodo sandbox infrastructure as basis to control available communities. Default is \code{FALSE}} -} -\if{html}{\out{
}} -} -} -\if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ZenodoRecord-addCommunity}{}}} -\subsection{Method \code{addCommunity()}}{ -Adds a community to the record metadata. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{ZenodoRecord$addCommunity(community, sandbox = FALSE)}\if{html}{\out{
}} -} - -\subsection{Arguments}{ -\if{html}{\out{
}} -\describe{ -\item{\code{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()}.} - -\item{\code{sandbox}}{Use the Zenodo sandbox infrastructure as basis to control available communities. Default is \code{FALSE}} -} -\if{html}{\out{
}} -} -\subsection{Returns}{ -\code{TRUE} if added, \code{FALSE} otherwise -} -} -\if{html}{\out{
}} -\if{html}{\out{}} -\if{latex}{\out{\hypertarget{method-ZenodoRecord-removeCommunity}{}}} -\subsection{Method \code{removeCommunity()}}{ -Removes a community from the record metadata. -\subsection{Usage}{ -\if{html}{\out{
}}\preformatted{ZenodoRecord$removeCommunity(community)}\if{html}{\out{
}} -} - -\subsection{Arguments}{ -\if{html}{\out{
}} -\describe{ -\item{\code{community}}{community to remove. The community should be set with the Zenodo id of the community.} -} -\if{html}{\out{
}} -} -\subsection{Returns}{ -\code{TRUE} if removed, \code{FALSE} otherwise -} -} -\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-ZenodoRecord-setGrants}{}}} \subsection{Method \code{setGrants()}}{