Skip to content

Commit df68c2b

Browse files
authored
Restyle for air 0.7.0 (#740)
1 parent 8d6e356 commit df68c2b

File tree

15 files changed

+73
-49
lines changed

15 files changed

+73
-49
lines changed

R/oauth-flow-device.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ oauth_flow_device_poll <- function(
144144
)
145145
break
146146
},
147-
httr2_oauth_authorization_pending = function(err) {
148-
},
147+
httr2_oauth_authorization_pending = function(err) {},
149148
httr2_oauth_slow_down = function(err) {
150149
delay <<- delay + 5
151150
}

R/oauth.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ cache_noop <- function() {
156156
abort("set() was called on cache_noop")
157157
invisible()
158158
},
159-
clear = function() {
160-
}
159+
clear = function() {}
161160
)
162161
}
163162
cache_mem <- function(client, key = NULL) {

R/req-cache.R

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ cache_prune_if_needed <- function(req, threshold = 60, debug = FALSE) {
138138

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

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

187189
cache_prune_files <- function(info, to_remove, why, debug = TRUE) {
188190
if (any(to_remove)) {
189-
if (debug)
191+
if (debug) {
190192
cli::cli_text("Deleted {sum(to_remove)} file{?s} that {?is/are} {why}")
193+
}
191194

192195
file.remove(info$name[to_remove])
193196
info[!to_remove, ]
@@ -217,18 +220,24 @@ cache_pre_fetch <- function(req, path = NULL) {
217220
if (is.null(cached_resp)) {
218221
return(req)
219222
}
220-
if (debug) cli::cli_text("Found url in cache {.val {hash(req$url)}}")
223+
if (debug) {
224+
cli::cli_text("Found url in cache {.val {hash(req$url)}}")
225+
}
221226

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

227234
resp <- cached_resp
228235
resp$body <- cache_body(cached_resp, path)
229236
resp
230237
} else {
231-
if (debug) cli::cli_text("Cached value is stale; checking for updates")
238+
if (debug) {
239+
cli::cli_text("Cached value is stale; checking for updates")
240+
}
232241
req_headers(
233242
req,
234243
`If-Modified-Since` = info$last_modified,
@@ -248,16 +257,18 @@ cache_post_fetch <- function(req, resp, path = NULL) {
248257

249258
if (is_error(resp)) {
250259
if (cache_use_on_error(req) && !is.null(cached_resp)) {
251-
if (debug)
260+
if (debug) {
252261
cli::cli_text("Request errored; retrieving response from cache")
262+
}
253263
cached_resp
254264
} else {
255265
resp
256266
}
257267
} else if (resp_status(resp) == 304 && !is.null(cached_resp)) {
258268
signal("", "httr2_cache_not_modified")
259-
if (debug)
269+
if (debug) {
260270
cli::cli_text("Cached value still ok; retrieving body from cache")
271+
}
261272

262273
# Combine headers
263274
resp$headers <- cache_headers(cached_resp, resp)
@@ -268,7 +279,9 @@ cache_post_fetch <- function(req, resp, path = NULL) {
268279
cache_set(req, resp)
269280
resp
270281
} else if (resp_is_cacheable(resp)) {
271-
if (debug) cli::cli_text("Saving response to cache {.val {hash(req$url)}}")
282+
if (debug) {
283+
cli::cli_text("Saving response to cache {.val {hash(req$url)}}")
284+
}
272285

273286
cache_set(req, resp)
274287
resp

R/req-promise.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ req_perform_promise <- function(
109109

110110
ensure_pool_poller <- function(pool, reject) {
111111
monitor <- pool_poller_monitor(pool)
112-
if (monitor$already_going()) return()
112+
if (monitor$already_going()) {
113+
return()
114+
}
113115

114116
poll_pool <- function(ready) {
115117
tryCatch(
@@ -142,8 +144,9 @@ ensure_pool_poller <- function(pool, reject) {
142144
pool_poller_monitor <- function(pool) {
143145
pool_address <- obj_address(pool)
144146
list(
145-
already_going = function()
146-
env_get(the$pool_pollers, pool_address, default = FALSE),
147+
already_going = function() {
148+
env_get(the$pool_pollers, pool_address, default = FALSE)
149+
},
147150
starting = function() env_poke(the$pool_pollers, pool_address, TRUE),
148151
ending = function() env_unbind(the$pool_pollers, pool_address)
149152
)

R/req-template.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ template_val <- function(name, dots, env, error_call = caller_env()) {
108108
}
109109

110110
template_vars <- function(x, type) {
111-
if (type == "none") return(character())
111+
if (type == "none") {
112+
return(character())
113+
}
112114

113115
pattern <- switch(
114116
type,

R/resp-stream.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ resp_boundary_pushback <- function(
302302
)
303303
}
304304
} else {
305-
print_buffer <- function(buf, label) {
306-
}
305+
print_buffer <- function(buf, label) {}
307306
}
308307

309308
# Read chunks until we find an event or reach the end of input

R/test.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ example_app <- function() {
3131
app <- webfakes::httpbin_app()
3232
# paginated iris endpoint
3333
app$get("/iris", function(req, res) {
34-
page <- req$query$page
35-
if (is.null(page)) page <- 1L
34+
page <- req$query$page %||% 1L
3635
page <- as.integer(page)
37-
page_size <- req$query$limit
38-
if (is.null(page_size)) page_size <- 20L
36+
37+
page_size <- req$query$limit %||% 20L
3938
page_size <- as.integer(page_size)
4039

4140
n <- nrow(datasets::iris)

R/url.R

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ url_modify <- function(
9999
url <- url_parse(url)
100100
}
101101

102-
if (!leave_as_is(scheme)) check_string(scheme, allow_null = TRUE)
103-
if (!leave_as_is(hostname)) check_string(hostname, allow_null = TRUE)
104-
if (!leave_as_is(username)) check_string(username, allow_null = TRUE)
105-
if (!leave_as_is(password)) check_string(password, allow_null = TRUE)
106-
if (!leave_as_is(port)) check_number_whole(port, min = 1, allow_null = TRUE)
107-
if (!leave_as_is(path)) check_string(path, allow_null = TRUE)
108-
if (!leave_as_is(fragment)) check_string(fragment, allow_null = TRUE)
102+
check_url_component(scheme)
103+
check_url_component(hostname)
104+
check_url_component(username)
105+
check_url_component(password)
106+
if (!leave_as_is(port)) {
107+
check_number_whole(port, min = 1, max = 65535, allow_null = TRUE)
108+
}
109+
check_url_component(path)
110+
check_url_component(fragment)
109111

110112
if (is_string(query)) {
111113
query <- url_query_parse(query)
@@ -139,6 +141,13 @@ url_modify <- function(
139141

140142
as_is <- quote(as_is)
141143
leave_as_is <- function(x) identical(x, as_is)
144+
check_url_component <- function(x, arg = caller_arg(x), call = caller_env()) {
145+
if (leave_as_is(x)) {
146+
return(invisible(NULL))
147+
}
148+
149+
check_string(x, allow_null = TRUE, arg = arg, call = call)
150+
}
142151

143152
#' @export
144153
#' @rdname url_modify

R/utils.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ modify_list <- function(
3939
error_call = caller_env()
4040
) {
4141
dots <- list2(...)
42-
if (length(dots) == 0) return(.x)
42+
if (length(dots) == 0) {
43+
return(.x)
44+
}
4345

4446
if (!is_named(dots)) {
4547
cli::cli_abort(
@@ -246,10 +248,8 @@ create_progress_bar <- function(
246248
) {
247249
if (is_false(config)) {
248250
return(list(
249-
update = function(...) {
250-
},
251-
done = function() {
252-
}
251+
update = function(...) {},
252+
done = function() {}
253253
))
254254
}
255255

@@ -292,7 +292,9 @@ read_con <- function(con, buffer = 32 * 1024) {
292292
bytes <- raw()
293293
repeat {
294294
new <- readBin(con, "raw", n = buffer)
295-
if (length(new) == 0) break
295+
if (length(new) == 0) {
296+
break
297+
}
296298
bytes <- c(bytes, new)
297299
}
298300
if (length(bytes) == 0) {

tests/testthat/_snaps/url.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
Condition
5555
Error in `url_modify()`:
5656
! `port` must be a whole number or `NULL`, not the string "x".
57+
Code
58+
url_modify(url, port = -1)
59+
Condition
60+
Error in `url_modify()`:
61+
! `port` must be a whole number between 1 and 65535 or `NULL`, not the number -1.
5762
Code
5863
url_modify(url, username = 1)
5964
Condition

0 commit comments

Comments
 (0)