-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
192 lines (143 loc) · 4.14 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = " ",
fig.path = "man/figures/README-",
out.width = "100%"
)
cat_mariobox_yaml <- function(path_pipo) {
cat(
paste0(
readLines(
file.path(path_pipo, "inst/mariobox.yml")
),
"\n"
)
)
}
```
# {mariobox}
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/mariobox)](https://CRAN.R-project.org/package=mariobox)
[![R-CMD-check](https://github.com/ThinkR-open/mariobox/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ThinkR-open/mariobox/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
[DISCLAIMER] This is a Work In Progress, please use at your own risk.
The goal of `{mariobox}` is to provide a framework for packaging {plumber} APIs.
Think of it as the "`{golem}` for `{plumber}`"
## Installation
You can install the development version of `{mariobox}` from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("ThinkR-Open/mariobox")
```
## Example
```{r}
library(mariobox)
```
### Creating a new {mariobox} project:
```{r}
path_pipo <- tempfile(pattern = "pipo")
# The create mariobox function will generate a new prefilled project
# with everything you need to get your started
create_mariobox(
path = path_pipo,
open = FALSE
)
```
By default, you'll find the following structure:
```{r}
fs::dir_tree(path_pipo)
```
The default API comes with one default endpoint, `/health`, which returns a 200 with the "ok" text.
We've made the choice to organise your API inside a YAML, so that you can be as close as possible to a package structure.
`{mariobox}` will then do a little bit of its magic and parse this YAML to build the `{plumber}` API.
```{r, echo=FALSE}
cat_mariobox_yaml(path_pipo)
```
### Add/Remove endpoints
`{mariobox}` comes with a series of functions to add endpoints to your app.
The generic one is `add_endpoint`, that allows you to pass any HTTP verb, and add_get and friends are wrappers around this function.
```{r}
add_endpoint(
name = "allo",
method = "GET",
open = FALSE,
pkg = path_pipo
)
```
This will produce the following R file:
```{r echo = FALSE}
readLines(
file.path(
path_pipo,
"R/get_allo.R"
)
) |> cat(sep = "\n")
```
```{r}
add_get(
name = "hey",
open = FALSE,
pkg = path_pipo
)
```
```{r}
fs::dir_tree(path_pipo)
```
The YALML is automatically updated:
```{r, echo=FALSE}
cat_mariobox_yaml(path_pipo)
```
```{r}
remove_endpoint(
name = "allo",
method = "GET",
pkg = path_pipo
)
```
```{r}
fs::dir_tree(path_pipo)
```
The YALML is automatically updated:
```{r, echo=FALSE}
cat_mariobox_yaml(path_pipo)
```
### About endpoint functions
All endpoint functions will be in the following format:
```{r eval = FALSE}
METHOD_NAME <- function(req, res) {
METHOD_NAME_f()
}
METHOD_NAME_f <- function() {
return("ok")
}
```
where `METHOD` is the HTTP verb and `name` is the name you've set in your function.
This format might seem weird, but the idea is to separate the concerns in the following format:
+ METHOD_NAME() will handle the http elements (login, headers..)
+ METHOD_NAME_f() will be a standard function returning data.
That way, you can handle the data manipulation function just like a plain standard one, test it, interact with it, etc, without having to care about the HTTP part.
## Running the API
In dev, you can launch the file at dev/run_dev.R.
```{r eval = FALSE}
source("dev/run_dev.R", echo = TRUE)
```
In production, the `run_api()` is in charge of running the API.
If you want to deloy to RStudio Connect, the `build_plumber_file()` function will create a `plumber.R` file at the root of your folder. You can deploy using this file.
```{r}
build_plumber_file(pkg = path_pipo)
```
This will produce the following file:
```{r echo = FALSE}
readLines(
file.path(
path_pipo,
"plumber.R"
)
) |> cat(sep = "\n")
```