Skip to content

Commit

Permalink
Merge pull request #18 from R-ArcGIS/arcreq
Browse files Browse the repository at this point in the history
Generic base request function
  • Loading branch information
JosiahParry authored Feb 7, 2024
2 parents c6f225a + 8148eda commit a344097
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ URL: https://github.com/R-ArcGIS/arcgisutils,
BugReports: https://github.com/R-ArcGIS/arcgisutils/issues
Imports:
dbplyr,
httr2 >= 1.0.0,
httr2 (>= 1.0.0),
jsonify,
Rcpp,
RcppSimdJson,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ S3method(has_z,sfc)
S3method(has_z,sfg)
export("%||%")
export(arc_agent)
export(arc_base_req)
export(arc_host)
export(arc_token)
export(as_esri_features)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# arcgisutils (development version)

- `arc_base_req()` is introduce creating a standardized way to making base httr2 request objects.
- httr2 must be >= 1.0.0 now
* New function `arc_agent()` is added to set a package sepcific user agent
* `fetch_layer_metadata()` now puts `f=json` in the url instead of the request body
Expand Down
43 changes: 43 additions & 0 deletions R/arc-base-req.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' Generate base request
#'
#' This function takes a url and creates a basic httr2 request that
#' adds the user-agent and adds an authorization token to the
#' `X-Esri-Authorization` header.
#'
#' @param url a valid url that is passed to [`httr2::request()`]
#' @param token an object of class `httr2_token` as generated by [`auth_code()`]
#' or related function
#' @param error_call the caller environment to be used when propagating errors.
#' @export
#' @examples
#' arc_base_req("https://arcgis.com")
arc_base_req <- function(url, token = NULL, error_call = rlang::caller_env()) {
# set the user agent
req <- arc_agent(httr2::request(url))

# if token is not missing, check it
if (!is.null(token)) {

# ensure that the token is an httr2_token
obj_check_token(token, error_call)

# set the auth header
req <- httr2::req_headers(
req,
"X-Esri-Authorization" = paste("Bearer", token[["access_token"]])
)
}

req
}



obj_check_token <- function(token, call = rlang::caller_env()) {
if (!rlang::inherits_only(token, "httr2_token")) {
cli::cli_abort(
"{.arg token} must be an {.cls httr2_token} not {.cls {class(token)}}"
)
}
invisible(token)
}
4 changes: 2 additions & 2 deletions man/arc_agent.Rd

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

24 changes: 24 additions & 0 deletions man/arc_base_req.Rd

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

0 comments on commit a344097

Please sign in to comment.