Skip to content

Instantly share code, notes, and snippets.

@psolymos
Last active April 3, 2023 19:33
Show Gist options
  • Save psolymos/284b43b8dd0583b33ca7fc7dcf71082b to your computer and use it in GitHub Desktop.
Save psolymos/284b43b8dd0583b33ca7fc7dcf71082b to your computer and use it in GitHub Desktop.

Revisions

  1. psolymos revised this gist Mar 27, 2023. 1 changed file with 37 additions and 0 deletions.
    37 changes: 37 additions & 0 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -171,6 +171,43 @@ s$start(listen = "127.0.0.1", port = 8080)
    # curl -d '["Friend"]' -H 'Content-Type: application/json' \
    # http://127.0.0.1:8080/custom/hello

    #shiny
    library(shiny)
    ui <- fluidPage(
    mainPanel(
    fluidRow(
    h2("Curl"),
    textOutput("url")
    ),
    fluidRow(
    h2("Message"),
    textOutput("msg")
    )
    )
    )
    server <- function(input, output, session) {
    x <- reactiveVal()
    url <- session$registerDataObj(
    name = "hello",
    data = list(),
    filter = function(data, req) {
    body <- jsonlite::fromJSON(rawToChar(req$rook.input$read()))
    x(paste0("Hello ", body, "!"))
    shiny::httpResponse(
    status = 200L,
    content_type = "application/json",
    content = jsonlite::toJSON(
    paste0("Hello ", body, "!")))
    }
    )
    output$url <- renderText({
    paste0("curl -d '[\"Friend\"]' -H 'Content-Type: application/json' http://127.0.0.1:8080/", url)
    })
    output$msg <- renderText({x()})
    }
    runApp(shinyApp(ui, server), port=8080)
    # go to http://127.0.0.1:8080 and copy the curl

    # check download stats
    library(ggplot2)
    library(dlstats)
  2. psolymos revised this gist Mar 27, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -167,9 +167,9 @@ app <- function(env){
    }
    s <- Rhttpd$new()
    s$add(RhttpdApp$new(app = app, name = "hello"))
    s$start(listen = "127.0.0.1", port = 5000)
    s$start(listen = "127.0.0.1", port = 8080)
    # curl -d '["Friend"]' -H 'Content-Type: application/json' \
    # http://127.0.0.1:5000/custom/hello
    # http://127.0.0.1:8080/custom/hello

    # check download stats
    library(ggplot2)
  3. psolymos revised this gist Mar 26, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion r-apis
    Original file line number Diff line number Diff line change
    @@ -156,6 +156,7 @@ Rserve::run.Rserve(http.port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # Rook
    library(Rook)
    app <- function(env){
    req <- Request$new(env)
    @@ -170,7 +171,6 @@ s$start(listen = "127.0.0.1", port = 5000)
    # curl -d '["Friend"]' -H 'Content-Type: application/json' \
    # http://127.0.0.1:5000/custom/hello


    # check download stats
    library(ggplot2)
    library(dlstats)
  4. psolymos revised this gist Mar 26, 2023. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -156,6 +156,21 @@ Rserve::run.Rserve(http.port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    library(Rook)
    app <- function(env){
    req <- Request$new(env)
    input <- fromJSON(rawToChar(req$body()$read()))
    res <- Response$new()
    res$write(toJSON(paste0("Hello ", input, "!")))
    res$finish()
    }
    s <- Rhttpd$new()
    s$add(RhttpdApp$new(app = app, name = "hello"))
    s$start(listen = "127.0.0.1", port = 5000)
    # curl -d '["Friend"]' -H 'Content-Type: application/json' \
    # http://127.0.0.1:5000/custom/hello


    # check download stats
    library(ggplot2)
    library(dlstats)
  5. psolymos revised this gist Mar 26, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -160,9 +160,9 @@ Rserve::run.Rserve(http.port = 8080)
    library(ggplot2)
    library(dlstats)

    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "shiny")
    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "Rook", "shiny")
    x <- cran_stats(pkg)
    ggplot(x, aes(x = end, y = downloads, group=package, color=package)) +
    geom_line() +
    scale_colour_brewer(palette = "Set1") +
    facet_wrap(~package) +
    theme_minimal()
  6. psolymos revised this gist Mar 26, 2023. 1 changed file with 3 additions and 19 deletions.
    22 changes: 3 additions & 19 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -156,29 +156,13 @@ Rserve::run.Rserve(http.port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # Rook
    library(Rook)
    app <- function(env){
    req <- Request$new(env)
    input <- fromJSON(rawToChar(req$body()$read()))
    res <- Response$new()
    res$write(toJSON(paste0("Hello ", input, "!")))
    res$finish()
    }
    s <- Rhttpd$new()
    s$add(RhttpdApp$new(app = app, name = "hello"))
    s$start(listen = "127.0.0.1", port = 5000)
    # curl -d '["Friend"]' -H 'Content-Type: application/json' \
    # http://127.0.0.1:5000/custom/hello

    # check download stats
    library(ggplot2)
    library(dlstats)

    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "Rook", "shiny")
    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "shiny")
    x <- cran_stats(pkg)
    ggplot(x, aes(x = end, y = downloads, group=package, color=package)) +
    geom_line() +
    scale_y_log10() +
    theme_minimal() +
    facet_wrap(~package)
    scale_colour_brewer(palette = "Set1") +
    theme_minimal()
  7. psolymos revised this gist Mar 26, 2023. 1 changed file with 19 additions and 3 deletions.
    22 changes: 19 additions & 3 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -156,13 +156,29 @@ Rserve::run.Rserve(http.port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # Rook
    library(Rook)
    app <- function(env){
    req <- Request$new(env)
    input <- fromJSON(rawToChar(req$body()$read()))
    res <- Response$new()
    res$write(toJSON(paste0("Hello ", input, "!")))
    res$finish()
    }
    s <- Rhttpd$new()
    s$add(RhttpdApp$new(app = app, name = "hello"))
    s$start(listen = "127.0.0.1", port = 5000)
    # curl -d '["Friend"]' -H 'Content-Type: application/json' \
    # http://127.0.0.1:5000/custom/hello

    # check download stats
    library(ggplot2)
    library(dlstats)

    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "shiny")
    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "Rook", "shiny")
    x <- cran_stats(pkg)
    ggplot(x, aes(x = end, y = downloads, group=package, color=package)) +
    geom_line() +
    scale_colour_brewer(palette = "Set1") +
    theme_minimal()
    scale_y_log10() +
    theme_minimal() +
    facet_wrap(~package)
  8. psolymos revised this gist Mar 21, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion r-apis
    Original file line number Diff line number Diff line change
    @@ -77,7 +77,7 @@ handle <- function(request, response, keys, ...) {
    }
    app <- Fire$new(host = "0.0.0.0", port = 8080L)
    route <- Route$new()
    route$add_handler('post', '/', handle)
    route$add_handler("post", "/", handle)
    router <- RouteStack$new()
    router$add_route(route, "hello")
    app$attach(router)
  9. psolymos revised this gist Mar 21, 2023. 1 changed file with 22 additions and 1 deletion.
    23 changes: 22 additions & 1 deletion r-apis
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ pr() |>
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # fiery
    # fiery - longer
    library(fiery)
    library(reqres)
    handle <- function(request) {
    @@ -64,6 +64,27 @@ app$ignite()
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # fiery - shorter with routr
    library(fiery)
    library(reqres)
    library(routr)
    handle <- function(request, response, keys, ...) {
    request$parse(json = parse_json())
    response$status <- 200L
    response$type <- "application/json; charset=utf-8"
    response$body <- jsonlite::toJSON(paste0("Hello ", request$body, "!"))
    return(FALSE)
    }
    app <- Fire$new(host = "0.0.0.0", port = 8080L)
    route <- Route$new()
    route$add_handler('post', '/', handle)
    router <- RouteStack$new()
    router$add_route(route, "hello")
    app$attach(router)
    app$ignite()
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # beakr
    library(beakr)
    handle <- function(req, res, err) {
  10. psolymos revised this gist Mar 19, 2023. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ httpuv::runServer(
    call = function(req) {
    list(
    status = 200L,
    headers = list('Content-Type' = 'application/json'),
    headers = list("Content-Type" = "application/json"),
    body = handle(req))
    }
    )
    @@ -39,10 +39,10 @@ pr() |>
    library(fiery)
    library(reqres)
    handle <- function(request) {
    jsonlite::toJSON(paste0('Hello ', request$body, '!'))
    jsonlite::toJSON(paste0("Hello ", request$body, "!"))
    }
    app <- Fire$new(host = '0.0.0.0', port = 8080L)
    app$on('request', function(server, id, request, ...) {
    app$on("request", function(server, id, request, ...) {
    OK <- request$parse(json = parse_json())
    response <- request$respond()
    if (OK) {
    @@ -58,7 +58,7 @@ app$on('request', function(server, id, request, ...) {
    response$body <- jsonlite::toJSON("Error: wrong input")
    response$status <- 400L
    }
    response$type <- 'application/json; charset=utf-8'
    response$type <- "application/json; charset=utf-8"
    })
    app$ignite()
    # curl http://localhost:8080/ -d '["Friend"]' \
    @@ -89,7 +89,7 @@ library(ambiorix)
    options(ambiorix.host = "0.0.0.0", ambiorix.port = 8080)
    app <- Ambiorix$new()
    handle <- function(body) {
    paste0('Hello ', body, '!')
    paste0("Hello ", body, "!")
    }
    app$post("/", function(req, res){
    res$json(handle(parse_json(req)))
    @@ -136,8 +136,8 @@ Rserve::run.Rserve(http.port = 8080)
    # -H 'Content-Type: application/json'

    # check download stats
    library("ggplot2")
    library("dlstats")
    library(ggplot2)
    library(dlstats)

    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "shiny")
    x <- cran_stats(pkg)
  11. psolymos revised this gist Mar 19, 2023. No changes.
  12. psolymos created this gist Mar 19, 2023.
    147 changes: 147 additions & 0 deletions r-apis
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    # httpuv
    library(httpuv)
    handle <- function(req) {
    input <- req[["rook.input"]]
    postdata <- input$read_lines()
    jsonlite::toJSON(paste0("Hello ",
    jsonlite::fromJSON(paste(postdata)), "!"))
    }
    httpuv::runServer(
    host = "0.0.0.0",
    port = 8080,
    app = list(
    call = function(req) {
    list(
    status = 200L,
    headers = list('Content-Type' = 'application/json'),
    body = handle(req))
    }
    )
    )
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # plumber
    library(plumber) # load plumber
    handle <- function(req) {
    paste0("Hello ", jsonlite::fromJSON(paste(req$postBody)), "!")
    }
    pr() |>
    pr_post(
    "/",
    handler = handle,
    serializer = serializer_unboxed_json()) |>
    pr_run(host = "0.0.0.0", port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # fiery
    library(fiery)
    library(reqres)
    handle <- function(request) {
    jsonlite::toJSON(paste0('Hello ', request$body, '!'))
    }
    app <- Fire$new(host = '0.0.0.0', port = 8080L)
    app$on('request', function(server, id, request, ...) {
    OK <- request$parse(json = parse_json())
    response <- request$respond()
    if (OK) {
    result <- try(handle(request))
    if (inherits(result, "try-error")) {
    response$body <- jsonlite::toJSON(result)
    response$status <- 400L
    } else {
    response$body <- result
    response$status <- 200L
    }
    } else {
    response$body <- jsonlite::toJSON("Error: wrong input")
    response$status <- 400L
    }
    response$type <- 'application/json; charset=utf-8'
    })
    app$ignite()
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # beakr
    library(beakr)
    handle <- function(req, res, err) {
    paste0("Hello ", jsonlite::fromJSON(paste(req$body)), "!")
    }
    newBeakr() |>
    httpPOST(
    path = "/",
    decorate(
    FUN = handle,
    content_type = "application/json"
    )
    ) |>
    handleErrors() |>
    listen(host = "0.0.0.0", port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # ambiorix
    # not on CRAN (archived)
    # remotes::install_github("devOpifex/ambiorix")
    library(ambiorix)
    options(ambiorix.host = "0.0.0.0", ambiorix.port = 8080)
    app <- Ambiorix$new()
    handle <- function(body) {
    paste0('Hello ', body, '!')
    }
    app$post("/", function(req, res){
    res$json(handle(parse_json(req)))
    })
    app$start(open = FALSE)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # RestRserve
    library(RestRserve)
    handle = function(.req, .res) {
    .res$set_body(paste0("Hello ", .req$body, "!"))
    }
    app = Application$new(
    content_type = "application/json")
    app$add_post(
    path = "/",
    FUN = handle)
    backend = BackendRserve$new()
    backend$start(app, http_port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # opencpu
    library(opencpu)
    ocpu_start_server(
    port = 8080,
    root = "/",
    workers = 1,
    preload = NULL,
    on_startup = NULL,
    no_cache = FALSE)
    # curl http://localhost:8080/library/base/R/paste/json \
    # -H 'Content-Type: application/json' \
    # -d '{"x":["Hello","Friend","!"],"collapse":" "}'

    # Rserve
    library(Rserve)
    .http.request <- function(url, query, body, headers) {
    paste0("Hello ", jsonlite::fromJSON(rawToChar(body)), "!")
    }
    Rserve::run.Rserve(http.port = 8080)
    # curl http://localhost:8080/ -d '["Friend"]' \
    # -H 'Content-Type: application/json'

    # check download stats
    library("ggplot2")
    library("dlstats")

    pkg <- c("httpuv", "opencpu", "plumber", "fiery", "beakr", "RestRserve", "ambiorix", "Rserve", "shiny")
    x <- cran_stats(pkg)
    ggplot(x, aes(x = end, y = downloads, group=package, color=package)) +
    geom_line() +
    scale_colour_brewer(palette = "Set1") +
    theme_minimal()