Programmatically use @apiVersion with plumber

229 Views Asked by At

I have written an API endpoint using plumber. I'm using the @apiVersion tag in the comments for documentation purposes and would like to add the version as string to the JSON object my endpoint is returning:

#' @apiTitle My cool API
#' @apiDescription This API does something.
#' @apiVersion 0.1

#* @serializer unboxedJSON
#* @post /call_me
function() {
    return(list(my_value = 42, api_version = "0.1"))
}

Is there a way to programmatically access the string in @apiVersion?

1

There are 1 best solutions below

1
On BEST ANSWER

apiVersion info is stored in the router private global settings.

pr$.__enclos_env__$private$globalSettings$info$version

They are the last information to be read when parsing a plumber file so they are not available until after the parsing of the file is done.

This is one way to do it using the apiSpec directly.

#' @apiTitle My cool API
#' @apiDescription This API does something.
#' @apiVersion 0.599

#' @plumber
function(pr) {
  assign("apiV",
         function() {
           pr$getApiSpec()$info$version
           # or the line below if endpoint response time is important
           # pr$.__enclos_env__$private$globalSettings$info$version
         },
         envir = pr$environment)
}

#* @serializer unboxedJSON
#* @post /call_me
function() {
  return(list(my_value = 42, api_version = apiV()))
}