From ebef0257f6db26f144fe9db195ca02f0380c3a1e Mon Sep 17 00:00:00 2001 From: Josiah Parry Date: Wed, 7 Feb 2024 09:35:22 -0500 Subject: [PATCH 1/2] Add arc_base_req() function: - fix httr2 package version specification in DESCRIPTION - add arc_base_req() which will add the user-agent and X-Esri-Authorization header --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/arc-base-req.R | 43 +++++++++++++++++++++++++++++++++++++++++++ man/arc_agent.Rd | 4 ++-- man/arc_base_req.Rd | 24 ++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 R/arc-base-req.R create mode 100644 man/arc_base_req.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 80c4ab4..1a617cd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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, diff --git a/NAMESPACE b/NAMESPACE index 0e0a856..9e3503e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/arc-base-req.R b/R/arc-base-req.R new file mode 100644 index 0000000..89c0c9a --- /dev/null +++ b/R/arc-base-req.R @@ -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) +} diff --git a/man/arc_agent.Rd b/man/arc_agent.Rd index 5ae4d99..0526a78 100644 --- a/man/arc_agent.Rd +++ b/man/arc_agent.Rd @@ -17,6 +17,6 @@ Override the default user-agent set by httr2 to indicate that a request came from arcgisutils. } \examples{ -httr2::request("http://example.com") |> - arc_agent() +req <- httr2::request("http://example.com") +arc_agent(req) } diff --git a/man/arc_base_req.Rd b/man/arc_base_req.Rd new file mode 100644 index 0000000..ad986fe --- /dev/null +++ b/man/arc_base_req.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/arc-base-req.R +\name{arc_base_req} +\alias{arc_base_req} +\title{Generate base request} +\usage{ +arc_base_req(url, token = NULL, error_call = rlang::caller_env()) +} +\arguments{ +\item{url}{a valid url that is passed to \code{\link[httr2:request]{httr2::request()}}} + +\item{token}{an object of class \code{httr2_token} as generated by \code{\link[=auth_code]{auth_code()}} +or related function} + +\item{error_call}{the caller environment to be used when propagating errors.} +} +\description{ +This function takes a url and creates a basic httr2 request that +adds the user-agent and adds an authorization token to the +\code{X-Esri-Authorization} header. +} +\examples{ +arc_base_req("https://arcgis.com") +} From 8148eda4ab06d6ea51c468ae3d2de3f6a48a4f8b Mon Sep 17 00:00:00 2001 From: Josiah Parry Date: Wed, 7 Feb 2024 09:37:09 -0500 Subject: [PATCH 2/2] update news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 8715b93..c3dc5cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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