Skip to content

Commit

Permalink
Add wrapper for fileUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 5, 2012
1 parent 657a33d commit 7f427d5
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -54,3 +54,4 @@ Collate:
'url-query.r'
'http-patch.r'
'user-agent.r'
'upload-file.r'
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions R/http-post.r
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions R/http-put.r
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions 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)
}
2 changes: 1 addition & 1 deletion inst/tests/test-PUT-POST.r
Expand Up @@ -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", {
Expand Down
6 changes: 2 additions & 4 deletions man/POST.Rd
Expand Up @@ -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"))))
}

3 changes: 1 addition & 2 deletions man/PUT.Rd
Expand Up @@ -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"))))
}

18 changes: 18 additions & 0 deletions 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"))))
}

0 comments on commit 7f427d5

Please sign in to comment.