diff --git a/NEWS.md b/NEWS.md index 424e952..6fb9e04 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # arcgislayers 0.1.0 (unreleased) +- Add `truncate_layer()` to support truncate and append workflow - Add support for opening `MapServers` - `arc_open()` with a layer that does not support `Query` sets the `n` attribute to`NA` - Print method will show something like `>` diff --git a/R/truncate.R b/R/truncate.R new file mode 100644 index 0000000..c6c0cf0 --- /dev/null +++ b/R/truncate.R @@ -0,0 +1,84 @@ +#' Truncate a Feature Layer +#' +#' Removes all features in a Feature Layer or Table and resets the object ID +#' counter. Truncating a Feature Layer does not change the schema of the data +#' (does not add, remove, or alter existing database columns, constraints, +#' or indexes). +#' +#' @inheritParams arc_select +#' @param async default `FALSE`. It is recommended to set `TRUE` for +#' larger datasets. +#' @param attachment_only default `FALSE`. Deletes all the attachments for this +#' layer. None of the layer features will be deleted when `TRUE`. +#' @references [ArcGIS Developers Rest API Doc](https://developers.arcgis.com/rest/services-reference/online/truncate-feature-layer-.htm) +#' +#' @returns a named list with the name "success" and a value of `TRUE` or `FALSE` +#' +#' @examples +#' \donttest{ +#' if (interactive()) { +#' +#' # authorize using code flow +#' set_auth_token(auth_code()) +#' +#' # create a FeatureLayer object +#' flayer <- arc_open("your-feature-layer-url") +#' +#' # truncate it +#' truncate_layer(flayer) +#' } +#' } +truncate_layer <- function( + x, + async = FALSE, + attachment_only = FALSE, + token = Sys.getenv("ARCGIS_TOKEN") +) { + + # check to see if it is a compatible class + obj_check_layer(x) + + # ensure that it supports truncate + if (!x[["supportsTruncate"]]) { + obj <- rlang::caller_arg(x) + cli::cli_abort( + "{.arg {obj}} does not support the {.code truncate } operation" + ) + } + + # extract url and its components + furl <- x[["url"]] + parts <- httr2::url_parse(furl) + + # replace /services/ with /admin/services/ + parts[["path"]] <- sub("\\/services/", "/admin/services/", parts[["path"]]) + + # rebuild the url + burl <- httr2::url_build(parts) + + # create the base request + b_req <- httr2::req_url_path_append( + httr2::request(burl), + "truncate" + ) + + # create the request + req <- httr2::req_body_form( + b_req, + f = "json", + async = async, + attachmentOnly = attachment_only + ) + + # perform request + resp <- httr2::req_perform( + # add the token + httr2::req_auth_bearer_token(req, token) + ) + + res_string <- httr2::resp_body_string(resp) + + res <- RcppSimdJson::fparse(res_string) + detect_errors(res) + res +} diff --git a/man/truncate_layer.Rd b/man/truncate_layer.Rd new file mode 100644 index 0000000..b23e91d --- /dev/null +++ b/man/truncate_layer.Rd @@ -0,0 +1,52 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/truncate.R +\name{truncate_layer} +\alias{truncate_layer} +\title{Truncate a Feature Layer} +\usage{ +truncate_layer( + x, + async = FALSE, + attachment_only = FALSE, + token = Sys.getenv("ARCGIS_TOKEN") +) +} +\arguments{ +\item{x}{A \code{FeatureLayer} or \code{Table} class object created with \code{\link[=arc_open]{arc_open()}}.} + +\item{async}{default \code{FALSE}. It is recommended to set \code{TRUE} for +larger datasets.} + +\item{attachment_only}{default \code{FALSE}. Deletes all the attachments for this +layer. None of the layer features will be deleted when \code{TRUE}.} + +\item{token}{your authorization token. By default checks the environment +variable \code{ARCGIS_TOKEN}. Set your token using \code{arcgisutils::set_auth_token()}.} +} +\value{ +a named list with the name "success" and a value of \code{TRUE} or \code{FALSE} +} +\description{ +Removes all features in a Feature Layer or Table and resets the object ID +counter. Truncating a Feature Layer does not change the schema of the data +(does not add, remove, or alter existing database columns, constraints, +or indexes). +} +\examples{ +\donttest{ +if (interactive()) { + + # authorize using code flow + set_auth_token(auth_code()) + + # create a FeatureLayer object + flayer <- arc_open("your-feature-layer-url") + + # truncate it + truncate_layer(flayer) +} +} +} +\references{ +\href{https://developers.arcgis.com/rest/services-reference/online/truncate-feature-layer-.htm}{ArcGIS Developers Rest API Doc} +}