-
Notifications
You must be signed in to change notification settings - Fork 1
/
plumber.R
205 lines (185 loc) · 3.44 KB
/
plumber.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
box::use(
plumber[...],
config[
get
],
dplyr[
mutate,
case_when,
summarise,
group_by,
n,
select
],
lubridate[
dmy,
month,
year
],
tidyr[
complete
],
purrr[
map_dbl
],
checkmate[
assert_subset
],
)
database_utils <- get("database_utils")
assert_subset(
database_utils,
c(
"dynamodb",
"supabase"
)
)
if (database_utils == "supabase") {
box::use(
utils/supabase_utils[
get_table_data,
get_table_schema,
put_table_row,
delete_table_row
],
)
}
if (database_utils == "dynamodb") {
box::use(
utils/dynamo_utils[
get_table_data = get_processed_table_data,
get_table_schema,
put_table_row,
delete_table_row
],
)
}
# Helper ----
#' Function to help with API Authentication
#' @param res the response object from Plumber
#' @param req the request object from Plumber
#' @param FUN the function call if authentication succeeds
#' @param ... params to pass to the function handler
auth_helper <- function(
res,
req,
FUN, #nolint: object_name_linter
...
) {
req_has_key <- "HTTP_X_API_KEY" %in% names(req)
key_is_valid <- req$HTTP_X_API_KEY == Sys.getenv("API_KEY")
environment_not_set <- nchar(Sys.getenv("API_KEY")) <= 1
if (!req_has_key || !key_is_valid || environment_not_set) {
res$body <- "Unauthorized"
res$status <- 401
return("Missing or invalid API key, or invalid configuration!")
} else {
FUN(...) #nolint: object_name_linter
}
}
# API Spec ----
#* @apiTitle Hrafnagud
#* @apiDescription An all-seeing API for personal use
#* @apiTag CRUD DynamoDb Utility Endpoints
## CRUD ----
### Schema ----
#* Schema
#* @param table_name:chr The table name to fetch the schema for.
#* @get /schema
#* @tag CRUD
function(
res,
req,
table_name
) {
auth_helper(
res,
req,
get_table_schema,
table_name = table_name
)
}
### Create ----
#* New row
#* @param table_name:chr The table name to add the row to.
#* @param input_list:[chr] The list of values to add in the row.
#* @param show_old:logical Show the last values of the row?
#* @put /create
#* @tag CRUD
function(
res,
req,
table_name,
input_list,
show_old
) {
auth_helper(
res,
req,
put_table_row,
table_name = table_name,
input_list = as.list(input_list),
show_old = as.logical(show_old)
)
}
### Read ----
#* Table
#* @param table_name:chr The table name to fetch data from.
#* @param limit:numeric The number of rows to limit at. Use 0 for all rows.
#* @get /read
#* @tag CRUD
function(
res,
req,
table_name,
limit
) {
auth_helper(
res,
req,
get_table_data,
table_name = table_name,
limit = as.numeric(limit)
)
}
### Update ----
#* Update row
#* @param table_name:chr The table name to modify the row in.
#* @param input_list:[chr] The list of values to add in the row.
#* @put /update
#* @tag CRUD
function(
res,
req,
table_name,
input_list,
) {
auth_helper(
res,
req,
put_table_row,
table_name = table_name,
input_list = as.list(input_list),
is_update = TRUE
)
}
### Delete ----
#* Delete row
#* @param table_name:chr The table name to remove the row from.
#* @param row_key:numeric The index of the row to delete.
#* @delete /delete
#* @tag CRUD
function(
res,
req,
table_name,
row_key,
) {
auth_helper(
res,
req,
delete_table_row,
table_name = table_name,
id_value = as.numeric(row_key)
)
}