How to best best structure larger microservices in Plumber - part 2

@Blair09M - the previous topic (link) was closed and I just briefly wanted to follow-up on the dynamic routes and parameters logic. I tried your solution but I'm getting a weird error:

Starting server to listen on port 5644
Error in if (type == "bool" || type == "logical") { : 
  missing value where TRUE/FALSE needed

I tried exactly your solution from before and that is the full code:

#* Check API readiness
#* @get /healthcheck
function(res) {
  
  res$status <- 200
  return("Give some data, I'm ready to predict")
  
}

#* Make a prediction for selected country
#* @param country Select model country code
#* @param check_it Should it be checked?
#* @post /<country>/predict
#* @serializer unboxedJSON
function(req, res, country, check_it = FALSE) {
    
    if (check_it == FALSE){
      checked <- "no"
    } else {
      checked <- "yes"
    }
    
    if (country == "au"){
      
      country <- "selected_au"
      
    } else if (country == "nl"){
      
      country <- "selected_nl"
      
    } else {
      stop("Invalid country parameters. Currently supported countries are: au and nl")
    }
    
    res$body <- list(
      checked,
      country
    )
}

Now, based on some online resources if I change the host to:

#* @post /<country::string>/predict

the API is able to start and swagger automatically generates such URL:

curl -X POST "http://127.0.0.1:5644/{country}/predict?check_it=TRUE&country=nl" -H  "accept: application/json"

which I find confusing because I though that country wouldn't need to be specified after '?' similarly to 'check_it' if it's part of the host. The API returns 404. What's your view on that?

Based on some experimentation that I did it seems that when a dynamic route is specified, you shouldn't make it a parameter. So this code sample runs for instance:

#* Make a prediction for selected country
#* @param check_it Should it be checked?
#* @post /<country>/predict
#* @serializer unboxedJSON
function(req, res, country, check_it = FALSE) {
    
    if (check_it == FALSE){
      checked <- "no"
    } else {
      checked <- "yes"
    }
    
    if (country == "au"){
      
      country <- "selected_au"
      
    } else if (country == "nl"){
      
      country <- "selected_nl"
      
    } else {
      stop("Invalid country parameters. Currently supported countries are: au and nl")
    }
    
    res$body <- list(
      checked,
      country
    )
}

But unfortunately the default swagger call won't work. I'm not sure if I'm doing something wrong or if there's a bug over there, hence I'll wait for you confirmation on that one.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.