forked from ojalaquellueva/nsr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsr_api_example.R
More file actions
278 lines (230 loc) · 7.77 KB
/
nsr_api_example.R
File metadata and controls
278 lines (230 loc) · 7.77 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
###############################################
# R NSR API Example
###############################################
rm(list=ls())
#################################
# Parameters & libraries
#################################
##################
# Base URL
##################
url = "https://bien.nceas.ucsb.edu/nsr/nsr_wsb.php"
##################
# Libraries
##################
library(httr) # API requests
library(jsonlite) # JSON coding/decoding
##################
# Test data
##################
# Test data
# For details on how to organize an NSR input file, see:
# http://bien.nceas.ucsb.edu/bien/tools/nsr/batch-mode/
example_file <- "https://bien.nceas.ucsb.edu/bien/wp-content/uploads/2023/06/nsr_testfile.csv"
# Set TRUE to use sample of 10 rows of data, FALSE to use all
data.use.sample <- FALSE
##################
# Misc parameters
##################
# API variables to clear before each API call
# Avoids spillover between calls
api_vars <- c("mode")
# Response variables to clear before each API call
# Avoids confusion with previous results if API call fails
response_vars <- c("request_json", "response_json", "response")
#################################
# Functions
#################################
make_request_json <- function(
mode, # API mode; required
data=NULL # Raw data; required if mode=='resolve'
) {
######################################
# Accepts: options parameters and (optionally) data
# Returns: formatted JSON api request
######################################
# Set defaults if applicable
if ( mode=="resolve" ) {
# Convert raw data to JSON
if (is.null(data) ) stop("ERROR: mode 'resolve' requires data!\n")
data_json <- jsonlite::toJSON(unname(data))
}
opts <- data.frame(mode = mode)
opts_json <- jsonlite::toJSON(opts)
opts_json <- gsub('\\[','',opts_json)
opts_json <- gsub('\\]','',opts_json)
# Combine the options and data into single JSON object
if ( mode=="resolve" ) {
input_json <- paste0('{"opts":', opts_json, ',"data":', data_json, '}' )
} else {
input_json <- paste0('{"opts":', opts_json, '}' )
}
return(input_json)
}
send_request_json <- function( url, request_json ) {
###################################
# Accepts: API url + JSON options & data
# Sends: POST request to url, with JSON
# attached to body
# Returns: JSON response
###################################
if ( is.null(url) || is.na(url) ) {
stop("ERROR: parameter 'url' missing (function send_request_json()'\n")
}
if ( is.null(request_json) || is.na(request_json) ) {
stop("ERROR: parameter 'json_body' missing (function send_request_json()'\n")
}
response_json <- POST(url = url,
add_headers('Content-Type' = 'application/json'),
add_headers('Accept' = 'application/json'),
add_headers('charset' = 'UTF-8'),
body = request_json,
encode = "json"
)
return(response_json)
}
decode_response_json <- function( response_json, mode ) {
###################################
# Converts resonse json to data frame
# Also decodes weird format of NSR JSON
###################################
response_raw <- fromJSON( rawToChar( response_json$content ) )
response <- as.data.frame(response_raw)
if ( mode=="resolve") {
# Extra transformations needed due to weird NSR response data format
col.names <- response$id
response <- as.data.frame(t(response[,-1]))
colnames(response) <- col.names
# Convert f'ing factors to character
factor_columns <- sapply(response, is.factor)
response[factor_columns] <- lapply(
response[factor_columns], as.character
)
# Convert numeric columns
response$isIntroduced <- as.integer(response$isIntroduced)
response$isCultivatedNSR <- as.integer(response$isCultivatedNSR)
# Reset row numbers
row.names(response) <- 1:nrow(response)
} else {
# Unnest
response <- response[[1]]
}
return( response )
}
get_request <- function(
url, # Required
mode, # Required
data=NULL # Raw data; required if mode=='resolve)
) {
######################################
# Accepts: options parameters and (optionally) data
# required for API request
# Sends: POST request to API
# Returns: response formatted as data frame
#
# Meta-function which combine functions
# make_request_json, send_request_json &
# decode_response_json. See component
# functions for details
######################################
if ( is.null(url) || is.na(url) ) {
stop("ERROR: parameter 'url' missing (function get_request()'\n")
}
if ( is.null(mode) || is.na(mode) ) {
stop("ERROR: parameter 'mode' missing (function get_request()'\n")
}
request_json <- make_request_json(
mode=mode,
data= data
)
response_json <- send_request_json( url, request_json )
response_df <- decode_response_json( response_json, mode=mode )
# if ( ncol(response_df)==1 ) {
# colnames(response_df) <- "error"
# response_df$http_status <- response_json$status
# }
return( response_df )
}
specify_decimal <- function(x, k) {
# Set fixed number of decimals
if ( is.na(x) || is.null(x) ) {
x.formatted <- x
} else {
x.formatted <- format(round(x, k), nsmall=k)
}
return( x.formatted )
}
#################################
# Selected metadata checks
#
# Available metadata calls:
# "meta", "sources", "citations", "checklist_countries", "country_checklists"
# print(response) to see the complete results of each call
#################################
# Application version and database version
mode <- "meta"
rm( list = Filter( exists, response_vars ) )
# request_json <- make_request_json(mode=mode)
# response_json <- send_request_json( url, request_json )
# response <- decode_response_json(response_json)
response <- get_request(url=url, mode=mode)
db_version <- response$db_version
db_date <- response$build_date
if( "app_version" %in% colnames(response) ) {
app_version <- response$app_version
} else {
app_version <- response$code_version
}
# Available sources
mode <- "sources"
rm( list = Filter( exists, response_vars ) )
response <- get_request(url=url, mode=mode)
source.details <- response
# App and source citations
mode <- "citations"
rm( list = Filter( exists, response_vars ) )
citations <- get_request(url=url, mode=mode)
# Display results
cat("NSR version: ", app_version, "\n")
cat("DB version: ", db_version, " (", db_date, ")\n")
cat("Checklists:\n")
print(source.details, row.names=FALSE)
cat("Citations:")
print(citations, row.names=FALSE)
#################################
# Load test data
#################################
data <- read.csv(example_file, header=TRUE)
if (data.use.sample==TRUE) data <- head(data, 10) # Just a sample
cat("Raw data:\n")
print(data)
#################################
# Example 1: Resolve mode
#################################
# Clear existing variables
suppressWarnings( rm( list = Filter( exists, c(response_vars, api_vars ) ) ) )
# Set options
mode <- "resolve" # Processing mode
response <- get_request(url=url, mode=mode, data=data )
if ( colnames(response)[1]=="error" ) {
print( response )
} else {
print(response)
# More compact display:
print(response[ , c("family", "species", "country", "state_province", "native_status", "native_status_reason", "native_status_sources")])
}
#################################
# Example 2: Country checklists
#################################
mode <- "country_checklists"
rm( list = Filter( exists, response_vars ) )
country.checklists <- get_request(url=url, mode=mode)
country.checklists <- country.checklists[ order(country.checklists$gid_0), ]
print(country.checklists)
#################################
# Example 3: Checklist countries
#################################
mode <- "checklist_countries"
rm( list = Filter( exists, response_vars ) )
checklist.countries <- get_request(url=url, mode=mode)
print(checklist.countries)