-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from R-ArcGIS/arcreq
Generic base request function
- Loading branch information
Showing
6 changed files
with
72 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.