Last active
November 7, 2022 19:08
-
-
Save randyphoa/35f75a57eb7c33dff394d1457a2f8b10 to your computer and use it in GitHub Desktop.
UI function that handles GET or POST requests
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
ui <- function(req) { | |
if (identical(req$REQUEST_METHOD, "GET")) { | |
htmlTemplate("index.html") | |
} else if (identical(req$REQUEST_METHOD, "POST")) { | |
body <- rawToChar(req$rook.input$read(-1)) | |
url <- stringr::str_interp("${CPD_URL}/ml/v4/deployments/${DEPLOYMENT_ID}/predictions") | |
query <- list(version=VERSION) | |
r <- POST( | |
url, | |
add_headers( | |
.headers = getHeaders() | |
), | |
query=query, | |
body=body, | |
encode="raw" | |
) | |
content <- jsonlite::toJSON( | |
list( | |
fields = unlist(httr::content(r)$predictions[[1]]$fields), | |
values = lapply(httr::content(r)$predictions[[1]]$values, unlist) | |
) | |
) | |
httpResponse( | |
status = 200L, | |
content_type = "application/json", | |
content = content | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment