Skip to content

Commit 9952eb2

Browse files
committed
option for ncurl to only return raw data
1 parent 6a6f6ee commit 9952eb2

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#### New Features
44

55
* New `stream()` interface exposes low-level byte stream functionality in the NNG library, intended for communicating with non-NNG endpoints, including but not limited to websocket servers.
6-
* `ncurl()` adds an 'async' option to perform HTTP requests asynchronously, returning immediately with a 'recvAio'. Also adds explicit arguments for HTTP method, headers (which takes a named list or character vector) and request data.
6+
* `ncurl()` adds an 'async' option to perform HTTP requests asynchronously, returning immediately with a 'recvAio'. Also adds arguments 'convert' to specify if conversion from raw bytes is required, as well as for HTTP method, headers (which takes a named list or character vector) and request data.
77
* New `messenger()` function implements a multi-threaded console-based messaging system using NNG's scalability protocols (currently as proof of concept).
88
* New `nano_init()` function intended to be called immediately after package load to set global options.
99

R/ncurl.R

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#'
77
#' @param url the URL address.
88
#' @param async [default FALSE] logical value whether to perform actions async.
9+
#' @param convert [default TRUE] logical value whether to attempt conversion of
10+
#' the received raw bytes to a character vetor.
911
#' @param method (optional) the HTTP method (defaults to 'GET' if not specified).
1012
#' @param headers (optional) a named list or character vector specifying the
1113
#' HTTP request headers e.g. \code{list(`Content-Type` = "text/plain")} or
@@ -16,9 +18,9 @@
1618
#' \itemize{
1719
#' \item{\code{$raw}} {- raw vector of the received resource (use
1820
#' \code{\link{writeBin}} to save to a file).}
19-
#' \item{\code{$data}} {- converted character string (if a recognised text
20-
#' format), or NULL otherwise. Other tools can be used to further parse this
21-
#' as html, json, xml etc. if required.}
21+
#' \item{\code{$data}} {- converted character string (if \code{'convert' = TRUE}
22+
#' and content is a recognised text format), or NULL otherwise. Other tools
23+
#' can be used to further parse this as html, json, xml etc. if required.}
2224
#' }
2325
#'
2426
#' Or else, if \code{async = TRUE}, a 'recvAio' (object of class 'recvAio').
@@ -40,12 +42,17 @@
4042
#'
4143
#' @examples
4244
#' ncurl("http://httpbin.org/get")
43-
#' ncurl("http://httpbin.org/put", ,"PUT", list(Authorization = "Bearer APIKEY"), "hello world")
44-
#' ncurl("http://httpbin.org/post", ,"POST", c(`Content-Type` = "application/json"),'{"k":"v"}')
45+
#' ncurl("http://httpbin.org/put",,,"PUT", list(Authorization = "Bearer APIKEY"), "hello world")
46+
#' ncurl("http://httpbin.org/post",,,"POST", c(`Content-Type` = "application/json"),'{"k":"v"}')
4547
#'
4648
#' @export
4749
#'
48-
ncurl <- function(url, async = FALSE, method = NULL, headers = NULL, data = NULL) {
50+
ncurl <- function(url,
51+
async = FALSE,
52+
convert = TRUE,
53+
method = NULL,
54+
headers = NULL,
55+
data = NULL) {
4956

5057
data <- if (!missing(data)) writeBin(object = data, con = raw())
5158

@@ -59,14 +66,17 @@ ncurl <- function(url, async = FALSE, method = NULL, headers = NULL, data = NULL
5966
continue %in% c("n", "N", "no", "NO") && return(invisible(res))
6067
return(ncurl(res))
6168
}
62-
data <- tryCatch(rawToChar(res), error = function(e) NULL)
69+
70+
data <- if (missing(convert) || isTRUE(convert)) tryCatch(rawToChar(res), error = function(e) NULL)
71+
6372
list(raw = res, data = data)
6473

6574
} else {
6675

6776
aio <- .Call(rnng_ncurl_aio, url, method, headers, data)
6877
is.integer(aio) && return(invisible(aio))
6978

79+
convert <- missing(convert) || isTRUE(convert)
7080
data <- raw <- NULL
7181
unresolv <- TRUE
7282
env <- new.env(hash = FALSE)
@@ -81,7 +91,7 @@ ncurl <- function(url, async = FALSE, method = NULL, headers = NULL, data = NULL
8191
return(invisible(res))
8292
}
8393
raw <<- res
84-
data <<- tryCatch(rawToChar(res), error = function(e) NULL)
94+
data <<- if (convert) tryCatch(rawToChar(res), error = function(e) NULL)
8595
aio <<- env[["aio"]] <<- NULL
8696
unresolv <<- FALSE
8797
}
@@ -98,7 +108,7 @@ ncurl <- function(url, async = FALSE, method = NULL, headers = NULL, data = NULL
98108
return(invisible(res))
99109
}
100110
raw <<- res
101-
data <<- tryCatch(rawToChar(res), error = function(e) NULL)
111+
data <<- if (convert) tryCatch(rawToChar(res), error = function(e) NULL)
102112
aio <<- env[["aio"]] <<- NULL
103113
unresolv <<- FALSE
104114
}

man/ncurl.Rd

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)