-
Notifications
You must be signed in to change notification settings - Fork 0
/
plumber.R
96 lines (81 loc) · 2.26 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
library(plumber)
library(mongolite)
library(jsonlite)
#* @apiTitle Tidy Tuesday API
#* @apiDescription This is an API to access data sets from the Tidy Tuesday project. For more information about this API, see the [project GitHub](https://github.com/asbates/tidytuesdayapi). You can find out more about Tidy Tuesday on it's [home page](https://github.com/rfordatascience/tidytuesday).
#* @apiVersion 0.0.1
#* @filter cors
cors <- function(res) {
res$setHeader("Access-Control-Allow-Origin", "*")
plumber::forward()
}
# serves the landing page
#* @assets ./static /
list()
#* Get information about available data sets, like date and description.
#* @param limit Limit the number of results to return.
#* @serializer json
#* @get /available
function(req, res, limit = 10L) {
limit <- as.integer(limit)
# if limit is character, as.integer() coerces to NA
if (is.na(limit)) {
res$status <- 500
return(
list(error = unbox("Limit must be an integer."))
)
}
# 200 ~ 52 weeks * 4 years of data (2018...2021)
if ( (limit > 0 & limit > 200) | limit < 0) {
res$status <- 500
return(
list(error = unbox("Limit must be between 1 and 200."))
)
}
collection <- mongo(
collection = "available",
db = "metadata",
url = Sys.getenv("DB_CONNECTION_URI")
)
available <- collection$find(
query = '{}',
fields = '{"_id": false}',
sort = '{"date": -1}',
limit = limit
)
collection$disconnect()
available
}
#* Get a data set.
#* @param date The date for the data set in the format 'YYYY-MM-DD'.
#* @serializer json
#* @get /data
function(req, res, date){
# check the date format
valid_date <- grepl("^[0-9]{4}-[0-9]{2}-[0-9]{2}$", date)
if (!valid_date) {
res$status <- 500
return(
list(error = unbox("That is not a valid date format."))
)
}
fs <- gridfs(
db = "data",
url = Sys.getenv("DB_CONNECTION_URI")
)
# if no data for that date, fs$read() returns an error, not empty data.frame
doc <- tryCatch(
{
raw_doc <- fs$read(date, progress = FALSE)
unserialize(raw_doc$data)
},
error = function(cond) {
res$status <- 500
return(
list(error = unbox("Data for that date is not available."))
)
},
finally = fs$disconnect()
)
doc
}