Learn R Programming

RestRserve (version 0.1.5)

RestRserveApplication: Creates RestRserveApplication.

Description

Creates RestRserveApplication object. RestRserveApplication converts user-supplied R code into high-performance REST API by allowing to easily register R functions for handling http-requests.

Usage

RestRserveApplication

Format

R6Class object.

Fields

logger

Logger instance. Can be replaced/manipulated with corresponding Logger methods.

Usage

For usage details see Methods, Arguments and Examples sections.

  • app = RestRserveApplication$new()

  • app$add_route(path = "/echo", method = "GET", FUN = function(request, response) { response$body = request$query[[1]] response$content_type = "text/plain" forward() })

  • app$routes()

Methods

$new(middleware = list(),content_type = "application/json", ...)

Constructor for RestRserveApplication. Sets middleware ( list of RestRserveMiddleware) and content_type - default response format.

$add_route(path, method, FUN, ...)

Adds endpoint and register user-supplied R function as a handler. User function FUN must take two arguments: first is request and second is response. The goal of the user function is to modify response and call RestRserve::forward() at the end. (which means return RestRserveForward object). Both response and request objects modified in-place and internally passed further to RestRserve execution pipeline.

$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, ..., background = FALSE)

starts RestRserve application from current R session. http_port - http port for application. Negative values (such as -1) means not to expose plain http. ... - 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 on 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

$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.

Arguments

app

A RestRserveApplication object

path

character of length 1. Should be valid path for example '/a/b/c'. If it is named character vector with name equal to "prefix" then all the endopoints which begin with the path will call corresponding handler.

method

character of length 1. At the moment one of ("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")

FUN

function which must take 2 arguments - request, response objects. See RestRserveRequest and RestRserveResponse for details.