Skip to content

Commit

Permalink
support #157
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed May 22, 2024
1 parent 0c27b6c commit d225fb3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
55 changes: 53 additions & 2 deletions R/ZenodoManager.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions R/ZenodoRequest.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions man/ZenodoManager.Rd

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

4 changes: 3 additions & 1 deletion tests/testthat/test_communities.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d225fb3

Please sign in to comment.