Skip to content

Commit

Permalink
fix #34 fix #35 fix #36 -
Browse files Browse the repository at this point in the history
using gzip encoding by default now
setting User-Agent header instead of useragent header
user set headers now will replace default ones, only applies ot useragent and accept encoding
bumped dev version
  • Loading branch information
sckott committed Apr 6, 2017
1 parent 2b33a69 commit b2b321e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
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.3.4.9410
Version: 0.3.4.9415
License: MIT + file LICENSE
Authors@R: c(
person("Scott", "Chamberlain", role = c("aut", "cre"),
Expand Down
9 changes: 6 additions & 3 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ HttpClient <- R6::R6Class(
url = url,
method = "get",
options = list(
httpget = TRUE,
useragent = make_ua()
httpget = TRUE
),
headers = self$headers
headers = list(
`User-Agent` = make_ua(),
`Accept-Encoding` = 'gzip, deflate'
)
)
rr$headers <- norm_headers(rr$headers, self$headers)
rr$options <- utils::modifyList(
rr$options, c(self$opts, self$proxies, self$auth, ...))
rr$disk <- disk
Expand Down
6 changes: 5 additions & 1 deletion R/httprequest.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,12 @@ HttpRequest <- R6::R6Class(
httpget = TRUE,
useragent = make_ua()
),
headers = self$headers
headers = list(
`User-Agent` = make_ua(),
`Accept-Encoding` = 'gzip, deflate'
)
)
rr$headers <- norm_headers(rr$headers, self$headers)
rr$options <- utils::modifyList(
rr$options, c(self$opts, self$proxies, self$auth, ...))
rr$disk <- disk
Expand Down
18 changes: 15 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,27 @@ prep_opts <- function(method, url, self, opts, ...) {
url = url,
method = method,
options = as.list(c(
opts$opts,
useragent = make_ua()
opts$opts
)),
headers = as.list(c(
opts$type,
`User-Agent` = make_ua(),
`Accept-Encoding` = 'gzip, deflate'
)),
headers = c(self$headers, opts$type),
fields = opts$fields
)
rr$headers <- norm_headers(rr$headers, self$headers)
rr$options <- utils::modifyList(
rr$options,
c(self$opts, self$proxies, self$auth, ...)
)
return(rr)
}

norm_headers <- function(x, y) {
if (length(names(y)) > 0) {
x <- x[!names(x) %in% names(y)]
x <- c(x, y)
}
return(x)
}
6 changes: 3 additions & 3 deletions tests/testthat/test-headers.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("headers work - just default headers", {
aa <- cli$get('get')

expect_is(aa, "HttpResponse")
expect_named(aa$request_headers, 'useragent')
expect_named(aa$request_headers, c('User-Agent', 'Accept-Encoding'))
})

test_that("headers work - user headers passed", {
Expand All @@ -20,7 +20,7 @@ test_that("headers work - user headers passed", {
bb <- cli$get('get')

expect_is(bb, "HttpResponse")
expect_named(bb$request_headers, c('useragent', 'hello'))
expect_named(bb$request_headers, c('User-Agent', 'Accept-Encoding', 'hello'))
expect_true(
any(grepl("Hello", names(jsonlite::fromJSON(bb$parse())$headers))))
any(grepl("Hello", names(jsonlite::fromJSON(bb$parse("UTF-8"))$headers))))
})

0 comments on commit b2b321e

Please sign in to comment.