Skip to content

Commit

Permalink
Ensure on disk contents work like in memory.
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
hadley committed Aug 23, 2014
1 parent 25eebe3 commit a48c1f5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ S3method(c,config)
S3method(cookies,handle)
S3method(cookies,response)
S3method(headers,response)
S3method(length,path)
S3method(print,cache_info)
S3method(print,config)
S3method(print,handle)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# httr 0.4.0.99

* You can now save response bodies directly to disk by using the `write_disk()`
config. This is useful if you want to capture large files that don't fit in
memory (#44).

* Typo which broke `set_cookies()` fixed by @hrbrmstr.

* `content(type = "text")` uses `readBin()` instead of `rawToChar()` so
Expand Down
12 changes: 9 additions & 3 deletions R/content.r
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,16 @@ content <- function(x, as = NULL, type = NULL, encoding = NULL, ...) {
as <- as %||% parseability(type)
as <- match.arg(as, c("raw", "text", "parsed"))

if (is.path(x$content)) {
raw <- readBin(x$content, "raw", file.info(x$content)$size)
} else {
raw <- x$content
}

switch(as,
raw = x$content,
text = parse_text(x$content, type, encoding),
parsed = parse_auto(x$content, type, encoding, ...)
raw = raw,
text = parse_text(raw, type, encoding),
parsed = parse_auto(raw, type, encoding, ...)
)
}

Expand Down
4 changes: 4 additions & 0 deletions R/response.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ print.response <- function(x, ..., max.lines = 10, width = getOption("width")) {
}

cat(" Size: ", bytes(size), "\n", sep = "")
if (is.path(x$content)) {
cat("<ON DISK> ", x$content)
return()
}
if (!is_text(content_type)) {
cat("<BINARY BODY>\n")
return()
Expand Down
13 changes: 11 additions & 2 deletions R/write-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ write_term <- function(x) UseMethod("write_term")
#' @useDynLib httr writer
#' @examples
#' tmp <- tempfile()
#' r <- GET("https://www.google.com", write_disk(tmp))
#' r1 <- GET("https://www.google.com", write_disk(tmp))
#' readLines(tmp)
#'
#' # The default
#' r <- GET("https://www.google.com", write_memory())
#' r2 <- GET("https://www.google.com", write_memory())
#'
#' # Save a very large file
#' \dontrun{
#' GET("http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip",
#' write_disk("csv_pus.zip"), progress())
#' }
write_disk <- function(path, overwrite = FALSE) {
if (!overwrite && file.exists(path)) {
stop("Path exists and overrwrite is FALSE", call. = FALSE)
Expand Down Expand Up @@ -79,6 +85,9 @@ print.write_disk <- function(x, ...) {
}

path <- function(x) structure(x, class = "path")
#' @export
length.path <- function(x) file.info(x)$size
is.path <- function(x) inherits(x, "path")

#' @rdname write_disk
#' @export
Expand Down
10 changes: 8 additions & 2 deletions man/write_disk.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ than memory, use \code{write_disk()} to save it to a known path.
}
\examples{
tmp <- tempfile()
r <- GET("https://www.google.com", write_disk(tmp))
r1 <- GET("https://www.google.com", write_disk(tmp))
readLines(tmp)
# The default
r <- GET("https://www.google.com", write_memory())
r2 <- GET("https://www.google.com", write_memory())
# Save a very large file
\dontrun{
GET("http://www2.census.gov/acs2011_5yr/pums/csv_pus.zip",
write_disk("csv_pus.zip"), progress())
}
}

0 comments on commit a48c1f5

Please sign in to comment.