Skip to content

Commit 4b39012

Browse files
authored
Merge pull request #222 from elipousson/clear-default-query
Add helper functions to clear default query in URL (#215)
2 parents 8dc9093 + 9e0121b commit 4b39012

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

Diff for: NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
- `arc_raster()` gains an argument `raster_fn` which takes a character scalar and performs a raster function server side before returning results
1111
- `list_service_raster_fns()` is a new helper function to list available raster functions for an `ImageServer`
12+
- `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))
1213

1314
## Breaking changes
1415

Diff for: R/arc-open.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
arc_open <- function(url, token = arc_token()) {
5858
check_url(url)
5959

60+
# parse url query and strip from url if query matches default
61+
query <- parse_url_query(url) %||% list()
62+
url <- clear_url_query(url)
63+
6064
# extract layer metadata
6165
meta <- fetch_layer_metadata(url, token)
6266

@@ -84,12 +88,12 @@ arc_open <- function(url, token = arc_token()) {
8488
"FeatureLayer" = structure(
8589
meta,
8690
class = layer_class,
87-
query = list()
91+
query = query
8892
),
8993
"Table" = structure(
9094
meta,
9195
class = layer_class,
92-
query = list()
96+
query = query
9397
),
9498
"FeatureServer" = structure(
9599
meta,

Diff for: R/utils.R

+47
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,50 @@ data_frame <- function(x, call = rlang::caller_env()) {
216216
check_data_frame(x, call = call)
217217
structure(x, class = c("tbl", "data.frame"))
218218
}
219+
220+
#' @noRd
221+
clear_url_query <- function(url, keep_default = FALSE) {
222+
query <- parse_url_query(url, keep_default = keep_default)
223+
224+
if (!is.null(query) && rlang::is_empty(query)) {
225+
return(url)
226+
}
227+
228+
url_elements <- httr2::url_parse(url)
229+
230+
# Rebuild URL without query
231+
paste0(
232+
url_elements[["scheme"]], "://",
233+
url_elements[["hostname"]],
234+
sub("/query$", "", url_elements[["path"]])
235+
)
236+
}
237+
238+
#' @noRd
239+
parse_url_query <- function(url, keep_default = FALSE) {
240+
# Parse url
241+
url_elements <- httr2::url_parse(url)
242+
243+
# Return empty list if no query is included in url
244+
if (is.null(url_elements[["query"]])) {
245+
return(list())
246+
}
247+
248+
# Check for default query values
249+
query_match <- match(
250+
url_elements[["query"]],
251+
c(
252+
list(outFields = "*", where = "1=1", f = "geojson"),
253+
list(outFields = "*", where = "1=1")
254+
)
255+
)
256+
257+
# Return NULL for default query
258+
if (is.numeric(query_match) && !keep_default) {
259+
return(NULL)
260+
}
261+
262+
# Otherwise return query
263+
url_elements[["query"]]
264+
}
265+

Diff for: tests/testthat/test-arc_open.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
test_that("arc_open(): Feature Layer", {
55
ft_url <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0"
66

7-
expect_no_error(arc_open(ft_url))
7+
lyr <- arc_open(ft_url)
88

9+
expect_no_error(lyr)
10+
11+
ft_query_url <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0/query?outFields=%2A&where=1%3D1"
12+
13+
lyr_q <- arc_open(ft_query_url)
14+
15+
expect_identical(lyr, lyr_q)
916
})
1017

1118

0 commit comments

Comments
 (0)