Skip to content

Commit

Permalink
Merge pull request #143 from eblondel/127-zenodo-invenio-rdm
Browse files Browse the repository at this point in the history
127 zenodo invenio rdm
  • Loading branch information
eblondel authored Mar 11, 2024
2 parents 4636c28 + f1adc48 commit 2159e5f
Show file tree
Hide file tree
Showing 12 changed files with 827 additions and 258 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: zen4R
Version: 0.9
Date: 2023-09-20
Version: 1.0
Date: 2024-03-11
Title: Interface to 'Zenodo' REST API
Authors@R: c(
person("Emmanuel", "Blondel", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0002-5870-5762")),
Expand All @@ -11,7 +11,7 @@ Authors@R: c(
person("Jemma", "Stachelek", role = c("ctb"), comment = c(ORCID = "0000-0002-5924-2464")))
Maintainer: Emmanuel Blondel <[email protected]>
Depends: R (>= 3.3.0), methods
Imports: R6, httr, jsonlite, XML, xml2, keyring, tools, atom4R, utf8
Imports: R6, httr, jsonlite, XML, xml2, keyring, tools, atom4R, utf8, plyr
Suggests: testthat, parallel, knitr, markdown
Description: Provides an Interface to 'Zenodo' (<https://zenodo.org>) REST API,
including management of depositions, attribution of DOIs by 'Zenodo' and
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import(keyring)
import(methods)
import(xml2)
importFrom(R6,R6Class)
importFrom(plyr,rbind.fill)
importFrom(tools,file_path_as_absolute)
importFrom(tools,md5sum)
importFrom(utf8,utf8_encode)
574 changes: 418 additions & 156 deletions R/ZenodoManager.R

Large diffs are not rendered by default.

86 changes: 55 additions & 31 deletions R/ZenodoRecord.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ZenodoRecord <- R6Class("ZenodoRecord",
)
},
fromList = function(obj){
self$access = obj$access
self$conceptdoi = obj$conceptdoi
self$conceptrecid = obj$conceptrecid
self$created = obj$created
Expand All @@ -50,19 +51,24 @@ ZenodoRecord <- R6Class("ZenodoRecord",
)
})
self$id = obj$id
self$recid = obj$recid
self$links = obj$links
self$metadata = obj$metadata
self$modified = obj$modified
self$owner = obj$owner
self$record_id = obj$record_id
self$owners = obj$owners
self$status = obj$status
self$state = obj$state
self$submitted = obj$submitted
self$title = obj$title
self$version = obj$version
self$revision = obj$revision
if(!is.null(obj$stats)) self$stats = data.frame(obj$stats)
}
),
public = list(
#' @field access access policies
access = list(
record = "public",
files = "public"
),
#' @field conceptdoi record Concept DOI (common to all record versions)
conceptdoi = NULL,
#' @field conceptrecid record concept id
Expand All @@ -83,18 +89,18 @@ ZenodoRecord <- R6Class("ZenodoRecord",
metadata = list(),
#' @field modified record modification date
modified = NULL,
#' @field owner record owner
owner = NULL,
#' @field record_id record_id
record_id = NULL,
#' @field owners record owners
owners = NULL,
#' @field recid recid
recid = NULL,
#' @field status record status
status = NULL,
#' @field state record state
state = NULL,
#' @field submitted record submission status
submitted = FALSE,
#' @field title record title
title = NULL,
#' @field version record version
version = NULL,
#' @field revision record revision
revision = NULL,
#' @field stats stats
stats = NULL,

Expand All @@ -104,10 +110,35 @@ ZenodoRecord <- R6Class("ZenodoRecord",
#' or "DEBUG" (for complete curl http calls logs)
initialize = function(obj = NULL, logger = "INFO"){
super$initialize(logger = logger)
self$prereserveDOI(TRUE)
if(!is.null(obj)) private$fromList(obj)
},

#Invenio RDM API new methods
#---------------------------------------------------------------------------
#'@description Set the access policy for record, among values "public" (default) or "restricted"
#'@param access access policy ('public' or 'restricted')
setAccessPolicyRecord = function(access = c("public","resticted")){
self$access$record = access
},

#'@description Set the access policy for files, among values "public" (default) or "restricted"
#'@param access access policy ('public' or 'restricted')
setAccessPolicyFiles = function(access = c("public","resticted")){
self$access$files = access
},

#'@description Set access policy embargo options
#'@param active whether embargo is active or not. Default is \code{FALSE}
#'@param until embargo date, object of class \code{Date}. Default is \code{NULL}. Must be provided if embargo is active
#'@param reason embargo reason, object of class \code{character}. Default is an empty string
setAccessPolicyEmbargo = function(active = FALSE, until = NULL, reason = ""){
if(!is.null(until)) if(!is(until, "Date")) stop("Argument 'until' should be of class 'Date'")
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)}
Expand Down Expand Up @@ -971,18 +1002,18 @@ ZenodoRecord <- R6Class("ZenodoRecord",
#' @return the writen file name (with extension)
exportAs = function(format, filename, append_format = TRUE){
zenodo_url <- self$links$record_html
if(is.null(zenodo_url)) zenodo_url <- self$links$latest_html
if(is.null(zenodo_url)) zenodo_url <- self$links$self_html
if(is.null(zenodo_url)){
stop("Ups, this record seems a draft, can't export metadata until it is published!")
}
metadata_export_url <- switch(format,
"BibTeX" = paste0(zenodo_url,"/export/hx"),
"BibTeX" = paste0(zenodo_url,"/export/bibtex"),
"CSL" = paste0(zenodo_url,"/export/csl"),
"DataCite" = paste0(zenodo_url,"/export/dcite4"),
"DublinCore" = paste0(zenodo_url,"/export/xd"),
"DCAT" = paste0(zenodo_url,"/export/dcat"),
"DataCite" = paste0(zenodo_url,"/export/datacite-xml"),
"DublinCore" = paste0(zenodo_url,"/export/dublincore"),
"DCAT" = paste0(zenodo_url,"/export/dcat-ap"),
"JSON" = paste0(zenodo_url,"/export/json"),
"JSON-LD" = paste0(zenodo_url,"/export/schemaorg_jsonld"),
"JSON-LD" = paste0(zenodo_url,"/export/json-ld"),
"GeoJSON" = paste0(zenodo_url,"/export/geojson"),
"MARCXML" = paste0(zenodo_url,"/export/xm"),
NULL
Expand All @@ -992,19 +1023,12 @@ ZenodoRecord <- R6Class("ZenodoRecord",
}

fileext <- private$getExportFormatExtension(format)

html <- xml2::read_html(metadata_export_url)
reference <- xml2::xml_find_all(html, ".//pre")
reference <- reference[1]
reference <- gsub("<pre.*\">","",reference)
reference <- gsub("</pre>","",reference)
if(fileext %in% c("xml", "rdf")){
reference <- gsub("&lt;", "<", reference)
reference <- gsub("&gt;", ">", reference)
}

destfile <- paste(paste0(filename, ifelse(append_format,paste0("_", format),"")), fileext, sep = ".")
writeChar(reference, destfile, eos = NULL)

req <- httr::GET(
url = metadata_export_url,
httr::write_disk(path = destfile, overwrite = TRUE)
)
return(destfile)
},

Expand Down
13 changes: 8 additions & 5 deletions R/ZenodoRequest.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ ZenodoRequest <- R6Class("ZenodoRequest",
data <- data[!sapply(data, is.null)]
}else if(is(data, "list")){
meta <- data$metadata
if(!is.null(meta$prereserve_doi)) meta$prereserve_doi <- NULL
data <- list(metadata = meta)
if(!is.null(meta)){
if(!is.null(meta$prereserve_doi)) meta$prereserve_doi <- NULL
data <- list(metadata = meta)
}
}

data <- as(toJSON(data, pretty=T, auto_unbox=T), "character")
Expand Down Expand Up @@ -134,12 +136,12 @@ ZenodoRequest <- R6Class("ZenodoRequest",
PUT = function(url, request, data, progress){
req <- paste(url, request, sep="/")

if(regexpr("api/files", req)<0) data <- private$prepareData(data)
if(regexpr("draft/files", req)<0) data <- private$prepareData(data)

#headers
headers <- c(
"User-Agent" = private$agent,
"Content-Type" = if(regexpr("api/files", req)>0) "application/octet-stream" else "application/json",
"Content-Type" = if(regexpr("draft/files", req)>0) "application/octet-stream" else "application/json",
"Authorization" = paste("Bearer",private$token)
)

Expand Down Expand Up @@ -167,7 +169,8 @@ ZenodoRequest <- R6Class("ZenodoRequest",
},

DELETE = function(url, request, data){
req <- paste(url, request, data, sep="/")
req <- paste(url, request, sep="/")
if(!is.null(data)) req <- paste(req, data, sep = "/")
#headers
headers <- c(
"User-Agent" = private$agent,
Expand Down
1 change: 1 addition & 0 deletions R/zen4R.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' @importFrom tools md5sum
#' @import atom4R
#' @importFrom utf8 utf8_encode
#' @importFrom plyr rbind.fill
#'
#' @title Interface to 'Zenodo' REST API
#' @description Provides an Interface to 'Zenodo' (<https://zenodo.org>) REST API,
Expand Down
Loading

0 comments on commit 2159e5f

Please sign in to comment.