Skip to content

Commit

Permalink
Csv serializer (#520)
Browse files Browse the repository at this point in the history
Co-authored-by: pachamaltese <pachamaltese@users.noreply.github.com>
Co-authored-by: Barret Schloerke <barret@rstudio.com>
  • Loading branch information
3 people authored Jun 23, 2020
1 parent 03a34d1 commit e535922
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Suggests:
htmlwidgets,
visNetwork,
analogsea (>= 0.7.0),
later
later,
readr
Remotes:
rstudio/swagger
Collate:
Expand Down Expand Up @@ -68,6 +69,7 @@ Collate:
'post-parsers.R'
'response.R'
'serializer-content-type.R'
'serializer-csv.R'
'serializer-html.R'
'serializer-htmlwidget.R'
'serializer-rds.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export(plumb)
export(plumber)
export(randomCookieKey)
export(serializer_content_type)
export(serializer_csv)
export(serializer_html)
export(serializer_htmlwidget)
export(serializer_json)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
plumber (version)
--------------------------------------------------------------------------------

### New features

* CSV (UTF-8) serialization (@pachamaltese, #520)
* SVG (UTF-8) serialization (@pachamaltese, #398)

### Bug fixes

* Fix `plumb()` function when `plumb()`ing a directory so that `plumber.R` is
Expand Down
20 changes: 20 additions & 0 deletions R/serializer-csv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' @rdname serializers
#' @export
serializer_csv <- function(...) {
if (!requireNamespace("readr", quietly = TRUE)) {
stop("`readr` must be installed for `serializer_csv` to work")
}

function(val, req, res, errorHandler) {
tryCatch({
res$setHeader("Content-Type", "text/plain; charset=UTF-8")
res$body <- readr::format_csv(val, ...)
return(res$toResponse())
}, error = function(e){
errorHandler(req, res, e)
})
}
}

#' @include globals.R
.globals$serializers[["csv"]] <- serializer_csv
2 changes: 1 addition & 1 deletion man/plumber.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/serializers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tests/testthat/test-serializer-csv.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
context("CSV serializer")

test_that("CSV serializes properly", {
skip_if_not_installed("readr")

d <- data.frame(a=1, b=2, c="hi")
val <- serializer_csv()(d, data.frame(), PlumberResponse$new(), stop)
expect_equal(val$status, 200L)
expect_equal(val$headers$`Content-Type`, "text/plain; charset=UTF-8")
expect_equal(val$body, readr::format_csv(d))

d <- data.frame(a=1, b=2, c="hi", na=NA)
val <- serializer_csv()(d, data.frame(), PlumberResponse$new(), stop)
expect_equal(val$status, 200L)
expect_equal(val$headers$`Content-Type`, "text/plain; charset=UTF-8")
expect_equal(val$body, readr::format_csv(d, na = "NA"))

d <- data.frame(a=1, b=2, c="hi", na=NA)
val <- serializer_csv(na = 'string')(d, data.frame(), PlumberResponse$new(), stop)
expect_equal(val$status, 200L)
expect_equal(val$headers$`Content-Type`, "text/plain; charset=UTF-8")
expect_equal(val$body, readr::format_csv(d, na = 'string'))
})

test_that("Errors call error handler", {
skip_if_not_installed("readr")

errors <- 0
errHandler <- function(req, res, err){
errors <<- errors + 1
}

expect_equal(errors, 0)
serializer_csv()(parse(text="hi"), data.frame(), PlumberResponse$new("csv"), err = errHandler)
expect_equal(errors, 1)
})

0 comments on commit e535922

Please sign in to comment.