以编程方式将 @apiVersion 与管道工一起使用

Programmatically use @apiVersion with plumber

我已经使用 plumber 编写了一个 API 端点。我在注释中使用 @apiVersion 标记用于文档目的,并希望将版本作为字符串添加到我的端点返回的 JSON 对象中:

#' @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"))
}

有没有办法以编程方式访问@apiVersion 中的字符串?

apiVersion 信息存储在路由器私有全局设置中。

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

它们是解析管道工文件时要读取的最后信息,因此在文件解析完成之前它们不可用。

这是一种直接使用 apiSpec 的方法。

#' @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()))
}