Skip to content

Commit

Permalink
fix #181 replace usage of single letter variables in many places with…
Browse files Browse the repository at this point in the history
… meaningful var names
  • Loading branch information
sckott committed Jul 18, 2024
1 parent 0d53906 commit b76a01d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
62 changes: 32 additions & 30 deletions R/asyncvaried.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,55 +285,55 @@ AsyncVaried <- R6::R6Class(
multi_res <- list()

make_request <- function(i) {
w <- reqs[[i]]$payload
h <- w$url$handle
curl::handle_setopt(h, .list = w$options)
if (!is.null(w$fields)) {
curl::handle_setform(h, .list = w$fields)
request <- reqs[[i]]$payload
handle <- request$url$handle
curl::handle_setopt(handle, .list = request$options)
if (!is.null(request$fields)) {
curl::handle_setform(handle, .list = request$fields)
}
curl::handle_setheaders(h, .list = w$headers)
curl::handle_setheaders(handle, .list = request$headers)

if ("retry_options" %in% names(w)) {
do.call(retry, c(list(i=i, handle=h), w$retry_options))
} else if (is.null(w$disk) && is.null(w$stream)) {
if ("retry_options" %in% names(request)) {
do.call(retry, c(list(i=i, handle=handle), request$retry_options))
} else if (is.null(request$disk) && is.null(request$stream)) {
if (crul_opts$mock) {
check_for_package("webmockr")
adap <- webmockr::CrulAdapter$new()
multi_res[[i]] <<- adap$handle_request(w)
multi_res[[i]] <<- adap$handle_request(request)
} else {
curl::multi_add(
handle = h,
handle = handle,
done = function(res) multi_res[[i]] <<- res,
fail = function(res) multi_res[[i]] <<- make_async_error(res, w),
fail = function(res) multi_res[[i]] <<- make_async_error(res, request),
pool = crulpool
)
}
} else {
if (!is.null(w$disk) && is.null(w$stream)) {
stopifnot(inherits(w$disk, "character"))
ff <- file(w$disk, open = "wb")
if (!is.null(request$disk) && is.null(request$stream)) {
stopifnot(inherits(request$disk, "character"))
file_con <- file(request$disk, open = "wb")
curl::multi_add(
handle = h,
handle = handle,
done = function(res) {
close(ff)
close(file_con)
multi_res[[i]] <<- res
},
fail = function(res) {
close(ff)
multi_res[[i]] <<- make_async_error(res, w)
close(file_con)
multi_res[[i]] <<- make_async_error(res, request)
},
data = ff,
data = file_con,
pool = crulpool
)
} else if (is.null(w$disk) && !is.null(w$stream)) {
stopifnot(is.function(w$stream))
} else if (is.null(request$disk) && !is.null(request$stream)) {
stopifnot(is.function(request$stream))
# assign empty response since stream is a user supplied function
# to write somewhere of their choosing
multi_res[[i]] <<- make_async_error("", w)
multi_res[[i]] <<- make_async_error("", request)
curl::multi_add(
handle = h,
done = w$stream,
fail = function(res) multi_res[[i]] <<- make_async_error(res, w),
handle = handle,
done = request$stream,
fail = function(res) multi_res[[i]] <<- make_async_error(res, request),
pool = crulpool
)
}
Expand All @@ -356,10 +356,12 @@ AsyncVaried <- R6::R6Class(
if (grepl("^ftp://", z$url)) {
headers <- list()
} else {
hh <- rawToChar(z$headers %||% raw(0))
if (nzchar(hh)) {
headers <- lapply(curl::parse_headers(hh, multiple = TRUE),
head_parse)
headers_temp <- rawToChar(z$headers %||% raw(0))
if (nzchar(headers_temp)) {
headers <- lapply(
curl::parse_headers(headers_temp, multiple = TRUE),
head_parse
)
} else {
headers <- list()
}
Expand Down
12 changes: 6 additions & 6 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,15 @@ HttpClient <- R6::R6Class(
if (grepl("^ftp://", resp$url)) {
headers <- list()
} else {
hh <- rawToChar(resp$headers %||% raw(0))
if (!validEnc(hh)) {
Encoding(hh) <- "latin1"
if (!validEnc(hh)) stop("Headers aren't encoded in UTF-8 or Latin1")
headers_temp <- rawToChar(resp$headers %||% raw(0))
if (!validEnc(headers_temp)) {
Encoding(headers_temp) <- "latin1"
if (!validEnc(headers_temp)) stop("Headers aren't encoded in UTF-8 or Latin1")
}
if (is.null(hh) || nchar(hh) == 0) {
if (is.null(headers_temp) || nchar(headers_temp) == 0) {
headers <- list()
} else {
headers <- lapply(curl::parse_headers(hh, multiple = TRUE),
headers <- lapply(curl::parse_headers(headers_temp, multiple = TRUE),
head_parse)
}
}
Expand Down
17 changes: 9 additions & 8 deletions R/ok.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ ok.default <- function(x, status=200L, info=TRUE, verb="head",
#' @export
ok.character <- function(x, status=200L, info=TRUE, verb="head",
ua_random=FALSE, ...) {
z <- crul::HttpClient$new(x, opts = list(...))
ok(z, status, info, verb, ua_random, ...)
con <- crul::HttpClient$new(x, opts = list(...))
ok(con, status, info, verb, ua_random, ...)
}

#' @export
Expand All @@ -115,15 +115,16 @@ ok.HttpClient <- function(x, status=200L, info=TRUE, verb="head",
# set ua
if (ua_random) x$opts$useragent <- sample(agents, size=1)
for (i in seq_along(status)) {
ts <- tryCatch(httpcode::http_code(status[i]), error = function(e) e)
if (inherits(ts, "error"))
try_status <- tryCatch(
httpcode::http_code(status[i]), error = function(e) e)
if (inherits(try_status, "error"))
stop("status [", status[i], "] not in acceptable set")
}

w <- tryCatch(x$verb(verb), error = function(e) e)
if (inherits(w, "error")) {
if (info) message(w$message)
try_req <- tryCatch(x$verb(verb), error = function(e) e)
if (inherits(try_req, "error")) {
if (info) message(try_req$message)
return(FALSE)
}
w$status_code %in% status
try_req$status_code %in% status
}

0 comments on commit b76a01d

Please sign in to comment.