-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add httr_progress to use with the httr package #103
- Loading branch information
Showing
8 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' Waitress with httr | ||
#' | ||
#' Use a waitress progress bar with httr requests. | ||
#' Simply use `httr_progress` where you would use | ||
#' [httr::progress]. | ||
#' | ||
#' @param waitress The waitress object. | ||
#' @param type Type of progress to display: either number of | ||
#' bytes uploaded or downloaded. Passed to [httr::progress]. | ||
#' @param pre,post Pre and callback functions to run before | ||
#' the progress starts or once it is done. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' cap_speed <- httr::config(max_recv_speed_large = 10000) | ||
#' | ||
#' httr::GET( | ||
#' "http://httpbin.org/bytes/102400", | ||
#' httr_progress(w), | ||
#' cap_speed | ||
#' ) | ||
#' } | ||
httr_progress <- function( | ||
waitress, | ||
type = c("down", "up"), | ||
pre = NULL, | ||
post = NULL | ||
){ | ||
type <- match.arg(type) | ||
|
||
if(missing(waitress)) | ||
stop("Missing `waitress` object", call. = FALSE) | ||
|
||
httr_request <- utils::getFromNamespace("request", "httr") | ||
|
||
httr_request( | ||
options = list( | ||
noprogress = FALSE, | ||
progressfunction = progress_bar(waitress, type, pre, post) | ||
) | ||
) | ||
} | ||
|
||
progress_bar <- function(w, type, pre = NULL, post = NULL) { | ||
|
||
show_progress <- function(down, up) { | ||
if (type == "down") { | ||
total <- down[[1]] | ||
now <- down[[2]] | ||
} else { | ||
total <- up[[1]] | ||
now <- up[[2]] | ||
} | ||
|
||
if (total == 0 && now == 0) { | ||
if(!is.null(pre)) | ||
pre() | ||
# Reset progress bar when seeing first byte | ||
w$start() | ||
w$set(0) | ||
} else { | ||
w$max <- total | ||
|
||
# increment | ||
w$set(now) | ||
# close | ||
if(now == total) { | ||
w$close() | ||
|
||
if(!is.null(post)) | ||
post() | ||
} | ||
} | ||
|
||
TRUE | ||
} | ||
|
||
show_progress | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.