-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathRestRserve.Rmd
More file actions
204 lines (158 loc) · 5.6 KB
/
Copy pathRestRserve.Rmd
File metadata and controls
204 lines (158 loc) · 5.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
---
title: "Quick Start Guide"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Quick Start Guide}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r, include = FALSE}
run_bg = function(expr) {
args = c("--vanilla", "-q")
expr_c = deparse(substitute(expr))
expr_c = paste(expr_c, collapse = "\n")
pid = sys::r_background(c(args, "-e", expr_c), std_out = TRUE, std_err = TRUE)
return(pid)
}
```
## Introduction
Suppose you've developed a very useful algorithm or statistical model and you need to integrate it with some external system. Nowadays HTTP became de facto a lingua-franca for this kind of tasks.
In this article we will demonstrate how to use RestRserve to build a basic REST API.
## Workflow overview
Generally RestRserve workflow consists of several major steps:
1. Create application with `Application$new()`
1. Create a function which follows RestRserve API:
- should take 2 arguments - `request` and `response` as an input. `request` and `response` are instances of `RestRserve::Request` and `RestRserve::Response`. It is **important to remember** that both `request` and `response` are **mutable** objects.
- should modify `response` in place or `raise()` exception in case of error
1. Register this function as a handler for an endpoint
1. Start application
## 1. Create application
```{r}
library(RestRserve)
app = Application$new()
```
## 2. Define logic
For simplicity we will use Fibonacci number calculation as an algorithm we want to expose.
```{r}
calc_fib = function(n) {
if (n < 0L) stop("n should be >= 0")
if (n == 0L) return(0L)
if (n == 1L || n == 2L) return(1L)
x = rep(1L, n)
for (i in 3L:n) {
x[[i]] = x[[i - 1]] + x[[i - 2]]
}
return(x[[n]])
}
```
Create function which will handle requests.
```{r, req_res}
fib_handler = function(.req, .res) {
n = as.integer(.req$parameters_query[["n"]])
if (length(n) == 0L || is.na(n)) {
raise(HTTPError$bad_request())
}
.res$set_body(as.character(calc_fib(n)))
.res$set_content_type("text/plain")
}
```
You may have noticed strange `.req` and `.res` argument names. Starting from `RestRserve` v0.4.0 these "reserved" names allows to benefit from autocomplete:
<img src="https://s3.eu-west-1.amazonaws.com/cdn.rexy.ai/assets/req-res.gif" width="640" style="vertical-align:bottom", alt="request-response autocomplete gif">
Technically `.req` and `.res` are just empty instances of `?Request` and `?Response` classes exported by `RestRserve` in order to make autocomplete work.
## 2. Register endpoint
```{r}
app$add_get(path = "/fib", FUN = fib_handler)
```
## 3. Test endpoints
Now we can test our application without starting it:
```{r}
request = Request$new(path = "/fib", parameters_query = list(n = 10))
response = app$process_request(request)
cat("Response status:", response$status)
cat("Response body:", response$body)
```
It is generally a good idea to write unit tests against application. One can use a common framework such as [tinytest](https://cran.r-project.org/package=tinytest).
## 4. Add OpenAPI description and Swagger UI
Generally it is a good idea to provide documentation along with the API. Convenient way to do that is to supply a [openapi specification](https://swagger.io/docs/specification/about/). This as simple as adding a yaml file as an additional endpoint:
```yaml
openapi: 3.0.1
info:
title: RestRserve OpenAPI
version: '1.0'
servers:
- url: /
paths:
/fib:
get:
description: Calculates Fibonacci number
parameters:
- name: "n"
description: "x for Fibonnacci number"
in: query
schema:
type: integer
example: 10
required: true
responses:
200:
description: API response
content:
text/plain:
schema:
type: string
example: 5
400:
description: Bad Request
```
```{r}
yaml_file = system.file("examples", "openapi", "openapi.yaml", package = "RestRserve")
app$add_openapi(path = "/openapi.yaml", file_path = yaml_file)
app$add_swagger_ui(path = "/doc", path_openapi = "/openapi.yaml", use_cdn = TRUE)
```
## 5. Start the app
Now all is ready and we can start application with Rserve backend. It will block R session and start listening for incoming requests.
```{r eval = FALSE}
backend = BackendRserve$new()
backend$start(app, http_port = 8080)
```
## 6. Test it
Send request to calculate fibonacci number:
```{bash eval = FALSE}
curl localhost:8080/fib?n=10
```
Check out a swagger UI in the browser: `http://localhost:8080/doc`
```{r echo = FALSE, eval = FALSE}
pid = run_bg({
library(RestRserve)
calc_fib = function(n) {
if (n < 0L) stop("n should be >= 0")
if (n == 0L) return(0L)
if (n == 1L || n == 2L) return(1L)
x = rep(1L, n)
for (i in 3L:n) x[[i]] = x[[i - 1]] + x[[i - 2]]
x[[n]]
}
fib_handler = function(.req, .res) {
n = as.integer(.req$parameters_query[["n"]])
if (length(n) == 0L || is.na(n)) {
raise(HTTPError$bad_request())
}
.res$set_body(as.character(calc_fib(n)))
.res$set_content_type("text/plain")
}
app = RestRserve::Application$new()
app$add_get(path = "/fib", FUN = fib_handler)
yaml_file = system.file("examples", "openapi", "openapi.yaml", package = "RestRserve")
app$add_openapi(path = "/openapi.yaml", file_path = yaml_file)
app$add_swagger_ui(path = "/doc", path_openapi = "/openapi.yaml", use_cdn = TRUE)
backend = BackendRserve$new()
backend$start(app, http_port = 8080)
})
tools::pskill(pid)
```