Permalink
Browse files

Add wrapper for fileUpload

  • Loading branch information...
hadley committed May 5, 2012
1 parent 657a33d commit 7f427d54255e0d62a799af5928d77e154db97754
Showing with 40 additions and 13 deletions.
  1. +1 −0 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +2 −4 R/http-post.r
  4. +1 −2 R/http-put.r
  5. +13 −0 R/upload-file.r
  6. +1 −1 inst/tests/test-PUT-POST.r
  7. +2 −4 man/POST.Rd
  8. +1 −2 man/PUT.Rd
  9. +18 −0 man/upload_file.Rd
View
@@ -54,3 +54,4 @@ Collate:
'url-query.r'
'http-patch.r'
'user-agent.r'
+ 'upload-file.r'
View
@@ -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)
View
@@ -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,
View
@@ -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,
View
@@ -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)
+}
@@ -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", {
View
@@ -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"))))
}
View
@@ -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"))))
}
View
@@ -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"))))
+}
+

0 comments on commit 7f427d5

Please sign in to comment.