Skip to content

Commit

Permalink
Merge pull request #107 from R-ArcGIS/truncate
Browse files Browse the repository at this point in the history
add truncate support
  • Loading branch information
JosiahParry committed Nov 29, 2023
2 parents 8529e82 + 1f40bbe commit a60513f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# arcgislayers 0.1.0 (unreleased)

- Add `truncate_layer()` to support truncate and append workflow
- Add support for opening `MapServers` <https://github.com/R-ArcGIS/arcgislayers/pull/83>
- `arc_open()` with a layer that does not support `Query` sets the `n` attribute to`NA` <https://github.com/R-ArcGIS/arcgislayers/pull/83>
- Print method will show something like `<FeatureLayer <NA features, 10 fields>>`
Expand Down
84 changes: 84 additions & 0 deletions R/truncate.R
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions man/truncate_layer.Rd

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

0 comments on commit a60513f

Please sign in to comment.