diff --git a/NEWS.md b/NEWS.md index 19f3155..bd9c05a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -27,6 +27,7 @@ * [#148](https://github.com/eblondel/zen4R/issues/148) Zenodo to InvenioRDM - Support publisher * [#152](https://github.com/eblondel/zen4R/issues/152) Zenodo To InvenioRDM - New method to associate a record to a community * [#153](https://github.com/eblondel/zen4R/issues/153) Support API 'requests' methods +* [#157](https://github.com/eblondel/zen4R/issues/157) Support method to delete records from communities ## [zen4R 0.9](https://github.com/eblondel/zen4R) | [![CRAN_Status_Badge](https://img.shields.io/badge/CRAN-published-blue.svg)](https://cran.r-project.org/package=zen4R) diff --git a/R/ZenodoManager.R b/R/ZenodoManager.R index aac6726..76c3e6d 100644 --- a/R/ZenodoManager.R +++ b/R/ZenodoManager.R @@ -521,6 +521,57 @@ ZenodoManager <- R6Class("ZenodoManager", return(out) }, + #'@description Remove a record from one or more community + #'@param record an object of class \link{ZenodoRecord} + #'@param communities communities to which the record will be submitted + #'@return \code{TRUE} if removed, \code{FALSE} otherwise + removeRecordFromCommunities = function(record, communities = list()){ + + 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 removal from community" + cli::cli_alert_warning(warnMsg) + self$WARN(warnMsg) + return(NULL) + } + + payload = list( + communities = lapply(existing_coms, function(x){ + list(id = x$id) + }) + ) + zenReq <- ZenodoRequest$new(private$url, "DELETE", sprintf("records/%s/communities",record$id), + data = payload, + token= self$getToken(), + logger = self$loggerType) + zenReq$execute() + out <- zenReq$getResponse() + if(zenReq$getStatus() == 200){ + infoMsg = sprintf("Successful removed record %s from communities [%s]", + record$id, paste0(existing_com_names, collapse=",")) + cli::cli_alert_success(infoMsg) + self$INFO(infoMsg) + out = TRUE + }else{ + errMsg = sprintf("Error while removing record %s from communities [%s]:", + record$id, paste0(existing_com_names, collapse=",")) + print(out) + cli::cli_alert_danger(errMsg) + self$ERROR(errMsg) + out <- FALSE + } + return(out) + }, + #Special vocabulary/Awards (former Grants) #------------------------------------------------------------------------------------------ @@ -1740,8 +1791,8 @@ ZenodoManager <- R6Class("ZenodoManager", #' @param recordId ID of the record #' @param filename name of the file to be deleted deleteFile = function(recordId, filename){ - zenReq <- ZenodoRequest$new(private$url, "DELETE", sprintf("records/%s/draft/files", recordId), - data = filename, token = self$getToken(), + zenReq <- ZenodoRequest$new(private$url, "DELETE", sprintf("records/%s/draft/files/%s", recordId, filename), + token = self$getToken(), logger = self$loggerType) zenReq$execute() out <- FALSE diff --git a/R/ZenodoRequest.R b/R/ZenodoRequest.R index dab4cf9..d8e3703 100644 --- a/R/ZenodoRequest.R +++ b/R/ZenodoRequest.R @@ -177,21 +177,26 @@ ZenodoRequest <- R6Class("ZenodoRequest", DELETE = function(url, request, data){ req <- paste(url, request, sep="/") - if(!is.null(data)) req <- paste(req, data, sep = "/") #headers headers <- c( "User-Agent" = private$agent, - "Authorization" = paste("Bearer",private$token) + "Authorization" = paste("Bearer",private$token), + "Content-Type" = "application/json" ) if(self$verbose.debug){ + print(data) r <- with_verbose(httr::DELETE( url = req, - add_headers(headers) + add_headers(headers), + encode = "json", + body = data )) }else{ r <- httr::DELETE( url = req, - add_headers(headers) + add_headers(headers), + encode = "json", + body = data ) } responseContent <- content(r, type = "application/json", encoding = "UTF-8") diff --git a/man/ZenodoManager.Rd b/man/ZenodoManager.Rd index 3d59be2..521f37d 100644 --- a/man/ZenodoManager.Rd +++ b/man/ZenodoManager.Rd @@ -100,6 +100,7 @@ Emmanuel Blondel \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-removeRecordFromCommunities}{\code{ZenodoManager$removeRecordFromCommunities()}} \item \href{#method-ZenodoManager-getGrants}{\code{ZenodoManager$getGrants()}} \item \href{#method-ZenodoManager-getAwards}{\code{ZenodoManager$getAwards()}} \item \href{#method-ZenodoManager-getGrantsByName}{\code{ZenodoManager$getGrantsByName()}} @@ -410,6 +411,28 @@ 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-removeRecordFromCommunities}{}}} +\subsection{Method \code{removeRecordFromCommunities()}}{ +Remove a record from one or more community +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{ZenodoManager$removeRecordFromCommunities(record, communities = list())}\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} +} +\if{html}{\out{
}} +} +\subsection{Returns}{ +\code{TRUE} if removed, \code{FALSE} otherwise +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-ZenodoManager-getGrants}{}}} \subsection{Method \code{getGrants()}}{ diff --git a/tests/testthat/test_communities.R b/tests/testthat/test_communities.R index 1429baa..3fece06 100644 --- a/tests/testthat/test_communities.R +++ b/tests/testthat/test_communities.R @@ -27,7 +27,9 @@ test_that("community is retrieved by id",{ }) test_that("record is submitted to a community, with cancel and review actions (decline, accept) performed",{ - rec <- ZENODO$getDepositionByDOI("10.5072/zenodo.54894") + rec = ZENODO$getDepositionByDOI("10.5072/zenodo.54894") + removed = ZENODO$removeRecordFromCommunities(record = rec, communities = "openfair") + expect_true(removed) req = ZENODO$submitRecordToCommunities(record = rec, communities = "openfair") #cancel request (for user that submitted the record) canceled = ZENODO$cancelRequest(req$processed[[1]]$request_id)