This class encapsulates all of the logic of a plumber2 api, and is what gets passed around in the functional api of plumber2. The Plumber2 class is a subclass of the fiery::Fire class. Please consult the documentation for this for additional information on what this type of server is capable of. Note that the Plumber2 objects are reference objects, meaning that any change to it will change all instances of the object.
Initialization
A new 'Plumber2'-object is initialized using the new()
method on the
generator:
api <- Plumber2$new() |
However, most users will use the functional api of the package and thus
construct one using api()
Super class
fiery::Fire
-> Plumber2
Active bindings
request_router
The router handling requests
header_router
The router handling partial requests (the request will pass through this router prior to reading in the body)
doc_type
The type of API documentation to generate. Can be either
"rapidoc"
(the default),"redoc"
,"swagger"
, orNULL
(equating to not generating API docs)doc_path
The URL path to serve the api documentation from
Methods
Inherited methods
fiery::Fire$async()
fiery::Fire$attach()
fiery::Fire$close_ws_con()
fiery::Fire$delay()
fiery::Fire$exclude_static()
fiery::Fire$extinguish()
fiery::Fire$get_data()
fiery::Fire$has_plugin()
fiery::Fire$header()
fiery::Fire$is_running()
fiery::Fire$log()
fiery::Fire$off()
fiery::Fire$on()
fiery::Fire$reignite()
fiery::Fire$remove_async()
fiery::Fire$remove_data()
fiery::Fire$remove_delay()
fiery::Fire$remove_time()
fiery::Fire$resume()
fiery::Fire$safe_call()
fiery::Fire$send()
fiery::Fire$serve_static()
fiery::Fire$set_client_id_converter()
fiery::Fire$set_data()
fiery::Fire$set_logger()
fiery::Fire$start()
fiery::Fire$stop()
fiery::Fire$test_header()
fiery::Fire$test_message()
fiery::Fire$test_request()
fiery::Fire$test_websocket()
fiery::Fire$time()
fiery::Fire$trigger()
Method new()
Create a new Plumber2
api
Usage
Plumber2$new(
host = get_opts("host", "127.0.0.1"),
port = get_opts("port", 8080),
doc_type = get_opts("docType", "rapidoc"),
doc_path = get_opts("docPath", "__docs__"),
reject_missing_methods = get_opts("rejectMissingMethods", FALSE),
ignore_trailing_slash = get_opts("ignoreTrailingSlash", TRUE),
max_request_size = get_opts("maxRequestSize"),
shared_secret = get_opts("sharedSecret"),
compression_limit = get_opts("compressionLimit", 1000),
default_async = get_opts("async", "future"),
env = caller_env()
)
Arguments
host
A string overriding the default host
port
An port number overriding the default port
doc_type
The type of API documentation to generate. Can be either
"rapidoc"
(the default),"redoc"
,"swagger"
, orNULL
(equating to not generating API docs)doc_path
The URL path to serve the api documentation from
reject_missing_methods
Should requests to paths that doesn't have a handler for the specific method automatically be rejected with a 405 Method Not Allowed response with the correct Allow header informing the client of the implemented methods. Assigning a handler to
"any"
for the same path at a later point will overwrite this functionality. Be aware that setting this toTRUE
will prevent the request from falling through to other routes that might have a matching method and path. This setting anly affects handlers on the request router.ignore_trailing_slash
Logical. Should the trailing slash of a path be ignored when adding handlers and handling requests. Setting this will not change the request or the path associated with but just ensure that both
path/to/ressource
andpath/to/ressource/
ends up in the same handler. This setting will only affect routes that are created automatically.max_request_size
Sets a maximum size of request bodies. Setting this will add a handler to the header router that automatically rejects requests based on their
Content-Length
headershared_secret
Assigns a shared secret to the api. Setting this will add a handler to the header router that automatically rejects requests if their
Plumber-Shared-Secret
header doesn't contain the same value. Be aware that this type of authentication is very weak. Never put the shared secret in plain text but rely on e.g. the keyring package for storage. Even so, if requests are send over HTTP (not HTTPS) then anyone can read the secret and use itcompression_limit
The size threshold in bytes for trying to compress the response body (it is still dependant on content negotiation)
default_async
The default evaluator to use for async request handling
env
An environment that will be used as the default execution environment for the API
Method format()
Human readable description of the api object
Method ignite()
Begin running the server. Will trigger the start
event
Arguments
block
Should the console be blocked while running (alternative is to run in the background)
showcase
Should the default browser open up at the server address. If
TRUE
then a browser opens at the root of the api, unless the api contains OpenAPI documentation in which case it will open at that location. If a string the string is used as a path to add to the root before opening....
Arguments passed on to the
start
handlersilent
Should startup messaging by silenced
Method add_route()
Add a new route to either the request or header router
Arguments
name
The name of the route to add. If a route is already present with this name then the provided route (if any) is merged into it
route
The route to add. If
NULL
a new empty route will be createdheader
Logical. Should the route be added to the header router?
after
The location to place the new route on the stack.
NULL
will place it at the end. Will not have an effect if a route with the given name already exists.
Method request_handler()
Add a handler to a request. See api_request_handlers for detailed information
Usage
Plumber2$request_handler(
method,
path,
handler,
serializers,
parsers = NULL,
use_strict_serializer = FALSE,
download = FALSE,
async = FALSE,
doc = NULL,
route = NULL,
header = FALSE
)
Arguments
method
The HTTP method to attach the handler to
path
A string giving the path the handler responds to.
handler
A handler function to call when a request is matched to the path
serializers
A named list of serializers that can be used to format the response before sending it back to the client. Which one is selected is based on the request
Accept
headerparsers
A named list of parsers that can be used to parse the request body before passing it in as the
body
argument. Which one is selected is based on the requestContent-Type
headeruse_strict_serializer
By default, if a serializer that respects the requests
Accept
header cannot be found, then the first of the provided ones are used. Setting this toTRUE
will instead send back a406 Not Acceptable
responsedownload
Should the response mark itself for download instead of being shown inline? Setting this to
TRUE
will set theContent-Disposition
header in the response toattachment
. Setting it to a string is equivalent to setting it toTRUE
but will in addition also set the default filename of the download to the string valueasync
If
FALSE
create a regular handler. IfTRUE
, use the default async evaluator to create an async handler. If a string, the async evaluator registered to that name is used. If a function is provided then this is used as the async evaluatordoc
OpenAPI documentation for the handler. Will be added to the
paths$<handler_path>$<handler_method>
portion of the API.route
The route this handler should be added to. Defaults to the last route in the stack. If the route does not exist it will be created as the last route in the stack.
header
Logical. Should the handler be added to the header router
Method message_handler()
Add a handler to a WebSocket message. See api_message for detailed information
Arguments
handler
A function conforming to the specifications laid out in
api_message()
async
If
FALSE
create a regular handler. IfTRUE
, use the default async evaluator to create an async handler. If a string, the async evaluator registered to that name is used. If a function is provided then this is used as the async evaluator
Method redirect()
Add a redirect to the header router. Depending on the value
of permanent
it will respond with a 307 Temporary Redirect or 308
Permanent Redirect. from
and to
can contain path parameters and
wildcards which will be matched between the two to construct the correct
redirect path.
Arguments
method
The HTTP method the redirect should respond to
from
The path the redirect should respond to
to
The path/URL to redirect the incoming request towards. After resolving any path parameters and wildcards it will be used in the
Location
headerpermanent
Logical. Is the redirect considered permanent or temporary? Determines the type of redirct status code to use