forked from wpinvestigative/arcos-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpossible_future_api_calls.R
141 lines (101 loc) · 4.95 KB
/
possible_future_api_calls.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
### Annual raw data by specific drug and state and county
#' Returns raw data filtered by specific drug and state and county
#' Refer to drug_list() for complete list of drug options
#' @param drug Filter the data to only this drug (e.g. 'OXYCODONE')
#' @param key Key needed to make query successful
#' @param year Filter the data to only this year (e.g. 2009)
#' @param county If provided, filter the data to only this county (e.g. 'Mingo')
#' @param state Filter the data to only this state (e.g. 'WV')
#' @tag raw
#' @get /v1/drug_county_year
function(drug, county, state, year, key){
if (missing(key)) {
return(list(error="Authentication required. Did you include an API key?"))
} else {
if (!key %in% list_of_keys) {
return(list(error="Authentication required. Did you include an API key?"))
} else {
if (!missing(drug) & str_to_upper(drug) %in% drug_types) {
if (!missing(year)){
if (as.numeric(year) <= 2014 & as.numeric(year) >=2006) {
if (!missing(state)) {
state <- str_to_upper(state)
if (state %in% county_relationship_states) {
if (!missing(county)) {
county <- str_to_upper(county)
county <- gsub("-", " ", county)
county <- gsub("%20", " ", county)
if (county %in% county_relationship_counties) {
base_url <- "https://wp-stat.s3.amazonaws.com/dea-pain-pill-database/drugs/"
state_abb <- str_to_upper(state)
drug_lookup <- str_to_upper(gsub(", ", "_", drug))
url <- paste0(base_url, drug_lookup, "-", state_abb, "-", year, ".csv")
df <- vroom(url)
return(df)
} else {
return(list(error="County not found"))
}
}
else {
return(list(error="County not found"))
}
else {
return(list(error="State abbreviation not found"))
}
#}
} else {
return(list(error="Only data between 2006 and 2014 currently available"))
}
} else {
return(list(error="Year needed"))
}
} else {
return(list(error="Drug needed. Use the drug_list() function to see the options."))
}
}
}
}
### Annual raw data by specific drug and state
#' Returns raw data filtered by specific drug and state.
#' Refer to drug_list() for complete list of drug options
#' @param drug Filter the data to only this drug (e.g. 'OXYCODONE')
#' @param key Key needed to make query successful
#' @param year Filter the data to only this year (e.g. 2009)
#' @param state Filter the data to only this state (e.g. 'WV')
#' @tag raw
#' @get /v1/drug_state_year
function(drug, state, year, key){
if (missing(key)) {
return(list(error="Authentication required. Did you include an API key?"))
} else {
if (!key %in% list_of_keys) {
return(list(error="Authentication required. Did you include an API key?"))
} else {
if (!missing(drug) & str_to_upper(drug) %in% drug_types) {
if (!missing(year)){
if (as.numeric(year) <= 2014 & as.numeric(year) >=2006) {
if (!missing(state)) {
state <- str_to_upper(state)
if (state %in% county_relationship_states) {
base_url <- "https://wp-stat.s3.amazonaws.com/dea-pain-pill-database/drugs/"
state_abb <- str_to_upper(state)
drug_lookup <- str_to_upper(gsub(", ", "_", drug))
url <- paste0(base_url, drug_lookup, "-", state_abb, "-", year, ".csv")
df <- vroom(url)
return(df)
} else {
return(list(error="State abbreviation not found"))
}
}
} else {
return(list(error="Only data between 2006 and 2014 currently available"))
}
} else {
return(list(error="Year needed"))
}
} else {
return(list(error="Drug needed. Use the drug_list() function to see the options."))
}
}
}
}