Skip to content

async = TRUE #48

Open
Open

Description

@jrosell

I want to use the programable API with mirai and the docs/website is not very clear. Is async=TRUE deprecated?

Activity

  1. thomasp85 commented on Sep 19, 2025

    @thomasp85
    Collaborator

    Can you point me to where you are finding it unclear? async = TRUE is certainly the way to do it.

  2. jrosell commented on Sep 19, 2025

    @jrosell
    Author

    Sorry, I saw I was using the CRAN version and not the development version. When I updated the version, the async parameter is available.

    However, I find myself with the need to refactor my code to use mirai promises instead and I don't see . See why here in my code (Furthermore I wish I know how to use S7 generics with mirai).

    stopifnot(requireNamespace("rlang"))
    rlang::check_installed("pak")
    
    pkgs <- rlang::chr(
      "rlang" = "rlang",
      "plumber2" = "posit-dev/plumber2",
      "S7",
      "jsonlite",
      "httr2",
      "mirai"
    )
    pak::pak(pkgs)
    libs <- ifelse(names(pkgs) == "", pkgs, names(pkgs))
    lapply(libs, library, quiet = TRUE, character.only = TRUE) |> invisible()
    
    daemons(5)
    
    User <- new_class(
      "User",
      properties = list(
        id = class_integer,
        name = class_character,
        age = class_integer
      )
    )
    
    method(convert, list(User, class_list)) <- function(from, to) {
      list(
        id = from@id,
        name = from@name,
        age = from@age
      )
    }
    
    jordi <- User(id = 1L, name = "jordi", age = 20L)
    convert(jordi, class_list)
    # $id
    # [1] 1
    # $name
    # [1] "jordi"
    # $age
    # [1] 20
    
    if (exists("app")) {
      app |> api_stop()
    }
    
    
    app <- api() |>
      api_get(
        path = "/",
        serializers = get_serializers("html"),
        async = TRUE,
        handler = function() {
          list(
            body = list(
              h1 = "Programmatic Plumber!"
            )
          )
        }
      ) |>
      api_post(
        path = "/users",
        parsers = get_parsers("json"),
        serializers = get_serializers("json"),
        async = TRUE,
        handler = function(body) {
          user <- rlang::exec(User, !!!body)
          convert(user, class_list)
        }
      ) |>
      api_run(showcase = FALSE, block = TRUE)
    
    request("http://127.0.0.1:8080/") |>
      req_perform() |>
      resp_body_string()
    # [1] "<html><body><h1>Programmatic Plumber!</h1></body></html>\n"
    
    request("http://127.0.0.1:8080/users") |>
      req_body_json(list(
        id = 1L,
        name = "Alice",
        age = 30L
      )) |>
      req_perform() |>
      resp_body_json()
    # Error in `req_perform()`:
    # ! HTTP 500 Internal Server Error.
    # <error/rlang_error>
    # Error in `callback()`:
    # ! Error in handler(client_id = id, query = query, body = body): object 'User' not found
    
    if (exists("app")) {
      app |> api_stop()
    }
    
    
    app <- api() |>
      api_post(
        path = "/users",
        parsers = get_parsers("json"),
        serializers = get_serializers("unboxedJSON"),
        handler = function(request, response, body) {
          mirai(
            {
              result_list <- tryCatch(
                {
                  user <- rlang::exec(User, !!!body)
                  user_list <- unclass(user) |> attributes() |> as.list()
                  user_list["S7_class"] <- NULL
                  user_list
                },
                error = \(e) {
                  list(
                    error = TRUE,
                    message = conditionMessage(e)
                  )
                }
              )
              result_list
            },
            User = User,
            body = body
          )
        }
      ) |>
      api_run(showcase = FALSE, block = TRUE)
    
    request("http://127.0.0.1:8080/users") |>
      req_body_json(list(
        id = 1L,
        name = "Alice",
        age = 30L
      )) |>
      req_perform() |>
      resp_body_json()
    # $id
    # [1] 1
    # $name
    # [1] "Alice"
    # $age
    # [1] 30
    
    request("http://127.0.0.1:8080/users") |>
      req_body_json(list(
        id = 1L,
        name = "Alice",
        age = "d"
      )) |>
      req_perform() |>
      resp_body_json()
    # $error
    # [1] TRUE
    # $message
    # [1] "<User> object properties are invalid:\n- @age must be <integer>, not <character>"
    

    Edit: I made a gist, and added SQLite too https://gist.github.com/jrosell/e4820231d3c234b2c9cd02bf41f17af1

  3. thomasp85 commented on Oct 6, 2025

    @thomasp85
    Collaborator

    What is the exact question, if any, now?

  4. jrosell commented on Oct 6, 2025

    @jrosell
    Author

    Sorry for not being clear.

    I think that the docs should include some mirai({... }) example when not using async=TRUE parameter.

  5. thomasp85 commented on Oct 20, 2025

    @thomasp85
    Collaborator

    Yeah, I should probably add that. FWIW your own issue is that you define an S7 class with specific types for its slots and then use the query params without casting them into the correct type.

  6. jrosell commented on Oct 20, 2025

    @jrosell
    Author

    Thanks. About my code: The error is intended. I want S7 type check fail when types are not ok.

  7. sounkou-bioinfo commented on Jan 26, 2026

    @sounkou-bioinfo

    Adding my two cents here. Manually returning a

    mirai({...}) |> as.promise()

    even in the case of non programmatic api usage can allow one to bypass the error of "request and response cannot be used in async endpoints" and get "async" endpoints that manipulate or check headers for auth/etc before passing computation to say mirai daemons

    and thanks @jrosell for the mirai::everywhere example, i had exactly a duckdb application using this at api setup !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions