/usr/lib/R/site-library/crul/doc/async.Rmd is in r-cran-crul 0.5.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | <!--
%\VignetteIndexEntry{async with crul}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
-->
async with crul
===============
Asynchronous requests with `crul`.
There are two interfaces to asynchronous requests in `crul`:
1. Simple async: any number of URLs, all treated with the same curl options,
headers, etc., and only one HTTP method type at a time.
2. Varied request async: build any type of request and execute all asynchronously.
The first option takes less thinking, less work, and is good solution when you
just want to hit a bunch of URLs asynchronously.
The second option is ideal when you want to set curl options/headers on each
request and/or want to do different types of HTTP methods on each request.
One thing to think about before using async is whether the data provider is
okay with it. It's possible that a data provider's service may be brought down
if you do too many async requests.
```r
library("crul")
```
## simple async
Build request object with 1 or more URLs
```r
(cc <- Async$new(
urls = c(
'https://httpbin.org/get?a=5',
'https://httpbin.org/get?a=5&b=6',
'https://httpbin.org/ip'
)
))
#> <crul async connection>
#> urls:
#> https://httpbin.org/get?a=5
#> https://httpbin.org/get?a=5&b=6
#> https://httpbin.org/ip
```
Make request with any HTTP method
```r
(res <- cc$get())
#> [[1]]
#> <crul response>
#> url: https://httpbin.org/get?a=5
#> request_headers:
#> response_headers:
#> status: HTTP/1.1 200 OK
#> connection: keep-alive
#> server: meinheld/0.6.1
#> date: Fri, 19 Jan 2018 18:44:29 GMT
#> content-type: application/json
#> access-control-allow-origin: *
#> access-control-allow-credentials: true
#> x-powered-by: Flask
#> x-processed-time: 0.000792026519775
#> content-length: 346
#> via: 1.1 vegur
#> params:
#> a: 5
#> status: 200
#>
#> [[2]]
#> <crul response>
#> url: https://httpbin.org/get?a=5&b=6
#> request_headers:
#> response_headers:
#> status: HTTP/1.1 200 OK
#> connection: keep-alive
#> server: meinheld/0.6.1
#> date: Fri, 19 Jan 2018 18:44:29 GMT
#> content-type: application/json
#> access-control-allow-origin: *
#> access-control-allow-credentials: true
#> x-powered-by: Flask
#> x-processed-time: 0.00130796432495
#> content-length: 365
#> via: 1.1 vegur
#> params:
#> a: 5
#> b: 6
#> status: 200
#>
#> [[3]]
#> <crul response>
#> url: https://httpbin.org/ip
#> request_headers:
#> response_headers:
#> status: HTTP/1.1 200 OK
#> connection: keep-alive
#> server: meinheld/0.6.1
#> date: Fri, 19 Jan 2018 18:44:28 GMT
#> content-type: application/json
#> access-control-allow-origin: *
#> access-control-allow-credentials: true
#> x-powered-by: Flask
#> x-processed-time: 0.000822067260742
#> content-length: 32
#> via: 1.1 vegur
#> status: 200
```
You get back a list matching length of the number of input URLs
Access object variables and methods just as with `HttpClient` results, here just one at a time.
```r
res[[1]]$url
#> [1] "https://httpbin.org/get?a=5"
res[[1]]$success()
#> [1] TRUE
res[[1]]$parse("UTF-8")
#> [1] "{\n \"args\": {\n \"a\": \"5\"\n }, \n \"headers\": {\n \"Accept\": \"application/json, text/xml, application/xml, */*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Connection\": \"close\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"libcurl/7.54.0 r-curl/3.1 crul/0.5.0\"\n }, \n \"origin\": \"50.22.155.214\", \n \"url\": \"https://httpbin.org/get?a=5\"\n}\n"
```
Or apply access/method calls across many results, e.g., parse all results
```r
lapply(res, function(z) z$parse("UTF-8"))
#> [[1]]
#> [1] "{\n \"args\": {\n \"a\": \"5\"\n }, \n \"headers\": {\n \"Accept\": \"application/json, text/xml, application/xml, */*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Connection\": \"close\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"libcurl/7.54.0 r-curl/3.1 crul/0.5.0\"\n }, \n \"origin\": \"50.22.155.214\", \n \"url\": \"https://httpbin.org/get?a=5\"\n}\n"
#>
#> [[2]]
#> [1] "{\n \"args\": {\n \"a\": \"5\", \n \"b\": \"6\"\n }, \n \"headers\": {\n \"Accept\": \"application/json, text/xml, application/xml, */*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Connection\": \"close\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"libcurl/7.54.0 r-curl/3.1 crul/0.5.0\"\n }, \n \"origin\": \"50.22.155.214\", \n \"url\": \"https://httpbin.org/get?a=5&b=6\"\n}\n"
#>
#> [[3]]
#> [1] "{\n \"origin\": \"50.22.155.214\"\n}\n"
```
## varied request async
```r
req1 <- HttpRequest$new(
url = "https://httpbin.org/get?a=5",
opts = list(
verbose = TRUE
)
)
req1$get()
#> <crul http request> get
#> url: https://httpbin.org/get?a=5
#> curl options:
#> verbose: TRUE
#> proxies:
#> auth:
#> headers:
req2 <- HttpRequest$new(
url = "https://httpbin.org/post?a=5&b=6"
)
req2$post(body = list(a = 5))
#> <crul http request> post
#> url: https://httpbin.org/post?a=5&b=6
#> curl options:
#> proxies:
#> auth:
#> headers:
(res <- AsyncVaried$new(req1, req2))
#> <crul async varied connection>
#> requests:
#> get: https://httpbin.org/get?a=5
#> post: https://httpbin.org/post?a=5&b=6
```
Make requests asynchronously
```r
res$request()
```
Parse all results
```r
res$parse()
#> [1] "{\n \"args\": {\n \"a\": \"5\"\n }, \n \"headers\": {\n \"Accept\": \"application/json, text/xml, application/xml, */*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Connection\": \"close\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"libcurl/7.54.0 r-curl/3.1 crul/0.5.0\"\n }, \n \"origin\": \"50.22.155.214\", \n \"url\": \"https://httpbin.org/get?a=5\"\n}\n"
#> [2] "{\n \"args\": {\n \"a\": \"5\", \n \"b\": \"6\"\n }, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"a\": \"5\"\n }, \n \"headers\": {\n \"Accept\": \"application/json, text/xml, application/xml, */*\", \n \"Accept-Encoding\": \"gzip, deflate\", \n \"Connection\": \"close\", \n \"Content-Length\": \"137\", \n \"Content-Type\": \"multipart/form-data; boundary=------------------------14f323a90518346b\", \n \"Host\": \"httpbin.org\", \n \"User-Agent\": \"libcurl/7.54.0 r-curl/3.1 crul/0.5.0\"\n }, \n \"json\": null, \n \"origin\": \"50.22.155.214\", \n \"url\": \"https://httpbin.org/post?a=5&b=6\"\n}\n"
```
```r
lapply(res$parse(), jsonlite::prettify)
#> [[1]]
#> {
#> "args": {
#> "a": "5"
#> },
#> "headers": {
#> "Accept": "application/json, text/xml, application/xml, */*",
#> "Accept-Encoding": "gzip, deflate",
#> "Connection": "close",
#> "Host": "httpbin.org",
#> "User-Agent": "libcurl/7.54.0 r-curl/3.1 crul/0.5.0"
#> },
#> "origin": "50.22.155.214",
#> "url": "https://httpbin.org/get?a=5"
#> }
#>
#>
#> [[2]]
#> {
#> "args": {
#> "a": "5",
#> "b": "6"
#> },
#> "data": "",
#> "files": {
#>
#> },
#> "form": {
#> "a": "5"
#> },
#> "headers": {
#> "Accept": "application/json, text/xml, application/xml, */*",
#> "Accept-Encoding": "gzip, deflate",
#> "Connection": "close",
#> "Content-Length": "137",
#> "Content-Type": "multipart/form-data; boundary=------------------------14f323a90518346b",
#> "Host": "httpbin.org",
#> "User-Agent": "libcurl/7.54.0 r-curl/3.1 crul/0.5.0"
#> },
#> "json": null,
#> "origin": "50.22.155.214",
#> "url": "https://httpbin.org/post?a=5&b=6"
#> }
#>
```
Status codes
```r
res$status_code()
#> [1] 200 200
```
|