diff --git a/DESCRIPTION b/DESCRIPTION index 4fe70c3c..8806ca54 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,3 +54,4 @@ Collate: 'url-query.r' 'http-patch.r' 'user-agent.r' + 'upload-file.r' diff --git a/NAMESPACE b/NAMESPACE index 8cfc0640..445b3a02 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(set_cookies) export(stop_for_status) export(text_content) export(timeout) +export(upload_file) export(use_proxy) export(user_agent) export(verbose) diff --git a/R/http-post.r b/R/http-post.r index ea3153d3..e431e11a 100644 --- a/R/http-post.r +++ b/R/http-post.r @@ -17,14 +17,12 @@ #' POST(b) #' POST(b, body = "A simple text string") #' POST(b, body = list(a = 1, b = 2, c = 3)) -#' -#' library(RCurl) -#' POST(b, body = list(a = 1, file = fileUpload(system.file("CITATION")))) +#' POST(b, body = list(a = 1, file = upload_file(system.file("CITATION")))) #' #' b2 <- "http://httpbin.org/post" #' POST(b2, body = "A simple text string") #' POST(b2, body = list(x = "A simple text string")) -#' POST(b2, body = list(y = fileUpload(system.file("CITATION")))) +#' POST(b2, body = list(y = upload_file(system.file("CITATION")))) POST <- function(url = NULL, config = list(), body = NULL, multipart = TRUE, ..., handle = NULL) { hu <- handle_url(handle, url, ...) make_request("post", hu$handle, hu$url, body = body, diff --git a/R/http-put.r b/R/http-put.r index aff8bd3c..90529c63 100644 --- a/R/http-put.r +++ b/R/http-put.r @@ -12,8 +12,7 @@ #' b2 <- "http://httpbin.org/put" #' PUT(b2, body = "A simple text string") #' PUT(b2, body = list(x = "A simple text string")) -#' library(RCurl) -#' PUT(b2, body = list(y = fileUpload(system.file("CITATION")))) +#' PUT(b2, body = list(y = upload_file(system.file("CITATION")))) PUT <- function(url = NULL, config = list(), body = NULL, multipart = TRUE, ..., handle = NULL) { hu <- handle_url(handle, url, ...) make_request("put", hu$handle, hu$url, body = body, diff --git a/R/upload-file.r b/R/upload-file.r new file mode 100644 index 00000000..acb2c9ea --- /dev/null +++ b/R/upload-file.r @@ -0,0 +1,13 @@ +#' Upload a file with \code{\link{POST}} or \code{\link{PUT}}. +#' +#' This is a tiny wrapper for \pkg{RCurl}'s \code{\link[RCurl]{fileUpload}}. +#' +#' @param x path to file +#' @export +#' @examples +#' POST("http://httpbin.org/post", +#' body = list(y = upload_file(system.file("CITATION")))) +upload_file <- function(path) { + stopifnot(is.character(path), length(path) == 1) + fileUpload(path) +} \ No newline at end of file diff --git a/inst/tests/test-PUT-POST.r b/inst/tests/test-PUT-POST.r index d6440748..5328f3ff 100644 --- a/inst/tests/test-PUT-POST.r +++ b/inst/tests/test-PUT-POST.r @@ -7,7 +7,7 @@ round_trip <- function(method, body = NULL, ...) { } methods <- c("POST", "PUT") -citation <- fileUpload(system.file("CITATION")) +citation <- upload_file(system.file("CITATION")) citation_val <- c(readLines(system.file("CITATION")), "") test_that("empty body gives empty data element", { diff --git a/man/POST.Rd b/man/POST.Rd index 061e8e68..b92ac9de 100644 --- a/man/POST.Rd +++ b/man/POST.Rd @@ -49,13 +49,11 @@ b <- new_bin() POST(b) POST(b, body = "A simple text string") POST(b, body = list(a = 1, b = 2, c = 3)) - -library(RCurl) -POST(b, body = list(a = 1, file = fileUpload(system.file("CITATION")))) +POST(b, body = list(a = 1, file = upload_file(system.file("CITATION")))) b2 <- "http://httpbin.org/post" POST(b2, body = "A simple text string") POST(b2, body = list(x = "A simple text string")) -POST(b2, body = list(y = fileUpload(system.file("CITATION")))) +POST(b2, body = list(y = upload_file(system.file("CITATION")))) } diff --git a/man/PUT.Rd b/man/PUT.Rd index 8ff3407b..d4a42b9c 100644 --- a/man/PUT.Rd +++ b/man/PUT.Rd @@ -54,7 +54,6 @@ PUT("http://httpbin.org/put") b2 <- "http://httpbin.org/put" PUT(b2, body = "A simple text string") PUT(b2, body = list(x = "A simple text string")) -library(RCurl) -PUT(b2, body = list(y = fileUpload(system.file("CITATION")))) +PUT(b2, body = list(y = upload_file(system.file("CITATION")))) } diff --git a/man/upload_file.Rd b/man/upload_file.Rd new file mode 100644 index 00000000..24be0678 --- /dev/null +++ b/man/upload_file.Rd @@ -0,0 +1,18 @@ +\name{upload_file} +\alias{upload_file} +\title{Upload a file with \code{\link{POST}} or \code{\link{PUT}}.} +\usage{ + upload_file(path) +} +\arguments{ + \item{x}{path to file} +} +\description{ + This is a tiny wrapper for \pkg{RCurl}'s + \code{\link[RCurl]{fileUpload}}. +} +\examples{ +POST("http://httpbin.org/post", + body = list(y = upload_file(system.file("CITATION")))) +} +