rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

Swagger JSON endpoint without live documentation? #221

Closed JHibbard closed 6 years ago

JHibbard commented 6 years ago

Is it possible to expose the JSON endpoint for the generated swagger spec without setting "swagger=TRUE" in run? I don't want users to access the default swagger live docs, but instead want them to use a custom docs page that requires the swagger JSON to work. Thanks! And this is a great project :)

trestletech commented 6 years ago

Two options:

  1. The simplest path is just to define a handler for @get /__swagger__/ in your API. That would override whatever plumber would try to set. So an API like:
#' @get /__swagger__/
function(){
  "nope"
}

would return (JSON-encoded) nope when a user visits /__swagger__/, but /swagger.json would still work.

  1. You could try to replicate the swagger-generating code that gets run when swagger=TRUE in run in your own API. https://github.com/trestletech/plumber/blob/master/R/plumber.R#L207-L246 This would require interacting with your plumber router programmatically (https://www.rplumber.io/docs/programmatic-usage.html), so it wouldn't be quite as simple.And I'm not 100% sure off the top of my head whether or not that code that I pointed you to uses any internal methods on the plumber router which might make it difficult for you to replicate externally.
JHibbard commented 6 years ago

Thanks for the quick response. Option 1 seems to fail, I'm guessing the default /swagger/ route gets added after the user api handlers are added. I tested this by adding the custom /swagger/ endpoint to the 04-mean-sum example provided in the plumber package.

trestletech commented 6 years ago

Huh. I was concerned about that but when I tested the API I pasted above, it actually worked for me.

What version are you on?

JHibbard commented 6 years ago

That was the problem. I installed the most recent version and it's working fine. Thanks for all your help :)