Creates RestRserveApplication object. RestRserveApplication facilitates in turning user-supplied R code into high-performance REST API by allowing to easily register R functions for handling http-requests.
RestRserveApplicationR6Class object.
app = RestRserveApplication$new()
app$add_route(path = "/echo", method = "GET", FUN = function(request) {
RestRserve::create_response(body = request$query[[1]], content_type = "text/plain")
})
app$routes()
For usage details see Methods, Arguments and Examples sections.
$new()Constructor for RestRserveApplication. For the moment doesn't take any parameters.
$add_route(path, method, FUN, path_as_prefix = FALSE, ...)Adds endpoint
and register user-supplied R function as a handler.
User function FUN must return object of the class "RestRserveResponse"
which can be easily constructed with create_response. path_as_prefix at the moment used
mainly to help with serving static files. Probably in future it will be replaced with
other argument to perform URI template matching.
$add_get(path, FUN, ...)shorthand to add_route with GET method
$add_post(path, FUN, ...)shorthand to add_route with POST method
$add_static(path, file_path, content_type = NULL, ...)adds GET method to serve
file or directory at file_path. If content_type = NULL
then MIME code content_type will be inferred automatically (from file extension).
If it will be impossible to guess about file type then content_type will be set to
"application/octet-stream"
$run(http_port = 8001L, ...)starts RestRserve application from current R session.
http_port - http port for application.
... - key-value pairs of the Rserve configuration. If contains "http.port" then
http_port will be silently replaced with its value.
background - whether to try to launch in background process on UNIX systems. Ignored in windows.
$call_handler(request)Used internally, usually users don't need to call it. Calls handler function for a given request.
$routes()Lists all registered routes
$print_endpoints_summary()Prints all the registered routes with allowed methods
$set_404_handler(FUN)Register custom 404 response handler. FUN should be a
function which takes exactly one argument - request and return RestRserveResponse object.
Default handler is FUN = function(request) http_404_not_found(). See http_404_not_found for
details
$add_openapi(path = "/openapi.yaml", openapi = openapi_create())Adds endpoint to serve OpenAPI description of available methods.
$add_swagger_ui(path = "/swagger", path_openapi = "/openapi.yaml",
path_swagger_assets = "/__swagger__/",
file_path = tempfile(fileext = ".html"))Adds endpoint to show swagger-ui.
A RestRserveApplication. object
character of length 1. Should be valid path for example '/a/b/c'
character of length 1. At the moment one of "GET", "POST", "HEAD"
function which takes exactly one argument - request.
request R object returned by RestRserve:::parse_request() function.
Object corresponds to http-request and essentially request is a list with a fixed set of fields.
Representation of the "GET" request to "http://localhost:8001/somemethod?a=1&b=2" will look like:
= "/somepath", always character of length 1
= "GET", always character of length 1
= c("a" = "1", "b" = "2"), named character vector. Queiry parameters key-value pairs.
= NULL.
NULL if the http body is empty or zero length.
raw vector with a "content-type" attribute in all cases except URL encoded form (if specified in the headers)
named characeter vector in the case of a URL encoded form.
It will have the same shape as the query string (named string vector).
= "", always character of length 1
= c("a" = "1", "b" = "2"), named character vector. key-value pairs from http-header.
# NOT RUN {
echo_handler = function(request) {
RestRserve::create_response(body = request$query[[1]],
content_type = "text/plain",
headers = "Location: /echo",
status_code = 201L)
}
app = RestRserveApplication$new()
app$add_route(path = "/echo", method = "GET", FUN = echo_handler)
req = list(query = c("a" = "2"), method = "GET", path = "/echo")
answer = app$call_handler(request = req)
answer$body
# "2"
# }
Run the code above in your browser using DataLab