Skip to content

Commit

Permalink
Merge pull request #222 from elipousson/clear-default-query
Browse files Browse the repository at this point in the history
Add helper functions to clear default query in URL (#215)
  • Loading branch information
JosiahParry authored Sep 26, 2024
2 parents 8dc9093 + 9e0121b commit 4b39012
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- `arc_raster()` gains an argument `raster_fn` which takes a character scalar and performs a raster function server side before returning results
- `list_service_raster_fns()` is a new helper function to list available raster functions for an `ImageServer`
- `arc_open()` ignores queries included in input URLs and retains any custom queries in the `query` attribute for `Table` and `FeatureLayer`s. ([#215](https://github.com/R-ArcGIS/arcgislayers/issues/215))

## Breaking changes

Expand Down
8 changes: 6 additions & 2 deletions R/arc-open.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
arc_open <- function(url, token = arc_token()) {
check_url(url)

# parse url query and strip from url if query matches default
query <- parse_url_query(url) %||% list()
url <- clear_url_query(url)

# extract layer metadata
meta <- fetch_layer_metadata(url, token)

Expand Down Expand Up @@ -84,12 +88,12 @@ arc_open <- function(url, token = arc_token()) {
"FeatureLayer" = structure(
meta,
class = layer_class,
query = list()
query = query
),
"Table" = structure(
meta,
class = layer_class,
query = list()
query = query
),
"FeatureServer" = structure(
meta,
Expand Down
47 changes: 47 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,50 @@ data_frame <- function(x, call = rlang::caller_env()) {
check_data_frame(x, call = call)
structure(x, class = c("tbl", "data.frame"))
}

#' @noRd
clear_url_query <- function(url, keep_default = FALSE) {
query <- parse_url_query(url, keep_default = keep_default)

if (!is.null(query) && rlang::is_empty(query)) {
return(url)
}

url_elements <- httr2::url_parse(url)

# Rebuild URL without query
paste0(
url_elements[["scheme"]], "://",
url_elements[["hostname"]],
sub("/query$", "", url_elements[["path"]])
)
}

#' @noRd
parse_url_query <- function(url, keep_default = FALSE) {
# Parse url
url_elements <- httr2::url_parse(url)

# Return empty list if no query is included in url
if (is.null(url_elements[["query"]])) {
return(list())
}

# Check for default query values
query_match <- match(
url_elements[["query"]],
c(
list(outFields = "*", where = "1=1", f = "geojson"),
list(outFields = "*", where = "1=1")
)
)

# Return NULL for default query
if (is.numeric(query_match) && !keep_default) {
return(NULL)
}

# Otherwise return query
url_elements[["query"]]
}

9 changes: 8 additions & 1 deletion tests/testthat/test-arc_open.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
test_that("arc_open(): Feature Layer", {
ft_url <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0"

expect_no_error(arc_open(ft_url))
lyr <- arc_open(ft_url)

expect_no_error(lyr)

ft_query_url <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0/query?outFields=%2A&where=1%3D1"

lyr_q <- arc_open(ft_query_url)

expect_identical(lyr, lyr_q)
})


Expand Down

0 comments on commit 4b39012

Please sign in to comment.