0

Let say there is R code for REST API based on using the "plumber" package. Here is a function from it.

#' Date of sale
#' @get /date_of_sale
#' @param auth_key Auth key
#' @param user_id User ID
#' @example https://company.com/date_of_sale?auth_key=12345&user_id=6789
function(auth_key, user_id) {
   # ...
}

Let say there is another R script that uses API request to this server like

  api_string <- "https://company.com/date_of_sale?auth_key=12345&user_id=6789"
  date_of_sale <- jsonlite::fromJSON(api_string)

Is it possible to get a description of the parameters "auth_key" and "user_id" in the second script to have a full explanation of what means each parameter? For example, get for "auth_key" a string "Auth key"? Or how it will be possible to get access to function "date_of_sale" metadata at all?

Thanks for any ideas?

Andrii
  • 2,843
  • 27
  • 33

2 Answers2

1

Using a file plumber.R with content as you provided. Assuming it is in the working directory.

In R

pr_read <- plumber::pr("plumber.R")
spec <- pr_read$getApiSpec()
spec$paths$`/date_of_sale`$get$parameters

spec is an R list with the same structure as an OpenAPI document.

If you do not have access to API plumber file but your API is running somewhere you have access to.

spec <- jsonlite::fromJSON("{api_server}/openapi.json", simplifyDataFrame = FALSE)
spec$paths$`/date_of_sale`$get$parameters

Again this follows the OpenAPI documentation standards.

Bruno Tremblay
  • 756
  • 4
  • 9
0

Since this is an API GET request you can not access the description of the variable unless you explicitly include it in the API response.

I've learned some R script purely for this question, my guess is this is how you've prepared your JSON API response.

You can do something like this in your JSON request.

library(rjson)
auth_key <- "some_key";
user_id <- "340";
x <- list(auth_key = list(
        type = typeof(auth_key),
        lenght = length(auth_key),
        attributes = attributes(auth_key),
        string = auth_key
      ),
      user_id = list(
        type = typeof(user_id),
        lenght = length(user_id),
        attributes = attributes(user_id),
        string = user_id
      ),
      data = "your_data"
    );

#x
json <- toJSON(x, indent=0, method="C" )

fromJSON( json )

You might want to look a these. https://stat.ethz.ch/R-manual/R-devel/library/base/html/typeof.html https://ramnathv.github.io/pycon2014-r/learn/structures.html https://rdrr.io/cran/rjson/man/toJSON.html https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/attributes