Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache default_ua on package load #322

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions R/config.r
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,8 @@ curl_docs <- function(x) {
BROWSE(url)
}

default_ua <- function() {
versions <- c(
libcurl = curl::curl_version()$version,
`r-curl` = as.character(packageVersion("curl")),
httr = as.character(packageVersion("httr"))
)
paste0(names(versions), "/", versions, collapse = " ")
}
# This is defined in .onLoad()
default_ua <- NULL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please keep all the code here? I'd prefer you did something like

cache <- new.env(parent = emptyenv())
cache$default_ua <- NULL

and then returned early in default_ua() if that value was set


#' Set (and reset) global httr configuration.
#'
Expand Down
2 changes: 1 addition & 1 deletion R/request.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ request_default <- function() {
c(
request(
options = list(
useragent = default_ua(),
useragent = default_ua,
cainfo = find_cert_bundle()
),
headers = c(Accept = "application/json, text/xml, application/xml, */*"),
Expand Down
6 changes: 6 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
toset <- !(names(op.dplyr) %in% names(op))
if(any(toset)) options(op.dplyr[toset])

versions <- c(
libcurl = curl::curl_version()$version,
`r-curl` = as.character(packageVersion("curl")),
httr = as.character(packageVersion("httr")))
default_ua <<- paste0(names(versions), "/", versions, collapse = " ")

invisible()
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-response.r
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test_that("application/json responses parsed as lists", {
add_headers("user-agent" = user_agent))

expect_equal(response$status_code, 200)
expected_reply = list("user-agent" = user_agent %||% default_ua())
expected_reply = list("user-agent" = user_agent %||% default_ua)
expect_equal(expected_reply, content(response))
}

Expand Down