-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovidAPI.R
88 lines (68 loc) · 2.83 KB
/
covidAPI.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
source("data.R")
# API landing page --------------------------------------------------------
#* @get /
#* @html
home <- function() {
title <- "Covid19 CV API"
body_intro <- "API to Retrieve Data from Covid19 CV Package"
body_model <- paste("We are currently serving model version:", 1.0)
body_msg <- paste("To received the cases data by date registry,",
"submit the following variables to the <b>/covid19cv</b> endpoint:",
sep = "\n")
body_msg1 <- paste("To received the data of totals,",
"submit the following variables to the <b>/covid19cv/1</b> endpoint:",
sep = "\n")
body_msg2 <- paste("To received the data of totals by cities,",
"submit the following variables to the <b>/covid19cv/2</b> endpoint:",
sep = "\n")
body_msg3 <- paste("To received the data of totals by demographics (i.e age group and sex),",
"submit the following variables to the <b>/covid19cv/3</b> endpoint:",
sep = "\n")
body_reqs <- paste(collapse = "<br>")
result <- paste(
"<html>",
"<h1>", title, "</h1>", "<br>",
"<body>",
"<p>", body_intro, "</p>",
"<p>", body_model, "</p>",
"<p>", body_msg, "</p>",
"<p>", body_msg1, "</p>",
"<p>", body_msg2, "</p>",
"<p>", body_msg3, "</p>",
"<p>", body_reqs, "</p>",
"</body>",
"</html>",
collapse = "\n"
)
return(result)
}
# get covid19 data endpoint --------------------------------
#* Cabo Verde COVID-19 Cases
#* @serializer unboxedJSON
#* @param dataset_number:int If provided, filter by: 1.Total;2.Cities;3.Demographics
#* @get /covid19cv/
function(res, dataset_number) {
if(res$status==400)
{
list(error=jsonlite::unbox("Bad Request"))
}
if(missing(dataset_number)){
modified <- list(keys = colnames(covid19cvdata::covid19cv),
values = unname(plyr::alply(covid19cvdata::covid19cv, 1, identity)),status=200)
}else if(dataset_number==1){
modified <- list(keys = colnames(covid19cvdata::covid19cv_nacional),
values = unname(plyr::alply(covid19cvdata::covid19cv_nacional, 1, identity)),status=200)
}
else if (dataset_number==2) {
modified <- list(keys = colnames(covid19cvdata::covid19cv_cidades),
values = unname(plyr::alply(covid19cvdata::covid19cv_cidades, 1, identity)),status=200)
}
else if (dataset_number==3) {
modified <- list(keys = colnames(covid19cvdata::covid19cv_pop),
values = unname(plyr::alply(covid19cvdata::covid19cv_pop, 1, identity)),status=200)
}else{
msg <- "Your request did not include a correct parameter."
res$status <- 400 # Bad request
list(error=jsonlite::unbox(msg))
}
}