【问题标题】:Programmatically use @apiVersion with plumber以编程方式将 @apiVersion 与管道工一起使用
【发布时间】:2020-11-26 11:25:53
【问题描述】:

我使用管道工编写了一个 API 端点。我在 cmets 中使用 @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 中的字符串?

【问题讨论】:

    标签: r plumber


    【解决方案1】:

    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()))
    }
    

    【讨论】:

    • 当然可以,为什么我要发布无效的代码:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多