diff --git a/DESCRIPTION b/DESCRIPTION index 15e9a1b..bb5e636 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: objr Title: Wrapper for Objective APIs -Version: 0.0.0.9002 +Version: 0.0.0.9003 Authors@R: c( person("Scottish Government", , , "statistics.enquiries@gov.scot", role = "cph"), person("Alice", "Hannah", , "alice.hannah@gov.scot", c("aut", "cre")) diff --git a/NAMESPACE b/NAMESPACE index f71a00c..8974032 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(allow_bypass_2fa) export(asset_info) export(comments) export(create_folder) +export(delete_asset) export(download_file) export(my_user_id) export(my_workspaces) diff --git a/R/assets.R b/R/assets.R index 3d44421..270d013 100644 --- a/R/assets.R +++ b/R/assets.R @@ -86,3 +86,30 @@ asset_info <- function(asset_uuid, response[c("uuid", "name", "type", "extension", "description")] } + + +#' Delete an asset +#' +#' @param asset_uuid UUID of asset +#' @inheritParams objr +#' +#' @export + +delete_asset <- function(asset_uuid, + use_proxy = FALSE) { + + response <- objr( + endpoint = "assets", + method = "DELETE", + url_path = list(asset_uuid), + use_proxy = use_proxy + ) |> + httr2::resp_body_json() + + if(tolower(response$status) == "complete") { + cli::cli_alert_success("Asset deleted: {asset_uuid}.") + } + + invisible(response) + +} diff --git a/_pkgdown.yml b/_pkgdown.yml index fd59a8d..aa05005 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -65,6 +65,7 @@ reference: - download_file - upload_file - new_version + - delete_asset - subtitle: Comments desc: View comments and create new threads and replies in workspaces diff --git a/man/delete_asset.Rd b/man/delete_asset.Rd new file mode 100644 index 0000000..01cfc62 --- /dev/null +++ b/man/delete_asset.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/assets.R +\name{delete_asset} +\alias{delete_asset} +\title{Delete an asset} +\usage{ +delete_asset(asset_uuid, use_proxy = FALSE) +} +\arguments{ +\item{asset_uuid}{UUID of asset} + +\item{use_proxy}{Logical to indicate whether to use proxy} +} +\description{ +Delete an asset +} diff --git a/tests/testthat/api/assets/test_asset_uuid-DELETE.R b/tests/testthat/api/assets/test_asset_uuid-DELETE.R new file mode 100644 index 0000000..7745081 --- /dev/null +++ b/tests/testthat/api/assets/test_asset_uuid-DELETE.R @@ -0,0 +1,14 @@ +structure(list(method = "POST", url = "https://api/assets/test_asset_uuid", + status_code = 200L, headers = structure(list( + Date = "Tue, 09 Apr 2024 13:20:11 GMT", + `Content-Type` = "application/json", `Transfer-Encoding` = "chunked", + Connection = "keep-alive", `X-CONNECT-MDC` = "apisyjgCiSS", + `X-Frame-Options` = "deny", `X-XSS-Protection` = "1; mode=block", + `Cache-Control` = "no-cache, no-store", Expires = "0", + Pragma = "no-cache", `Strict-Transport-Security` = "max-age=31536000 ; includeSubDomains", + `Content-Security-Policy` = "script-src 'self' 'unsafe-inline'", + `X-Content-Type-Options` = "nosniff", `Referrer-Policy` = "strict-origin-when-cross-origin", + `Feature-Policy` = "vibrate 'none'; geolocation 'none'", + Authorization = "REDACTED", `Set-Cookie` = "REDACTED"), class = "httr2_headers"), + body = charToRaw("{\"model\":[\"Asset\"],\"uuid\":[\"test_asset_uuid\"],\"status\":[\"COMPLETE\"]}"), + cache = new.env(parent = emptyenv())), class = "httr2_response") diff --git a/tests/testthat/test-assets.R b/tests/testthat/test-assets.R index 079b3a1..3c9aeff 100644 --- a/tests/testthat/test-assets.R +++ b/tests/testthat/test-assets.R @@ -83,3 +83,39 @@ with_mock_api({ }) }) + + +# delete_asset ---- + +without_internet({ + + test_that("Valid request", { + + expect_DELETE( + delete_asset(asset_uuid = "test_asset_uuid"), + paste0("https://secure.objectiveconnect.co.uk/publicapi/1/assets/", + "test_asset_uuid") + ) + + }) + +}) + +with_mock_api({ + + test_that("Function returns invisible", { + + expect_invisible( + suppressMessages(delete_asset(asset_uuid = "test_asset_uuid")) + ) + + }) + + test_that("Function returns success message", { + + expect_message(delete_asset(asset_uuid = "test_asset_uuid")) + + }) + +}) +