-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstage_4.R
261 lines (214 loc) · 7.18 KB
/
stage_4.R
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
library(tidyverse)
library(glue)
library(plumber)
library(jose)
options("plumber.port" = 6312)
# Error handling ----------------------------------------------------------
# Custom error handling
# https://web.archive.org/web/20240110015732/https://unconj.ca/blog/structured-errors-in-plumber-apis.html
# Helper function to replace stop()
api_error <- function(message, status) {
err <- structure(
list(message = message, status = status),
class = c("api_error", "error", "condition")
)
signalCondition(err)
}
# General error handling function
error_handler <- function(req, res, err) {
if (!inherits(err, "api_error")) {
res$status <- 500
res$body <- jsonlite::toJSON(auto_unbox = TRUE, list(
status = 500,
message = "Internal server error."
))
res$setHeader("content-type", "application/json") # Make this JSON
# Print the internal error so we can see it from the server side. A more
# robust implementation would use proper logging.
print(err)
} else {
# We know that the message is intended to be user-facing.
res$status <- err$status
res$body <- jsonlite::toJSON(auto_unbox = TRUE, list(
status = err$status,
message = err$message
))
res$setHeader("content-type", "application/json") # Make this JSON
}
res
}
# API details -------------------------------------------------------------
#* @apiTitle Plumber Example API
#* @apiDescription Fun times with R and plumber and APIs
#* @apiContact list(name = "Andrew Heiss", url = "https://www.andrewheiss.com/")
#* @apiLicense list(name = "MIT", url = "https://opensource.org/license/mit/")
#* @apiVersion 0.1.0
#* @apiTag Data Access different data things
#* @apiTag Debugging Endpoints for testing to make sure things are working
#* @apiTag Authentication Endpoints for illustrating authentication
# Overall plumber pipeline
#* @plumber
function(pr) {
# Use custom error handler
pr |> pr_set_error(error_handler)
}
#* Enable Cross-origin Resource Sharing
#* @filter cors
# This is more complex than what's in the official documentation
# (https://www.rplumber.io/articles/security.html#cross-origin-resource-sharing-cors)
# because it correctly allows requests to come from http://localhost too
# (via https://github.com/rstudio/plumber/issues/66#issuecomment-418660334)
cors <- function(req, res) {
res$setHeader("Access-Control-Allow-Origin", "*")
if (req$REQUEST_METHOD == "OPTIONS") {
res$setHeader("Access-Control-Allow-Methods", "*")
res$setHeader("Access-Control-Allow-Headers", req$HTTP_ACCESS_CONTROL_REQUEST_HEADERS)
res$status <- 200
return(list())
} else {
plumber::forward()
}
}
# Endpoints ---------------------------------------------------------------
#* Plot a fancy histogram
#* @tag Debugging
#* @serializer png list(width = 500, height = 300)
#* @get /plot
function(n = 100) {
# Make sure n isn't ever too big so that the server doesn't crash
if (n >= 10000) {
api_error("`n` is too big. Use a number less than 10,000.", 400)
}
my_plot <- ggplot(
data = data.frame(x = rnorm(n)),
aes(x = x)
) +
geom_histogram(fill = "darkred", color = "white") +
labs(title = glue("A histogram of {n} random numbers")) +
theme_bw()
print(my_plot)
}
#* Return clean penguins data
#* @tag Data
#* @seralizer json
#* @get /penguins
function() {
library(palmerpenguins)
penguins_clean <- penguins |> dplyr::filter(!is.na(sex))
list(
extra_details = "All missing values have been removed. You're welcome!",
data = penguins_clean
)
}
#* Get and clean Goodreads data
#* @tag Data
#* @serializer json
#* @get /books
function(year = 2024) {
library(googlesheets4)
gs4_deauth() # The sheet is public so there's no need to log in
local_gs4_quiet() # Turn off the googlesheets messages
books_raw <- read_sheet("https://docs.google.com/spreadsheets/d/1oQqX4G4CJaa7cgfsEW4LeorcQwxVeYe0Q83WrJbcN6Y/edit#gid=0")
books_clean <- books_raw |>
# Convert the timestamp to an actual date
mutate(timestamp = dmy_hms(user_read_at)) |>
# Make some extra helper columns
mutate(
read_year = year(timestamp),
read_month = month(timestamp),
read_month_fct = month(timestamp, label = TRUE, abbr = FALSE)
) |>
# Only keep books for the specified year
filter(read_year == as.integer(year)) |>
# Only include a few columns
select(
timestamp = user_read_at,
book_title = title,
book_author = author_name,
rating = user_rating,
read_year, read_month, read_month_fct
)
# Find the count of all the books
total <- books_clean |> nrow()
# Calculate the number of books by month
monthly_count <- books_clean |>
group_by(read_month_fct, .drop = FALSE) |>
summarize(count = n())
# Return the total count, a count by month, and the full data
return(
list(
count = total,
monthly_count = monthly_count,
full_data = books_clean
)
)
}
# Authentication endpoints ------------------------------------------------
# Load secret variables as environment variables. Alternatively, use .Renviron for this.
source("secrets.R")
#* Super unsafe secret thing
#* @tag Authentication
#* @seralizer text
#* @get /secret_data
function(username, password) {
if (username == "your_name" & password == "secret") {
return("Here's some secret data")
} else {
api_error("Wrong username or password!", 401)
}
}
#* Slightly better secret thing
#* @tag Authentication
#* @seralizer text
#* @post /secret_data_better
function(username, password) {
if (username == "your_name" & password == "secret") {
return("Here's some secret data")
} else {
api_error("Wrong username or password!", 401)
}
}
#* Get a token
#* @tag Authentication
#* @post /get_token
function(req, res, username = "", password = "") {
if (username == Sys.getenv("API_USERNAME") & password == Sys.getenv("API_PASSWORD")) {
# If the user submits the correct login details, generate a token for them
claim <- jwt_claim(valid_user = TRUE)
key <- charToRaw(Sys.getenv("API_JWT_SECRET"))
jwt <- jwt_encode_hmac(claim, key)
return(list(token = jwt))
} else {
api_error(message = "Invalid username or password", status = 401)
}
}
# Function for making an endpoint require a token. This isn't an endpoint or
# filter or anything special. Use it inside an endpoint.
require_token <- function(req, res, manual_token) {
if (!is.na(manual_token)) {
# If a manual token is passed, use that
token <- as.character(manual_token)
} else {
# Otherwise use the one in the HTTP header
token <- req$HTTP_AUTHORIZATION |> str_remove("^Bearer ")
}
# If there isn't a token, that's wrong
if (is.null(token) || length(token) == 0) {
api_error(message = "No token provided", status = 401)
}
# Decode the token. If it matches what's on the server, yay. If not, it's wrong.
tryCatch({
jwt_decode_hmac(token, secret = Sys.getenv("API_JWT_SECRET"))
return(TRUE)
}, error = function(e) {
api_error(message = "Token is wrong", status = 401)
})
}
#* JWT secret thing
#* @tag Authentication
#* @seralizer text
#* @post /secret_data_jwt
function(req, res, manual_token = NA) {
require_token(req, res, manual_token)
return("Here's some secret data")
}