Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 1 addition & 2 deletions R/oauth-flow-device.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ oauth_flow_device_poll <- function(
)
break
},
httr2_oauth_authorization_pending = function(err) {
},
httr2_oauth_authorization_pending = function(err) {},
httr2_oauth_slow_down = function(err) {
delay <<- delay + 5
}
Expand Down
3 changes: 1 addition & 2 deletions R/oauth.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ cache_noop <- function() {
abort("set() was called on cache_noop")
invisible()
},
clear = function() {
}
clear = function() {}
)
}
cache_mem <- function(client, key = NULL) {
Expand Down
29 changes: 21 additions & 8 deletions R/req-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ cache_prune_if_needed <- function(req, threshold = 60, debug = FALSE) {

last_prune <- the$cache_throttle[[path]]
if (is.null(last_prune) || last_prune + threshold <= Sys.time()) {
if (debug) cli::cli_text("Pruning cache")
if (debug) {
cli::cli_text("Pruning cache")
}
cache_prune(path, max = req$policies$cache_max, debug = debug)
the$cache_throttle[[path]] <- Sys.time()

Expand Down Expand Up @@ -186,8 +188,9 @@ cache_info <- function(path, pattern = "\\.rds$") {

cache_prune_files <- function(info, to_remove, why, debug = TRUE) {
if (any(to_remove)) {
if (debug)
if (debug) {
cli::cli_text("Deleted {sum(to_remove)} file{?s} that {?is/are} {why}")
}

file.remove(info$name[to_remove])
info[!to_remove, ]
Expand Down Expand Up @@ -217,18 +220,24 @@ cache_pre_fetch <- function(req, path = NULL) {
if (is.null(cached_resp)) {
return(req)
}
if (debug) cli::cli_text("Found url in cache {.val {hash(req$url)}}")
if (debug) {
cli::cli_text("Found url in cache {.val {hash(req$url)}}")
}

info <- resp_cache_info(cached_resp)
if (!is.na(info$expires) && info$expires >= Sys.time()) {
signal("", "httr2_cache_cached")
if (debug) cli::cli_text("Cached value is fresh; using response from cache")
if (debug) {
cli::cli_text("Cached value is fresh; using response from cache")
}

resp <- cached_resp
resp$body <- cache_body(cached_resp, path)
resp
} else {
if (debug) cli::cli_text("Cached value is stale; checking for updates")
if (debug) {
cli::cli_text("Cached value is stale; checking for updates")
}
req_headers(
req,
`If-Modified-Since` = info$last_modified,
Expand All @@ -248,16 +257,18 @@ cache_post_fetch <- function(req, resp, path = NULL) {

if (is_error(resp)) {
if (cache_use_on_error(req) && !is.null(cached_resp)) {
if (debug)
if (debug) {
cli::cli_text("Request errored; retrieving response from cache")
}
cached_resp
} else {
resp
}
} else if (resp_status(resp) == 304 && !is.null(cached_resp)) {
signal("", "httr2_cache_not_modified")
if (debug)
if (debug) {
cli::cli_text("Cached value still ok; retrieving body from cache")
}

# Combine headers
resp$headers <- cache_headers(cached_resp, resp)
Expand All @@ -268,7 +279,9 @@ cache_post_fetch <- function(req, resp, path = NULL) {
cache_set(req, resp)
resp
} else if (resp_is_cacheable(resp)) {
if (debug) cli::cli_text("Saving response to cache {.val {hash(req$url)}}")
if (debug) {
cli::cli_text("Saving response to cache {.val {hash(req$url)}}")
}

cache_set(req, resp)
resp
Expand Down
9 changes: 6 additions & 3 deletions R/req-promise.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ req_perform_promise <- function(

ensure_pool_poller <- function(pool, reject) {
monitor <- pool_poller_monitor(pool)
if (monitor$already_going()) return()
if (monitor$already_going()) {
return()
}

poll_pool <- function(ready) {
tryCatch(
Expand Down Expand Up @@ -142,8 +144,9 @@ ensure_pool_poller <- function(pool, reject) {
pool_poller_monitor <- function(pool) {
pool_address <- obj_address(pool)
list(
already_going = function()
env_get(the$pool_pollers, pool_address, default = FALSE),
already_going = function() {
env_get(the$pool_pollers, pool_address, default = FALSE)
},
starting = function() env_poke(the$pool_pollers, pool_address, TRUE),
ending = function() env_unbind(the$pool_pollers, pool_address)
)
Expand Down
4 changes: 3 additions & 1 deletion R/req-template.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ template_val <- function(name, dots, env, error_call = caller_env()) {
}

template_vars <- function(x, type) {
if (type == "none") return(character())
if (type == "none") {
return(character())
}

pattern <- switch(
type,
Expand Down
3 changes: 1 addition & 2 deletions R/resp-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ resp_boundary_pushback <- function(
)
}
} else {
print_buffer <- function(buf, label) {
}
print_buffer <- function(buf, label) {}
}

# Read chunks until we find an event or reach the end of input
Expand Down
7 changes: 3 additions & 4 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ example_app <- function() {
app <- webfakes::httpbin_app()
# paginated iris endpoint
app$get("/iris", function(req, res) {
page <- req$query$page
if (is.null(page)) page <- 1L
page <- req$query$page %||% 1L
page <- as.integer(page)
page_size <- req$query$limit
if (is.null(page_size)) page_size <- 20L

page_size <- req$query$limit %||% 20L
page_size <- as.integer(page_size)

n <- nrow(datasets::iris)
Expand Down
23 changes: 16 additions & 7 deletions R/url.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ url_modify <- function(
url <- url_parse(url)
}

if (!leave_as_is(scheme)) check_string(scheme, allow_null = TRUE)
if (!leave_as_is(hostname)) check_string(hostname, allow_null = TRUE)
if (!leave_as_is(username)) check_string(username, allow_null = TRUE)
if (!leave_as_is(password)) check_string(password, allow_null = TRUE)
if (!leave_as_is(port)) check_number_whole(port, min = 1, allow_null = TRUE)
if (!leave_as_is(path)) check_string(path, allow_null = TRUE)
if (!leave_as_is(fragment)) check_string(fragment, allow_null = TRUE)
check_url_component(scheme)
check_url_component(hostname)
check_url_component(username)
check_url_component(password)
if (!leave_as_is(port)) {
check_number_whole(port, min = 1, max = 65535, allow_null = TRUE)
}
check_url_component(path)
check_url_component(fragment)

if (is_string(query)) {
query <- url_query_parse(query)
Expand Down Expand Up @@ -139,6 +141,13 @@ url_modify <- function(

as_is <- quote(as_is)
leave_as_is <- function(x) identical(x, as_is)
check_url_component <- function(x, arg = caller_arg(x), call = caller_env()) {
if (leave_as_is(x)) {
return(invisible(NULL))
}

check_string(x, allow_null = TRUE, arg = arg, call = call)
}

#' @export
#' @rdname url_modify
Expand Down
14 changes: 8 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ modify_list <- function(
error_call = caller_env()
) {
dots <- list2(...)
if (length(dots) == 0) return(.x)
if (length(dots) == 0) {
return(.x)
}

if (!is_named(dots)) {
cli::cli_abort(
Expand Down Expand Up @@ -246,10 +248,8 @@ create_progress_bar <- function(
) {
if (is_false(config)) {
return(list(
update = function(...) {
},
done = function() {
}
update = function(...) {},
done = function() {}
))
}

Expand Down Expand Up @@ -292,7 +292,9 @@ read_con <- function(con, buffer = 32 * 1024) {
bytes <- raw()
repeat {
new <- readBin(con, "raw", n = buffer)
if (length(new) == 0) break
if (length(new) == 0) {
break
}
bytes <- c(bytes, new)
}
if (length(bytes) == 0) {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/_snaps/url.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
Condition
Error in `url_modify()`:
! `port` must be a whole number or `NULL`, not the string "x".
Code
url_modify(url, port = -1)
Condition
Error in `url_modify()`:
! `port` must be a whole number between 1 and 65535 or `NULL`, not the number -1.
Code
url_modify(url, username = 1)
Condition
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/helper-sync.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ sync_req <- function(name, .env = parent.frame()) {
nanonext::listen(sock, url = sprintf("ipc:///tmp/nanonext%s", name))

function(
expr = {
},
expr = {},
timeout = 1000L
) {
if (!connected) {
Expand Down Expand Up @@ -46,8 +45,7 @@ sync_rep <- function(name, .env = parent.frame()) {
nanonext::dial(sock, url = sprintf("ipc:///tmp/nanonext%s", name))

function(
expr = {
},
expr = {},
timeout = 1000L
) {
if (!connected) {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-oauth-flow-auth-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ test_that("external auth code sources are detected correctly", {

test_that("auth codes can be retrieved from an external source", {
skip_on_cran()
local_mocked_bindings(sys_sleep = function(...) {
})
local_mocked_bindings(sys_sleep = function(...) {})

req <- local_app_request(function(req, res) {
# Error on first, and then respond on second
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-req-mock.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ test_that("mocked_response_sequence returns responses then errors", {

test_that("validates inputs", {
expect_snapshot(error = TRUE, {
local_mocked_responses(function(foo) {
})
local_mocked_responses(function(foo) {})
local_mocked_responses(10)
})
})
9 changes: 3 additions & 6 deletions tests/testthat/test-req-perform-iterative.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,17 @@ test_that("checks its inputs", {
req_perform_iterative(req, function(x, y) x + y)
req_perform_iterative(
req,
function(resp, req) {
},
function(resp, req) {},
path = 1
)
req_perform_iterative(
req,
function(resp, req) {
},
function(resp, req) {},
max_reqs = -1
)
req_perform_iterative(
req,
function(resp, req) {
},
function(resp, req) {},
progress = -1
)
})
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-url.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ test_that("url_modify checks its inputs", {
url_modify(url, scheme = 1)
url_modify(url, hostname = 1)
url_modify(url, port = "x")
url_modify(url, port = -1)
url_modify(url, username = 1)
url_modify(url, password = 1)
url_modify(url, path = 1)
Expand Down
Loading