# NOT RUN {
cc <- Async$new(
urls = c(
'https://httpbin.org/',
'https://httpbin.org/get?a=5',
'https://httpbin.org/get?foo=bar'
)
)
cc
(res <- cc$get())
res[[1]]
res[[1]]$url
res[[1]]$success()
res[[1]]$status_http()
res[[1]]$response_headers
res[[1]]$method
res[[1]]$content
res[[1]]$parse("UTF-8")
lapply(res, function(z) z$parse("UTF-8"))
# curl options/headers with async
urls = c(
'https://httpbin.org/',
'https://httpbin.org/get?a=5',
'https://httpbin.org/get?foo=bar'
)
cc <- Async$new(urls = urls,
opts = list(verbose = TRUE),
headers = list(foo = "bar")
)
cc
(res <- cc$get())
# using auth with async
dd <- Async$new(
urls = rep('https://httpbin.org/basic-auth/user/passwd', 3),
auth = auth(user = "foo", pwd = "passwd"),
opts = list(verbose = TRUE)
)
dd
res <- dd$get()
res
vapply(res, function(z) z$status_code, double(1))
vapply(res, function(z) z$success(), logical(1))
lapply(res, function(z) z$parse("UTF-8"))
# failure behavior
## e.g. when a URL doesn't exist, a timeout, etc.
urls <- c("http://stuffthings.gvb", "https://foo.com",
"https://httpbin.org/get")
conn <- Async$new(urls = urls)
res <- conn$get()
res[[1]]$parse("UTF-8") # a failure
res[[2]]$parse("UTF-8") # a failure
res[[3]]$parse("UTF-8") # a success
# use arbitrary http verb
cc <- Async$new(
urls = c(
'https://httpbin.org/',
'https://httpbin.org/get?a=5',
'https://httpbin.org/get?foo=bar'
)
)
method <- 'get'
(res <- cc$verb(method))
lapply(res, function(z) z$parse("UTF-8"))
# }
Run the code above in your browser using DataLab