Skip to content

Commit

Permalink
#133 method reserveDOI
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed May 14, 2024
1 parent f4b9ad3 commit 8453e9e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 86 deletions.
46 changes: 44 additions & 2 deletions R/ZenodoManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -1095,10 +1095,11 @@ ZenodoManager <- R6Class("ZenodoManager",

#' @description Deposits a record on Zenodo.
#' @param record the record to deposit, object of class \code{ZenodoRecord}
#' @param reserveDOI reserve DOI. By default \code{TRUE}
#' @param publish object of class \code{logical} indicating if record has to be published (default \code{FALSE}).
#' Can be set to \code{TRUE} (to use CAUTIOUSLY, only if you want to publish your record)
#' @return \code{TRUE} if deposited (and eventually published), \code{FALSE} otherwise
depositRecord = function(record, publish = FALSE){
#' @return object of class \code{ZenodoRecord}
depositRecord = function(record, reserveDOI = TRUE, publish = FALSE){
data <- record
type <- ifelse(is.null(record$id), "POST", "PUT")
request <- ifelse(is.null(record$id), "records",
Expand All @@ -1113,6 +1114,18 @@ ZenodoManager <- R6Class("ZenodoManager",
infoMsg = "Successful record deposition"
cli::cli_alert_success(infoMsg)
self$INFO(infoMsg)

if(reserveDOI){
if(is.null(record$pids$doi)){
out <- self$reserveDOI(out)
}else{
warnMsg = sprintf("Existing DOI (%s) for record %s. Aborting DOI reservation!", record$pids$doi$identifier, out$id)
cli::cli_alert_warning(warnMsg)
self$WARN(warnMsg)
}

}

}else{
out <- zenReq$getResponse()
errMsg = sprintf("Error while depositing record: %s", out$message)
Expand All @@ -1132,6 +1145,35 @@ ZenodoManager <- R6Class("ZenodoManager",
return(out)
},

#'@description Reserves a DOI for a deposition (draft record)
#' @param record the record to deposit, object of class \code{ZenodoRecord}
#'@return object of class \code{ZenodoRecord}
reserveDOI = function(record){
request <- sprintf("records/%s/draft/pids/doi", record$id)
zenReq <- ZenodoRequest$new(private$url, "POST", request, data = NULL,
token = self$getToken(),
logger = self$loggerType)
zenReq$execute()
out <- NULL
if(zenReq$getStatus() == 201){
out <- ZenodoRecord$new(obj = zenReq$getResponse())
infoMsg = sprintf("Successful reserved DOI for record %s", record$id)
cli::cli_alert_success(infoMsg)
self$INFO(infoMsg)
}else{
out <- zenReq$getResponse()
errMsg = sprintf("Error while reserving DOI for record %s: %s", record$id, out$message)
cli::cli_alert_danger(errMsg)
self$ERROR(errMsg)
for(error in out$errors){
errMsg = sprintf("Error: %s - %s", error$field, error$message)
cli::cli_alert_danger(errMsg)
self$ERROR(errMsg)
}
}
return(out)
},

#' @description Deposits a record version on Zenodo. For details about the behavior of this function,
#' see \href{https://developers.zenodo.org/#new-version}{https://developers.zenodo.org/#new-version}
#' @param record the record version to deposit, object of class \code{ZenodoRecord}
Expand Down
76 changes: 41 additions & 35 deletions R/ZenodoRecord.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,24 @@ ZenodoRecord <- R6Class("ZenodoRecord",
}
),
public = list(

#' @field created record creation date
created = NULL,
#' @field access access policies
access = list(
record = "public",
files = "public"
),

#v LEGACY (TO ANALYZE)
#' @field conceptdoi record Concept DOI (common to all record versions)
conceptdoi = NULL,
#' @field conceptrecid record concept id
conceptrecid = NULL,
#' @field created record creation date
created = NULL,
#' @field doi record doi
doi = NULL,
#' @field doi_url record doi URL
doi_url = NULL,
#' @field files list of files associated to the record
files = list(),
#' @field id record id
id = NULL,
#' @field links list of links associated to the record
links = list(),
#' @field metadata metadata elements associated to the record
metadata = list(),
#' @field modified record modification date
modified = NULL,
#' @field owners record owners
Expand All @@ -107,6 +102,25 @@ ZenodoRecord <- R6Class("ZenodoRecord",
submitted = FALSE,
#' @field revision record revision
revision = NULL,
#^ LEGACY

#' @field files list of files associated to the record
files = list(),
#' @field id record id
id = NULL,
#' @field links list of links associated to the record
links = list(),
#' @field media_files media files
media_files = list(),
#' @field metadata metadata elements associated to the record
metadata = list(),
#' @field parent parent record
parent = NULL,
#' @field pids pids
pids = list(),

#zen4R specific fields

#' @field stats stats
stats = NULL,

Expand All @@ -119,6 +133,15 @@ ZenodoRecord <- R6Class("ZenodoRecord",
if(!is.null(obj)) private$fromList(obj)
},

#zen4R specific methods
#---------------------------------------------------------------------------

#' @description Get record statistics
#' @return statistics as \code{data.frame}
getStats = function(){
return(self$stats)
},

#Invenio RDM API new methods
#---------------------------------------------------------------------------
#'@description Set the access policy for record, among values "public" (default) or "restricted"
Expand All @@ -142,29 +165,17 @@ ZenodoRecord <- R6Class("ZenodoRecord",
self$access$embargo = list(active = active, until = until, reason = reason)
},

#legacy REST API methods (to be evaluated under Zenodo Invenio RDM migration)
#----------------------------------------------------------------------------

#' @description Set prereserve_doi if \code{TRUE}, \code{FALSE} otherwise to create a record without
#' prereserved DOI by Zenodo. By default, this method will be called to prereserve a DOI assuming
#' the record created doesn't yet handle a DOI. To avoid prereserving a DOI call \code{$prereserveDOI(FALSE)}
#' on your record.
#' @param prereserve whether a DOI has to be pre-reserved by Zenodo
prereserveDOI = function(prereserve){
if(!is(prereserve,"logical")){
stop("The argument should be 'logical' (TRUE/FALSE)")
}
self$metadata$prereserve_doi <- prereserve
},

#' @description Set the DOI. This method can be used if a DOI has been already assigned outside Zenodo.
#' This method will call the method \code{$prereserveDOI(FALSE)}.
#' @param doi DOI to set for the record
setDOI = function(doi){
self$metadata$doi <- doi
self$prereserveDOI(FALSE)
#' @param provider DOI provider
#' @param client DOI client
setDOI = function(doi, provider = NULL, client = NULL){
self$pids = list(doi = list(identifier = doi, provider = provider, client = client))
},

#legacy REST API methods (to be evaluated under Zenodo Invenio RDM migration)
#----------------------------------------------------------------------------

#' @description Get the concept (generic) DOI. The concept DOI is a generic DOI common to all versions
#' of a Zenodo record. When a deposit is unsubmitted, this concept DOI is inherited based
#' on the prereserved DOI of the first record version.
Expand Down Expand Up @@ -231,12 +242,7 @@ ZenodoRecord <- R6Class("ZenodoRecord",

return(versions)
},

#' @description Get record statistics
#' @return statistics as \code{data.frame}
getStats = function(){
return(self$stats)
},


#' @description Set the resource type (mandatory).
#' @param resourceType record resource type
Expand Down
27 changes: 25 additions & 2 deletions man/ZenodoManager.Rd

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

Loading

0 comments on commit 8453e9e

Please sign in to comment.