-
Notifications
You must be signed in to change notification settings - Fork 2
/
arc-base-req.R
73 lines (61 loc) · 1.81 KB
/
arc-base-req.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#' 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 path a character vector of paths to be appended to url using [`httr2::req_url_path_append()`]
#' @param query a named vector or named list of query parameters to be appended to the url using [`httr2::req_url_query()`]
#' @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,
path = NULL,
query = NULL,
error_call = rlang::caller_env()
) {
# set the user agent
req <- arc_agent(httr2::request(url))
# handle path if provided
if (!is.null(path)) {
# check path is a character vector
if (!rlang::is_bare_character(path)) {
cli::cli_abort(
"{.arg path} must be a character vector",
call = error_call
)
}
# add path to the url
req <- httr2::req_url_path_append(
req,
rlang::inject(!!path)
)
}
# append a query as well
if (!is.null(query)) {
if (!rlang::is_named2(query)) {
cli::cli_abort(
"{.arg query} must be a named list",
call = error_call
)
}
req <- httr2::req_url_query(req, !!!query)
}
# 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
}