Title: | A Framework For Packaging {plumber} APIs |
---|---|
Description: | A framework for building robust {plumber} APIs. This package contains a series of tools to build {plumber} APIs as packages. |
Authors: | Antoine Languillaume [aut], Colin Fay [aut, cre] |
Maintainer: | Colin Fay <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9000 |
Built: | 2024-11-07 02:56:06 UTC |
Source: | https://github.com/ThinkR-open/mariobox |
Add or remove endpoints in a mariobox project.
add_endpoint(name, method = "GET", open = TRUE, pkg = ".") add_delete(name, open = TRUE, pkg = ".") add_get(name, open = TRUE, pkg = ".") add_patch(name, open = TRUE, pkg = ".") add_post(name, open = TRUE, pkg = ".") add_put(name, open = TRUE, pkg = ".") remove_endpoint(name, method, pkg = ".")
add_endpoint(name, method = "GET", open = TRUE, pkg = ".") add_delete(name, open = TRUE, pkg = ".") add_get(name, open = TRUE, pkg = ".") add_patch(name, open = TRUE, pkg = ".") add_post(name, open = TRUE, pkg = ".") add_put(name, open = TRUE, pkg = ".") remove_endpoint(name, method, pkg = ".")
name |
A character string. The name of the endpoint. |
method |
A character string. The name of the rest method. |
open |
A logical. Should the project be exported or not? |
pkg |
A character string. The path to package root. |
Nothing. Used for its side effect.
## Not run: # Create a new mariobox project path_pipo <- tempfile(pattern = "pipo") create_mariobox( path = path_pipo, open = FALSE ) # Add an endpoint add_endpoint( name = "allo", method = "GET", open = FALSE, pkg = path_pipo ) # Remove endpoint remove_endpoint( name = "allo", pkg = path_pipo ) ## End(Not run)
## Not run: # Create a new mariobox project path_pipo <- tempfile(pattern = "pipo") create_mariobox( path = path_pipo, open = FALSE ) # Add an endpoint add_endpoint( name = "allo", method = "GET", open = FALSE, pkg = path_pipo ) # Remove endpoint remove_endpoint( name = "allo", pkg = path_pipo ) ## End(Not run)
Build the plumber.R file
build_plumber_file(pkg = ".")
build_plumber_file(pkg = ".")
pkg |
path to package |
This function will create a prepopulated package with all the necessary elements to publish a plumber API as a package.
create_mariobox( path, open = TRUE, overwrite = FALSE, package_name = basename(path) )
create_mariobox( path, open = TRUE, overwrite = FALSE, package_name = basename(path) )
path |
A character string indicating the path (folder) where to create the package in. The folder name will also be used as the package name. |
open |
A logical. Should the new project be open? |
overwrite |
A logical. Should the already existing project be overwritten ? |
package_name |
Package name to use. By default, mariobox uses
|
The path to the project. Invisibly
path_pipo <- tempfile(pattern = "pipo") create_mariobox( path = path_pipo, open = FALSE )
path_pipo <- tempfile(pattern = "pipo") create_mariobox( path = path_pipo, open = FALSE )
This function is similar to stop()
and is used to automatically
distinguish server vs. client errors in a Plumber API.
http_error(status = 500L, message = NULL)
http_error(status = 500L, message = NULL)
status |
Integer, HTTP status code for the error (4xx or 5xx). |
message |
Character, the error message (can be a vector). |
A condition object of class "http_error"
.
## R's default error str(attr(try(stop("Hey, stop!")), "condition")) ## default status code 500 with default error message str(attr(try(http_error()), "condition")) ## custom status code with default error message str(attr(try(http_error(501L)), "condition")) ## custom status code for client error str(attr(try(http_error(400L, "Provide valid email address.")), "condition"))
## R's default error str(attr(try(stop("Hey, stop!")), "condition")) ## default status code 500 with default error message str(attr(try(http_error()), "condition")) ## custom status code with default error message str(attr(try(http_error(501L)), "condition")) ## custom status code for client error str(attr(try(http_error(400L, "Provide valid email address.")), "condition"))
Log Sys.time() and endpoint name
mario_log(method, name)
mario_log(method, name)
method |
HTTP verb |
name |
Name of the endpoint |
mario_log("GET", "health")
mario_log("GET", "health")
This function is similar to try()
and is called inside a Plumber API endpoint
definition. The function needs access to Plumbers response object.
Use http_error()
to automate the handling of HTTP status codes
and differentiate between server vs. client errors.
mario_try(res, expr, silent = TRUE, ...)
mario_try(res, expr, silent = TRUE, ...)
res |
The HTTP response object. |
expr |
An R expression to try. |
... |
Other arguments passed to |
message |
Logical, should the report of error messages be suppressed? |
The value of the expression if 'expr' is evaluated without error, but an invisible object of class ‘"try-error"’ containing the error message, and the error condition as the ‘"condition"’ attribute, if it fails.
Launch the API
new_api(yaml_file)
new_api(yaml_file)
yaml_file |
path to the yaml |