Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse binary done #116

Merged
merged 2 commits into from
Jul 30, 2019
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: A simple HTTP client, with tools for making HTTP requests,
The package name is a play on curl, the widely used command line tool
for HTTP, and this package is built on top of the R package 'curl', an
interface to 'libcurl' (<https://curl.haxx.se/libcurl>).
Version: 0.8.1.9120
Version: 0.8.1.9123
License: MIT + file LICENSE
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"),
Expand Down
3 changes: 3 additions & 0 deletions R/client.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
stuff_env <- new.env()

#' HTTP client
#'
#' @export
Expand Down Expand Up @@ -524,6 +526,7 @@ HttpClient <- R6::R6Class(
}
}
# build response
stuff_env$z <- list(opts = opts, resp = resp, headers = headers)
HttpResponse$new(
method = opts$method,
url = resp$url,
Expand Down
4 changes: 3 additions & 1 deletion R/response.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ HttpResponse <- R6::R6Class(
pld <- self$content
}
raw <- readBin(pld, "raw", file.info(pld)$size)
return(rawToChar(raw))
try_raw2ch <- tryCatch(rawToChar(raw), error = function(e) e)
rawout <- if (inherits(try_raw2ch, "error")) raw else rawToChar(raw)
return(rawout)
}
if ("stream" %in% names(self$request)) {
return(raw(0))
Expand Down
10 changes: 5 additions & 5 deletions revdep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

|field |value |
|:--------|:-------------------------------------------|
|version |R version 3.6.0 Patched (2019-04-30 r76446) |
|version |R version 3.6.1 Patched (2019-07-17 r76848) |
|os |macOS Mojave 10.14.5 |
|system |x86_64, darwin15.6.0 |
|ui |X11 |
|language |(EN) |
|collate |en_US.UTF-8 |
|ctype |en_US.UTF-8 |
|tz |US/Pacific |
|date |2019-06-27 |
|date |2019-07-26 |

# Dependencies

|package |old |new |Δ |
|:-------|:-----|:-----|:--|
|crul |0.7.4 |0.8.0 |* |
|package |old |new |Δ |
|:-------|:-----|:----------|:--|
|crul |0.8.0 |0.8.1.9123 |* |

# Revdeps

Binary file added tests/fixtures/41001c1990.nc
Binary file not shown.
41 changes: 41 additions & 0 deletions tests/testthat/test-response.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@ test_that("parse method", {
expect_is(suppressMessages(aa$parse(sub = "", toRaw = TRUE)[[1]]), "raw")
})

test_that("parse works when file on disk is binary", {
url <- "https://dods.ndbc.noaa.gov/thredds/fileServer/data/cwind/41001/41001c1997.nc"
f <- "../fixtures/41001c1990.nc"
# f <- file.path(tempdir(), "41001c1990.nc")
# out <- HttpClient$new(url)$get(disk = f)

opts <- list(
url = handle(url),
method = "get",
options = list(httpget = TRUE,
useragent = "libcurl/7.54.0 r-curl/4.0 crul/0.8.1.9123"),
headers = list(`Accept-Encoding` = "gzip, deflate",
Accept = "application/json, text/xml, application/xml, */*"),
disk = f
)

aa <- HttpResponse$new(
method = "get",
url = url,
status_code = 200,
request_headers = opts$headers,
content = f,
handle = opts$url$handle,
request = opts
)

# data is in the file
expect_is(aa$content, "character")
expect_equal(aa$content, f)
expect_is(readLines(aa$content, n = 1, warn = FALSE), "character")

# parse works on the binary content
parsed <- aa$parse()
expect_is(parsed, "raw")
# binfile <- tempfile()
# writeBin(parsed, con = file(binfile, open = "wb"))
# on.exit(close(binfile))
# file.info(binfile)
# file.info(f)
})

test_that("internal fxn: parse_params", {
url <- "https://httpbin.org/get?a=5&foo=bar"
x <- parse_params(url)
Expand Down