Skip to content

Commit ae0681d

Browse files
committed
refactor gen_*_api to gen_api and begin resulting changes
1 parent 1370bc0 commit ae0681d

File tree

10 files changed

+183
-343
lines changed

10 files changed

+183
-343
lines changed

R/gen_api.R

Lines changed: 47 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#' gen_genesis_api
1+
#' gen_api
22
#'
3-
#' @description Low-level function to interact with the GENESIS API
3+
#' @description Low-level function to interact with the one of the APIs
44
#'
55
#' @param endpoint Character string. The endpoint of the API that is to be queried.
6+
#' @param database The database the query should be sent to.
67
#' @param ... Further parameters passed on to the final API call.
78
#'
89
#' @importFrom httr2 `%>%`
@@ -11,122 +12,78 @@
1112
#'
1213
#' @examples
1314
#' \dontrun{
14-
#' gen_genesis_api("helloworld/logincheck") %>%
15+
#' gen_api(endpoint = "helloworld/logincheck", database = "genesis") %>%
1516
#' httr2::resp_body_json()
1617
#' }
1718
#'
18-
gen_genesis_api <- function(endpoint,
19-
...) {
19+
gen_api <- function(endpoint,
20+
database,
21+
...) {
2022

21-
url <- Sys.getenv("RESTATIS_GENESIS_URL")
23+
#-----------------------------------------------------------------------------
2224

23-
user_agent <- "https://github.com/CorrelAid/restatis"
25+
# Define URLs
2426

25-
body_parameters <- list(...)
27+
if (database == "genesis") {
2628

27-
if (length(body_parameters) > 0) {
29+
url <- Sys.getenv("RESTATIS_GENESIS_URL")
2830

29-
req <- httr2::request(url) %>%
30-
httr2::req_body_form(!!!body_parameters)
31+
} else if (database == "zensus") {
3132

32-
} else {
33+
url <- Sys.getenv("RESTATIS_ZENSUS_URL")
3334

34-
req <- httr2::request(url) %>%
35-
httr2::req_body_form(!!!list("foo" = "bar"))
35+
} else if (database == "regio") {
3636

37-
}
37+
url <- Sys.getenv("RESTATIS_REGIO_URL")
3838

39-
req %>%
40-
httr2::req_user_agent(user_agent) %>%
41-
httr2::req_url_path_append(endpoint) %>%
42-
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded",
43-
"username" = gen_auth_get(database = "genesis")$username,
44-
"password" = gen_auth_get(database = "genesis")$password) %>%
45-
httr2::req_retry(max_tries = 3) %>%
46-
httr2::req_perform()
39+
}
4740

48-
}
41+
user_agent <- "https://github.com/CorrelAid/restatis"
4942

50-
#-------------------------------------------------------------------------------
43+
#-----------------------------------------------------------------------------
5144

52-
#' gen_regio_api
53-
#'
54-
#' @description Low-level function to interact with the regionalstatistik.de API
55-
#'
56-
#' @param endpoint Character string. The endpoint of the API that is to be queried.
57-
#' @param ... Further parameters passed on to the final API call.
58-
#'
59-
#' @importFrom httr2 `%>%`
60-
#'
61-
#' @noRd
62-
#'
63-
#' @examples
64-
#' \dontrun{
65-
#' gen_regio_api("helloworld/logincheck") %>%
66-
#' httr2::resp_body_json()
67-
#' }
68-
#'
69-
gen_regio_api <- function(endpoint,
70-
...) {
45+
# First try to request with POST
46+
# If POST errors, try GET
7147

72-
url <- Sys.getenv("RESTATIS_REGIO_URL")
48+
tryCatch(
7349

74-
httr2::request(url) %>%
75-
httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>%
76-
httr2::req_url_path_append(endpoint) %>%
77-
httr2::req_url_query(!!!gen_auth_get(database = "regio"), ...) %>%
78-
httr2::req_retry(max_tries = 3) %>%
79-
httr2::req_perform()
50+
error = function(cnd) {
8051

81-
}
52+
httr2::request(url) %>%
53+
httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>%
54+
httr2::req_url_path_append(endpoint) %>%
55+
httr2::req_url_query(!!!gen_auth_get(database = database), ...) %>%
56+
httr2::req_retry(max_tries = 3) %>%
57+
httr2::req_perform()
8258

83-
#-------------------------------------------------------------------------------
59+
}, {
8460

85-
#' gen_zensus_api
86-
#'
87-
#' @description Low-level function to interact with the Zensus 2022 database
88-
#'
89-
#' @param endpoint Character string. The endpoint of the API that is to be queried.
90-
#' @param ... Further parameters passed on to the final API call.
91-
#'
92-
#' @importFrom httr2 `%>%`
93-
#'
94-
#' @noRd
95-
#'
96-
#' @examples
97-
#' \dontrun{
98-
#' gen_zensus_api("helloworld/logincheck") %>%
99-
#' httr2::resp_body_json()
100-
#' }
101-
#'
102-
gen_zensus_api <- function(endpoint,
103-
...) {
61+
body_parameters <- list(...)
10462

105-
url <- Sys.getenv("RESTATIS_ZENSUS_URL")
63+
if (length(body_parameters) > 0) {
10664

107-
user_agent <- "https://github.com/CorrelAid/restatis"
65+
req <- httr2::request(url) %>%
66+
httr2::req_body_form(!!!body_parameters)
10867

109-
body_parameters <- list(...)
68+
} else {
11069

111-
if (length(body_parameters) > 0) {
70+
req <- httr2::request(url) %>%
71+
httr2::req_body_form(!!!list("foo" = "bar"))
11272

113-
req <- httr2::request(url) %>%
114-
httr2::req_body_form(!!!body_parameters)
73+
}
11574

116-
} else {
75+
req %>%
76+
httr2::req_user_agent(user_agent) %>%
77+
httr2::req_url_path_append(endpoint) %>%
78+
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded",
79+
"username" = gen_auth_get(database = database)$username,
80+
"password" = gen_auth_get(database = database)$password) %>%
81+
httr2::req_retry(max_tries = 3) %>%
82+
httr2::req_perform()
11783

118-
req <- httr2::request(url) %>%
119-
httr2::req_body_form(!!!list("foo" = "bar"))
84+
})
12085

121-
}
86+
#-----------------------------------------------------------------------------
12287

123-
req %>%
124-
httr2::req_user_agent(user_agent) %>%
125-
httr2::req_url_path_append(endpoint) %>%
126-
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded",
127-
"username" = gen_auth_get(database = "zensus")$username,
128-
"password" = gen_auth_get(database = "zensus")$password) %>%
129-
httr2::req_retry(max_tries = 3) %>%
130-
httr2::req_perform()
13188

13289
}

R/gen_catalogue.R

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ gen_catalogue <- function(code = NULL,
3939

4040
caller <- as.character(match.call()[1])
4141

42-
gen_fun <- test_database_function(database,
43-
error.input = error.ignore,
44-
text = verbose)
42+
# database_vector will hold a vector of the specified databases to query
43+
database_vector <- test_database_function(database,
44+
error.input = error.ignore,
45+
text = verbose)
4546

4647
check_function_input(code = code,
4748
category = category,
4849
detailed = detailed,
4950
error.ignore = error.ignore,
50-
database = gen_fun,
51+
database = database_vector,
5152
sortcriterion = sortcriterion,
5253
caller = caller,
5354
verbose = verbose)
@@ -61,32 +62,32 @@ gen_catalogue <- function(code = NULL,
6162
#-----------------------------------------------------------------------------
6263

6364
# Processing #
64-
res <- lapply(gen_fun, function(db){
65+
res <- lapply(database_vector, function(db){
6566

6667
if (isTRUE(verbose)) {
6768

68-
info <- paste("Started the processing of", rev_database_function(db), "database.")
69+
info <- paste("Started the processing of", db, "database.")
6970

7071
message(info)
7172

7273
}
7374

7475
#---------------------------------------------------------------------------
7576

76-
if ("cubes" %in% category && db == "gen_zensus_api") {
77+
if ("cubes" %in% category && db == "zensus") {
7778

7879
list_of_cubes <- "There are generally no 'cubes' objects available for the 'zensus' database."
7980

80-
} else if ("cubes" %in% category && (db == "gen_genesis_api" | db == "gen_regio_api")) {
81+
} else if ("cubes" %in% category && (db == "genesis" | db == "regio")) {
8182

82-
results_raw <- do.call(db,
83-
list(endpoint = "catalogue/cubes",
84-
username = gen_auth_get(database = rev_database_function(db))$username,
85-
password = gen_auth_get(database = rev_database_function(db))$password,
86-
selection = code,
87-
sortcriterion = sortcriterion,
88-
area = area,
89-
...))
83+
results_raw <- gen_api(endpoint = "catalogue/cubes",
84+
database = db,
85+
username = gen_auth_get(database = db)$username,
86+
password = gen_auth_get(database = db)$password,
87+
selection = code,
88+
sortcriterion = sortcriterion,
89+
area = area,
90+
...)
9091

9192
results_json <- test_if_json(results_raw)
9293

@@ -132,14 +133,13 @@ gen_catalogue <- function(code = NULL,
132133

133134
if ("statistics" %in% category) {
134135

135-
par_list <- list(endpoint = "catalogue/statistics",
136-
username = gen_auth_get(database = rev_database_function(db))$username,
137-
password = gen_auth_get(database = rev_database_function(db))$password,
138-
selection = code,
139-
sortcriterion = sortcriterion,
140-
...)
141-
142-
results_raw <- do.call(db, par_list)
136+
results_raw <- gen_api(endpoint = "catalogue/statistics",
137+
database = db,
138+
username = gen_auth_get(database = db)$username,
139+
password = gen_auth_get(database = db)$password,
140+
selection = code,
141+
sortcriterion = sortcriterion,
142+
...)
143143

144144
results_json <- test_if_json(results_raw)
145145

@@ -182,15 +182,14 @@ gen_catalogue <- function(code = NULL,
182182

183183
if ("tables" %in% category) {
184184

185-
par_list <- list(endpoint = "catalogue/tables",
186-
username = gen_auth_get(database = rev_database_function(db))$username,
187-
password = gen_auth_get(database = rev_database_function(db))$password,
188-
selection = code,
189-
area = area,
190-
sortcriterion = sortcriterion,
191-
...)
192-
193-
results_raw <- do.call(db, par_list)
185+
results_raw <- gen_api(endpoint = "catalogue/tables",
186+
database = db,
187+
username = gen_auth_get(database = db)$username,
188+
password = gen_auth_get(database = db)$password,
189+
selection = code,
190+
area = area,
191+
sortcriterion = sortcriterion,
192+
...)
194193

195194
results_json <- test_if_json(results_raw)
196195

@@ -243,7 +242,7 @@ gen_catalogue <- function(code = NULL,
243242
#---------------------------------------------------------------------------
244243
} else if ("cubes" %in% category) {
245244

246-
if (length(list_of_cubes) == 1 && db == "gen_zensus_api"){
245+
if (length(list_of_cubes) == 1 && db == "zensus"){
247246

248247
list_resp <- list_of_cubes
249248

@@ -289,12 +288,12 @@ gen_catalogue <- function(code = NULL,
289288
#---------------------------------------------------------------------------
290289

291290
attr(list_resp, "Code") <- code
292-
attr(list_resp, "Database") <- rev_database_function(db)
291+
attr(list_resp, "Database") <- db
293292
attr(list_resp, "Category") <- category
294293

295-
if (length(category) == 1 && "cubes" %in% category && db == "gen_zensus_api"){
294+
if (length(category) == 1 && "cubes" %in% category && db == "zensus"){
296295

297-
attr(list_resp, "Info") <- "NO API call done"
296+
attr(list_resp, "Info") <- "No API call has been executed."
298297

299298
} else {
300299

0 commit comments

Comments
 (0)