Skip to content

Commit

Permalink
refactor gen_*_api to gen_api and begin resulting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yannikbuhl committed Dec 14, 2024
1 parent 1370bc0 commit ae0681d
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 343 deletions.
137 changes: 47 additions & 90 deletions R/gen_api.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' gen_genesis_api
#' gen_api
#'
#' @description Low-level function to interact with the GENESIS API
#' @description Low-level function to interact with the one of the APIs
#'
#' @param endpoint Character string. The endpoint of the API that is to be queried.
#' @param database The database the query should be sent to.
#' @param ... Further parameters passed on to the final API call.
#'
#' @importFrom httr2 `%>%`
Expand All @@ -11,122 +12,78 @@
#'
#' @examples
#' \dontrun{
#' gen_genesis_api("helloworld/logincheck") %>%
#' gen_api(endpoint = "helloworld/logincheck", database = "genesis") %>%
#' httr2::resp_body_json()
#' }
#'
gen_genesis_api <- function(endpoint,
...) {
gen_api <- function(endpoint,
database,
...) {

url <- Sys.getenv("RESTATIS_GENESIS_URL")
#-----------------------------------------------------------------------------

user_agent <- "https://github.com/CorrelAid/restatis"
# Define URLs

body_parameters <- list(...)
if (database == "genesis") {

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

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

} else {
url <- Sys.getenv("RESTATIS_ZENSUS_URL")

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

}
url <- Sys.getenv("RESTATIS_REGIO_URL")

req %>%
httr2::req_user_agent(user_agent) %>%
httr2::req_url_path_append(endpoint) %>%
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded",
"username" = gen_auth_get(database = "genesis")$username,
"password" = gen_auth_get(database = "genesis")$password) %>%
httr2::req_retry(max_tries = 3) %>%
httr2::req_perform()
}

}
user_agent <- "https://github.com/CorrelAid/restatis"

#-------------------------------------------------------------------------------
#-----------------------------------------------------------------------------

#' gen_regio_api
#'
#' @description Low-level function to interact with the regionalstatistik.de API
#'
#' @param endpoint Character string. The endpoint of the API that is to be queried.
#' @param ... Further parameters passed on to the final API call.
#'
#' @importFrom httr2 `%>%`
#'
#' @noRd
#'
#' @examples
#' \dontrun{
#' gen_regio_api("helloworld/logincheck") %>%
#' httr2::resp_body_json()
#' }
#'
gen_regio_api <- function(endpoint,
...) {
# First try to request with POST
# If POST errors, try GET

url <- Sys.getenv("RESTATIS_REGIO_URL")
tryCatch(

httr2::request(url) %>%
httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>%
httr2::req_url_path_append(endpoint) %>%
httr2::req_url_query(!!!gen_auth_get(database = "regio"), ...) %>%
httr2::req_retry(max_tries = 3) %>%
httr2::req_perform()
error = function(cnd) {

}
httr2::request(url) %>%
httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>%
httr2::req_url_path_append(endpoint) %>%
httr2::req_url_query(!!!gen_auth_get(database = database), ...) %>%
httr2::req_retry(max_tries = 3) %>%
httr2::req_perform()

#-------------------------------------------------------------------------------
}, {

#' gen_zensus_api
#'
#' @description Low-level function to interact with the Zensus 2022 database
#'
#' @param endpoint Character string. The endpoint of the API that is to be queried.
#' @param ... Further parameters passed on to the final API call.
#'
#' @importFrom httr2 `%>%`
#'
#' @noRd
#'
#' @examples
#' \dontrun{
#' gen_zensus_api("helloworld/logincheck") %>%
#' httr2::resp_body_json()
#' }
#'
gen_zensus_api <- function(endpoint,
...) {
body_parameters <- list(...)

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

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

body_parameters <- list(...)
} else {

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

req <- httr2::request(url) %>%
httr2::req_body_form(!!!body_parameters)
}

} else {
req %>%
httr2::req_user_agent(user_agent) %>%
httr2::req_url_path_append(endpoint) %>%
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded",
"username" = gen_auth_get(database = database)$username,
"password" = gen_auth_get(database = database)$password) %>%
httr2::req_retry(max_tries = 3) %>%
httr2::req_perform()

req <- httr2::request(url) %>%
httr2::req_body_form(!!!list("foo" = "bar"))
})

}
#-----------------------------------------------------------------------------

req %>%
httr2::req_user_agent(user_agent) %>%
httr2::req_url_path_append(endpoint) %>%
httr2::req_headers("Content-Type" = "application/x-www-form-urlencoded",
"username" = gen_auth_get(database = "zensus")$username,
"password" = gen_auth_get(database = "zensus")$password) %>%
httr2::req_retry(max_tries = 3) %>%
httr2::req_perform()

}
73 changes: 36 additions & 37 deletions R/gen_catalogue.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ gen_catalogue <- function(code = NULL,

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

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

check_function_input(code = code,
category = category,
detailed = detailed,
error.ignore = error.ignore,
database = gen_fun,
database = database_vector,
sortcriterion = sortcriterion,
caller = caller,
verbose = verbose)
Expand All @@ -61,32 +62,32 @@ gen_catalogue <- function(code = NULL,
#-----------------------------------------------------------------------------

# Processing #
res <- lapply(gen_fun, function(db){
res <- lapply(database_vector, function(db){

if (isTRUE(verbose)) {

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

message(info)

}

#---------------------------------------------------------------------------

if ("cubes" %in% category && db == "gen_zensus_api") {
if ("cubes" %in% category && db == "zensus") {

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

} else if ("cubes" %in% category && (db == "gen_genesis_api" | db == "gen_regio_api")) {
} else if ("cubes" %in% category && (db == "genesis" | db == "regio")) {

results_raw <- do.call(db,
list(endpoint = "catalogue/cubes",
username = gen_auth_get(database = rev_database_function(db))$username,
password = gen_auth_get(database = rev_database_function(db))$password,
selection = code,
sortcriterion = sortcriterion,
area = area,
...))
results_raw <- gen_api(endpoint = "catalogue/cubes",
database = db,
username = gen_auth_get(database = db)$username,
password = gen_auth_get(database = db)$password,
selection = code,
sortcriterion = sortcriterion,
area = area,
...)

results_json <- test_if_json(results_raw)

Expand Down Expand Up @@ -132,14 +133,13 @@ gen_catalogue <- function(code = NULL,

if ("statistics" %in% category) {

par_list <- list(endpoint = "catalogue/statistics",
username = gen_auth_get(database = rev_database_function(db))$username,
password = gen_auth_get(database = rev_database_function(db))$password,
selection = code,
sortcriterion = sortcriterion,
...)

results_raw <- do.call(db, par_list)
results_raw <- gen_api(endpoint = "catalogue/statistics",
database = db,
username = gen_auth_get(database = db)$username,
password = gen_auth_get(database = db)$password,
selection = code,
sortcriterion = sortcriterion,
...)

results_json <- test_if_json(results_raw)

Expand Down Expand Up @@ -182,15 +182,14 @@ gen_catalogue <- function(code = NULL,

if ("tables" %in% category) {

par_list <- list(endpoint = "catalogue/tables",
username = gen_auth_get(database = rev_database_function(db))$username,
password = gen_auth_get(database = rev_database_function(db))$password,
selection = code,
area = area,
sortcriterion = sortcriterion,
...)

results_raw <- do.call(db, par_list)
results_raw <- gen_api(endpoint = "catalogue/tables",
database = db,
username = gen_auth_get(database = db)$username,
password = gen_auth_get(database = db)$password,
selection = code,
area = area,
sortcriterion = sortcriterion,
...)

results_json <- test_if_json(results_raw)

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

if (length(list_of_cubes) == 1 && db == "gen_zensus_api"){
if (length(list_of_cubes) == 1 && db == "zensus"){

list_resp <- list_of_cubes

Expand Down Expand Up @@ -289,12 +288,12 @@ gen_catalogue <- function(code = NULL,
#---------------------------------------------------------------------------

attr(list_resp, "Code") <- code
attr(list_resp, "Database") <- rev_database_function(db)
attr(list_resp, "Database") <- db
attr(list_resp, "Category") <- category

if (length(category) == 1 && "cubes" %in% category && db == "gen_zensus_api"){
if (length(category) == 1 && "cubes" %in% category && db == "zensus"){

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

} else {

Expand Down
Loading

0 comments on commit ae0681d

Please sign in to comment.