| 1 |
# nanonext - ncurl - async http client ----------------------------------------- |
| 2 |
|
| 3 |
#' ncurl |
| 4 |
#' |
| 5 |
#' nano cURL - a minimalist http(s) client. |
| 6 |
#' |
| 7 |
#' @param url the URL address. |
| 8 |
#' @param convert \[default TRUE\] logical value whether to attempt conversion |
| 9 |
#' of the received raw bytes to a character vector. Set to `FALSE` if |
| 10 |
#' downloading non-text data. |
| 11 |
#' @param follow \[default FALSE\] logical value whether to automatically follow |
| 12 |
#' redirects (not applicable for async requests). If `FALSE`, the redirect |
| 13 |
#' address is returned as response header 'Location'. |
| 14 |
#' @param method (optional) the HTTP method as a character string. Defaults to |
| 15 |
#' 'GET' if not specified, and could also be 'POST', 'PUT' etc. |
| 16 |
#' @param headers (optional) a named character vector specifying the HTTP |
| 17 |
#' request headers, for example: \cr |
| 18 |
#' `c(Authorization = "Bearer APIKEY", "Content-Type" = "text/plain")` \cr |
| 19 |
#' A non-character or non-named vector will be ignored. |
| 20 |
#' @param data (optional) request data to be submitted. Must be a character |
| 21 |
#' string or raw vector, and other objects are ignored. If a character vector, |
| 22 |
#' only the first element is taken. When supplying binary data, the |
| 23 |
#' appropriate 'Content-Type' header should be set to specify the binary |
| 24 |
#' format. |
| 25 |
#' @param response (optional) a character vector specifying the response headers |
| 26 |
#' to return e.g. `c("date", "server")`. These are case-insensitive and |
| 27 |
#' will return NULL if not present. Specify `TRUE` to return all response |
| 28 |
#' headers. A non-character vector will be ignored (other than `TRUE`). |
| 29 |
#' @param timeout (optional) integer value in milliseconds after which the |
| 30 |
#' transaction times out if not yet complete. |
| 31 |
#' @param tls (optional) applicable to secure HTTPS sites only, a client TLS |
| 32 |
#' Configuration object created by [tls_config()]. If missing or NULL, |
| 33 |
#' certificates are not validated. |
| 34 |
#' |
| 35 |
#' @return Named list of 3 elements: |
| 36 |
#' |
| 37 |
#' - `$status` - integer HTTP repsonse status code (200 - OK). Use |
| 38 |
#' [status_code()] for a translation of the meaning. |
| 39 |
#' - `$headers` - named list of response headers (all headers if |
| 40 |
#' `response = TRUE`, or those specified in `response`, or NULL otherwise). |
| 41 |
#' If the status code is within the 300 range, i.e. a redirect, the response |
| 42 |
#' header 'Location' is automatically appended to return the redirect address. |
| 43 |
#' - `$data` - the response body, as a character string if `convert = TRUE` (may |
| 44 |
#' be further parsed as html, json, xml etc. as required), or a raw byte |
| 45 |
#' vector if FALSE (use [writeBin()] to save as a file). |
| 46 |
#' |
| 47 |
#' @section Public Internet HTTPS: |
| 48 |
#' |
| 49 |
#' When making HTTPS requests over the public internet, you should supply a TLS |
| 50 |
#' configuration to validate server certificates. See [tls_config()] for |
| 51 |
#' details. |
| 52 |
#' |
| 53 |
#' @seealso [ncurl_aio()] for asynchronous http requests; [ncurl_session()] for |
| 54 |
#' persistent connections. |
| 55 |
#' |
| 56 |
#' @examples |
| 57 |
#' ncurl( |
| 58 |
#' "https://postman-echo.com/get", |
| 59 |
#' convert = FALSE, |
| 60 |
#' response = c("date", "content-type"), |
| 61 |
#' timeout = 1200L |
| 62 |
#' ) |
| 63 |
#' ncurl( |
| 64 |
#' "https://postman-echo.com/get", |
| 65 |
#' response = TRUE, |
| 66 |
#' timeout = 1200L |
| 67 |
#' ) |
| 68 |
#' ncurl( |
| 69 |
#' "https://postman-echo.com/put", |
| 70 |
#' method = "PUT", |
| 71 |
#' headers = c(Authorization = "Bearer APIKEY"), |
| 72 |
#' data = "hello world", |
| 73 |
#' timeout = 1500L |
| 74 |
#' ) |
| 75 |
#' ncurl( |
| 76 |
#' "https://postman-echo.com/post", |
| 77 |
#' method = "POST", |
| 78 |
#' headers = c(`Content-Type` = "application/json"), |
| 79 |
#' data = '{"key":"value"}', |
| 80 |
#' timeout = 1500L |
| 81 |
#' ) |
| 82 |
#' |
| 83 |
#' @export |
| 84 |
#' |
| 85 |
ncurl <- function( |
| 86 |
url, |
| 87 |
convert = TRUE, |
| 88 |
follow = FALSE, |
| 89 |
method = NULL, |
| 90 |
headers = NULL, |
| 91 |
data = NULL, |
| 92 |
response = NULL, |
| 93 |
timeout = NULL, |
| 94 |
tls = NULL |
| 95 |
) |
| 96 |
.Call(rnng_ncurl, url, convert, follow, method, headers, data, response, timeout, tls) |
| 97 |
|
| 98 |
#' ncurl Async |
| 99 |
#' |
| 100 |
#' nano cURL - a minimalist http(s) client - async edition. |
| 101 |
#' |
| 102 |
#' @inheritParams ncurl |
| 103 |
#' |
| 104 |
#' @return An 'ncurlAio' (object of class 'ncurlAio' and 'recvAio') (invisibly). |
| 105 |
#' The following elements may be accessed: |
| 106 |
#' |
| 107 |
#' - `$status` - integer HTTP repsonse status code (200 - OK). Use |
| 108 |
#' [status_code()] for a translation of the meaning. |
| 109 |
#' - `$headers` - named list of response headers (all headers if |
| 110 |
#' `response = TRUE`, or those specified in `response`, or NULL otherwise). |
| 111 |
#' If the status code is within the 300 range, i.e. a redirect, the response |
| 112 |
#' header 'Location' is automatically appended to return the redirect address. |
| 113 |
#' - `$data` - the response body, as a character string if `convert = TRUE` (may |
| 114 |
#' be further parsed as html, json, xml etc. as required), or a raw byte |
| 115 |
#' vector if FALSE (use [writeBin()] to save as a file). |
| 116 |
#' |
| 117 |
#' @inheritSection ncurl Public Internet HTTPS |
| 118 |
#' |
| 119 |
#' @section Promises: |
| 120 |
#' |
| 121 |
#' 'ncurlAio' may be used anywhere that accepts a 'promise' from the |
| 122 |
#' \CRANpkg{promises} package through the included `as.promise` method. |
| 123 |
#' |
| 124 |
#' The promises created are completely event-driven and non-polling. |
| 125 |
#' |
| 126 |
#' If a status code of 200 (OK) is returned then the promise is resolved with |
| 127 |
#' the reponse body, otherwise it is rejected with a translation of the status |
| 128 |
#' code or 'errorValue' as the case may be. |
| 129 |
#' |
| 130 |
#' @seealso [ncurl()] for synchronous http requests; [ncurl_session()] for |
| 131 |
#' persistent connections. |
| 132 |
#' |
| 133 |
#' @examples |
| 134 |
#' nc <- ncurl_aio( |
| 135 |
#' "https://postman-echo.com/get", |
| 136 |
#' response = c("date", "server"), |
| 137 |
#' timeout = 2000L |
| 138 |
#' ) |
| 139 |
#' call_aio(nc) |
| 140 |
#' nc$status |
| 141 |
#' nc$headers |
| 142 |
#' nc$data |
| 143 |
#' |
| 144 |
#' @examplesIf interactive() && requireNamespace("promises", quietly = TRUE) |
| 145 |
#' library(promises) |
| 146 |
#' p <- as.promise(nc) |
| 147 |
#' print(p) |
| 148 |
#' |
| 149 |
#' p2 <- ncurl_aio("https://postman-echo.com/get") %>% |
| 150 |
#' then(function(x) cat(x$data)) |
| 151 |
#' is.promise(p2) |
| 152 |
#' |
| 153 |
#' @export |
| 154 |
#' |
| 155 |
ncurl_aio <- function( |
| 156 |
url, |
| 157 |
convert = TRUE, |
| 158 |
method = NULL, |
| 159 |
headers = NULL, |
| 160 |
data = NULL, |
| 161 |
response = NULL, |
| 162 |
timeout = NULL, |
| 163 |
tls = NULL |
| 164 |
) |
| 165 |
data <- .Call(rnng_ncurl_aio, url, convert, method, headers, data, response, timeout, tls, environment()) |
| 166 |
|
| 167 |
#' ncurl Session |
| 168 |
#' |
| 169 |
#' nano cURL - a minimalist http(s) client. A session encapsulates a connection, |
| 170 |
#' along with all related parameters, and may be used to return data multiple |
| 171 |
#' times by repeatedly calling `transact()`, which transacts once over the |
| 172 |
#' connection. |
| 173 |
#' |
| 174 |
#' @inheritParams ncurl |
| 175 |
#' @param timeout (optional) integer value in milliseconds after which the |
| 176 |
#' connection and subsequent transact attempts time out. |
| 177 |
#' |
| 178 |
#' @return For `ncurl_session`: an 'ncurlSession' object if successful, or else |
| 179 |
#' an 'errorValue'. |
| 180 |
#' |
| 181 |
#' @inheritSection ncurl Public Internet HTTPS |
| 182 |
#' |
| 183 |
#' @seealso [ncurl()] for synchronous http requests; [ncurl_aio()] for |
| 184 |
#' asynchronous http requests. |
| 185 |
#' |
| 186 |
#' @examples |
| 187 |
#' s <- ncurl_session( |
| 188 |
#' "https://postman-echo.com/get", |
| 189 |
#' response = "date", |
| 190 |
#' timeout = 2000L |
| 191 |
#' ) |
| 192 |
#' s |
| 193 |
#' if (is_ncurl_session(s)) transact(s) |
| 194 |
#' if (is_ncurl_session(s)) close(s) |
| 195 |
#' |
| 196 |
#' @export |
| 197 |
#' |
| 198 |
ncurl_session <- function( |
| 199 |
url, |
| 200 |
convert = TRUE, |
| 201 |
method = NULL, |
| 202 |
headers = NULL, |
| 203 |
data = NULL, |
| 204 |
response = NULL, |
| 205 |
timeout = NULL, |
| 206 |
tls = NULL |
| 207 |
) |
| 208 |
.Call(rnng_ncurl_session, url, convert, method, headers, data, response, timeout, tls) |
| 209 |
|
| 210 |
#' @param session an 'ncurlSession' object. |
| 211 |
#' |
| 212 |
#' @return For `transact`: a named list of 3 elements: |
| 213 |
#' |
| 214 |
#' - `$status` - integer HTTP repsonse status code (200 - OK). Use |
| 215 |
#' [status_code()] for a translation of the meaning. |
| 216 |
#' - `$headers` - named list of response headers (all headers if |
| 217 |
#' `response = TRUE` was specified for the session, those specified in |
| 218 |
#' `response`, or NULL otherwise). |
| 219 |
#' - `$data` - the response body as a character string (if `convert = TRUE` was |
| 220 |
#' specified for the session), which may be further parsed as html, json, xml |
| 221 |
#' etc. as required, or else a raw byte vector, which may be saved as a file |
| 222 |
#' using [writeBin()]. |
| 223 |
#' |
| 224 |
#' @rdname ncurl_session |
| 225 |
#' @export |
| 226 |
#' |
| 227 |
transact <- function(session) .Call(rnng_ncurl_transact, session) |
| 228 |
|
| 229 |
#' @rdname close |
| 230 |
#' @method close ncurlSession |
| 231 |
#' @export |
| 232 |
#' |
| 233 |
close.ncurlSession <- function(con, ...) invisible(.Call(rnng_ncurl_session_close, con)) |
| 234 |
|
| 235 |
#' Make ncurlAio Promise |
| 236 |
#' |
| 237 |
#' Creates a 'promise' from an 'ncurlAio' object. |
| 238 |
#' |
| 239 |
#' This function is an S3 method for the generic `as.promise` for class |
| 240 |
#' 'ncurlAio'. |
| 241 |
#' |
| 242 |
#' Requires the \pkg{promises} package. |
| 243 |
#' |
| 244 |
#' Allows an 'ncurlAio' to be used with functions such as `promises::then()`, |
| 245 |
#' which schedules a function to run upon resolution of the Aio. |
| 246 |
#' |
| 247 |
#' The promise is resolved with a list of 'status', 'headers' and 'data' or |
| 248 |
#' rejected with the error translation if an NNG error is returned e.g. for an |
| 249 |
#' invalid address. |
| 250 |
#' |
| 251 |
#' @param x an object of class 'ncurlAio'. |
| 252 |
#' |
| 253 |
#' @return A 'promise' object. |
| 254 |
#' |
| 255 |
#' @exportS3Method promises::as.promise |
| 256 |
#' |
| 257 |
as.promise.ncurlAio <- function(x) { |
| 258 |
promise <- .subset2(x, "promise") |
| 259 |
|
| 260 |
if (is.null(promise)) { |
| 261 |
promise <- promises::promise( |
| 262 |
function(resolve, reject) { |
| 263 |
if (unresolved(x)) { |
| 264 |
.keep(x, environment()) |
| 265 |
} else { |
| 266 |
resolve( |
| 267 |
list( |
| 268 |
status = .subset2(x, "result"), |
| 269 |
headers = .subset2(x, "protocol"), |
| 270 |
data = .subset2(x, "value") |
| 271 |
) |
| 272 |
) |
| 273 |
} |
| 274 |
} |
| 275 |
)$then(onFulfilled = check_for_nng_error) |
| 276 |
`[[<-`(x, "promise", promise) |
| 277 |
} |
| 278 |
|
| 279 |
promise |
| 280 |
} |
| 281 |
|
| 282 |
#' @exportS3Method promises::is.promising |
| 283 |
#' |
| 284 |
is.promising.ncurlAio <- function(x) TRUE |
| 285 |
|
| 286 |
check_for_nng_error <- function(value, .visible) { |
| 287 |
value[["status"]] >= 100 || stop(nng_error(value[["status"]])) |
| 288 |
value |
| 289 |
} |