-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from kennedymwavu/20-add-example-on-how-to-par…
…se-raw-json ft: add example on how to parse raw json
- Loading branch information
Showing
8 changed files
with
956 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules/ | ||
nodemon.json | ||
package-lock.json | ||
package.json | ||
.Renviron |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Parse raw JSON | ||
|
||
Say you have an API endpoint that expects raw JSON data. How do you parse that? | ||
|
||
This example answers that and is in a way related to the example on [csv & xlsx upload](../11_csv_xlsx_upload). | ||
|
||
This is a simple example revolving around how you can select columns and filter | ||
rows in the `iris` dataset when the request body is something like this: | ||
|
||
```json | ||
{ | ||
"cols": ["Sepal.Length", "Petal.Width", "Species"], | ||
"species": ["virginica", "setosa"] | ||
} | ||
``` | ||
|
||
# Run API | ||
|
||
1. `cd` into the `13_parse_raw_json/` dir: | ||
|
||
```bash | ||
cd 13_parse_raw_json/ | ||
``` | ||
1. Fire up R and restore package dependencies: | ||
|
||
```r | ||
renv::restore() | ||
``` | ||
1. `server.R` is the entry point. Run this command in the terminal to start the API: | ||
|
||
```bash | ||
Rscript server.R | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
box::use( | ||
ambiorix[Ambiorix], | ||
webutils[parse_http], | ||
) | ||
|
||
#' Handle POST at '/' | ||
#' | ||
#' @param req Request object. | ||
#' @param res Response object. | ||
#' @return `res$json()` | ||
#' @export | ||
home_post <- \(req, res) { | ||
content_type <- req$CONTENT_TYPE | ||
body <- req$rook.input$read() | ||
|
||
if (length(body) == 0L) { | ||
response <- list( | ||
code = 400L, | ||
msg = "Invalid request" | ||
) | ||
|
||
return( | ||
res$set_status(400L)$json(response) | ||
) | ||
} | ||
|
||
postdata <- parse_http(body, content_type) | ||
|
||
# filter & select as necessary: | ||
row_inds <- iris$Species %in% postdata$species | ||
col_inds <- colnames(iris) %in% postdata$cols | ||
data <- iris[row_inds, col_inds, drop = FALSE] | ||
|
||
response <- list( | ||
code = 200L, | ||
msg = "success", | ||
data = data | ||
) | ||
|
||
res$json(response) | ||
} | ||
|
||
app <- Ambiorix$new(port = 3000, host = "127.0.0.1") | ||
|
||
app$ | ||
post("/", home_post)$ | ||
start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.