From 4b2a01160808b51602d2cd744af5c703cd1f2448 Mon Sep 17 00:00:00 2001 From: buhly Date: Fri, 27 Sep 2024 20:44:50 +0200 Subject: [PATCH 1/4] move api urls to r environ --- R/gen_alternative_terms.R | 1 + R/gen_api.R | 14 +++----------- R/gen_catalogue.R | 1 + R/gen_cube.R | 3 ++- R/gen_find.R | 1 + R/gen_jobs.R | 2 +- R/gen_objects2var.R | 1 + R/gen_table.R | 22 +++++----------------- R/zzz.R | 25 ++++++++++++++++++++++++- 9 files changed, 39 insertions(+), 31 deletions(-) diff --git a/R/gen_alternative_terms.R b/R/gen_alternative_terms.R index 10c5e27..0b14964 100644 --- a/R/gen_alternative_terms.R +++ b/R/gen_alternative_terms.R @@ -29,6 +29,7 @@ gen_alternative_terms <- function(term = NULL, database = c("all", "genesis", "zensus", "regio"), verbose = TRUE, ...) { + caller <- as.character(match.call()[1]) gen_fun <- test_database_function(database, diff --git a/R/gen_api.R b/R/gen_api.R index e79791b..64761b2 100644 --- a/R/gen_api.R +++ b/R/gen_api.R @@ -17,12 +17,9 @@ #' } #' gen_genesis_api <- function(endpoint, - overwrite_url, ...) { - url <- ifelse(is.null(overwrite_url), - "https://www-genesis.destatis.de/genesisWS/rest/2020", - overwrite_url) + url <- Sys.getenv("RESTATIS_GENESIS_URL") user_agent <- "https://github.com/CorrelAid/restatis" @@ -75,9 +72,7 @@ gen_regio_api <- function(endpoint, overwrite_url, ...) { - url <- ifelse(is.null(overwrite), - "https://www.regionalstatistik.de/genesisws/rest/2020/", - overwrite_url) + url <- Sys.getenv("RESTATIS_REGIO_URL") httr2::request(url) %>% httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>% @@ -109,12 +104,9 @@ gen_regio_api <- function(endpoint, #' } #' gen_zensus_api <- function(endpoint, - overwrite_url, ...) { - url <- ifelse(is.null(overwrite_url), - "https://ergebnisse.zensus2022.de/api/rest/2020", - overwrite_url) + url <- Sys.getenv("RESTATIS_ZENSUS_URL") user_agent <- "https://github.com/CorrelAid/restatis" diff --git a/R/gen_catalogue.R b/R/gen_catalogue.R index f12e65b..755f912 100644 --- a/R/gen_catalogue.R +++ b/R/gen_catalogue.R @@ -36,6 +36,7 @@ gen_catalogue <- function(code = NULL, error.ignore = FALSE, verbose = TRUE, ...) { + caller <- as.character(match.call()[1]) gen_fun <- test_database_function(database, diff --git a/R/gen_cube.R b/R/gen_cube.R index 4021623..e90ee5f 100644 --- a/R/gen_cube.R +++ b/R/gen_cube.R @@ -72,7 +72,8 @@ gen_cube_ <- function(name, classifyingvariable3 = NULL, classifyingkey3 = NULL, stand = NULL, - language = Sys.getenv("GENESIS_LANG")) { + language = Sys.getenv("RESTATIS_LANG")) { + area <- match.arg(area) database <- match.arg(database) diff --git a/R/gen_find.R b/R/gen_find.R index 6f42a23..d9bc28d 100644 --- a/R/gen_find.R +++ b/R/gen_find.R @@ -39,6 +39,7 @@ gen_find <- function(term = NULL, error.ignore = TRUE, verbose = TRUE, ...) { + caller <- as.character(match.call()[1]) gen_fun <- test_database_function(database, diff --git a/R/gen_jobs.R b/R/gen_jobs.R index b0f3450..59c9f8c 100644 --- a/R/gen_jobs.R +++ b/R/gen_jobs.R @@ -133,7 +133,7 @@ gen_download_job <- function(name, database = c("genesis", "regio"), area = c("all", "public", "user"), compress = FALSE, - language = Sys.getenv("GENESIS_LANG"), + language = Sys.getenv("RESTATIS_LANG"), all_character = TRUE) { #----------------------------------------------------------------------------- diff --git a/R/gen_objects2var.R b/R/gen_objects2var.R index 450f9fa..14b29b5 100644 --- a/R/gen_objects2var.R +++ b/R/gen_objects2var.R @@ -35,6 +35,7 @@ gen_objects2var <- function(code = NULL, error.ignore = FALSE, verbose = TRUE, ...) { + caller <- as.character(match.call()[1]) gen_fun <- test_database_function(database, diff --git a/R/gen_table.R b/R/gen_table.R index 67a14ef..882bc53 100644 --- a/R/gen_table.R +++ b/R/gen_table.R @@ -73,10 +73,9 @@ gen_table_ <- function(name, classifyingvariable3 = NULL, classifyingkey3 = NULL, stand = NULL, - language = Sys.getenv("GENESIS_LANG"), + language = Sys.getenv("RESTATIS_LANG"), job = FALSE, - all_character = TRUE, - overwrite_url = NULL) { + all_character = TRUE) { #----------------------------------------------------------------------------- # Parameter processing @@ -88,14 +87,6 @@ gen_table_ <- function(name, } - if (!is.null(overwrite_url) & - (!is.character(overwrite_url) | length(overwrite_url) != 1)) { - - stop("The parameter 'overwrite_url' has to be of type 'character' and of length 1.", - call. = FALSE) - - } - database <- match.arg(database) area <- match.arg(area) @@ -141,8 +132,7 @@ gen_table_ <- function(name, stand = stand, language = language, format = "ffcsv", - job = FALSE, - overwrite_url = overwrite_url) + job = FALSE) #----------------------------------------------------------------------------- @@ -166,8 +156,7 @@ gen_table_ <- function(name, stand = stand, language = language, format = "ffcsv", - job = job, - overwrite_url = overwrite_url) + job = job) #----------------------------------------------------------------------------- @@ -191,8 +180,7 @@ gen_table_ <- function(name, stand = stand, language = language, format = "ffcsv", - job = job, - overwrite_url = overwrite_url) + job = job) #----------------------------------------------------------------------------- diff --git a/R/zzz.R b/R/zzz.R index 22deb39..3701e32 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -1,9 +1,32 @@ .onLoad <- function(libname, pkgname) { + # Set the functions whose results are to be cached gen_genesis_api <<- memoise::memoise(gen_genesis_api) gen_zensus_api <<- memoise::memoise(gen_zensus_api) gen_regio_api <<- memoise::memoise(gen_regio_api) - if (!nzchar(Sys.getenv("GENESIS_LANG"))) Sys.setenv(GENESIS_LANG = "en") + # Set the default language of the package + if (!nzchar(Sys.getenv("RESTATIS_LANG"))) Sys.setenv(GENESIS_LANG = "en") + + # Set the default URL for the GENESIS database + if (!nzchar(Sys.getenv("RESTATIS_GENESIS_URL"))) { + + Sys.setenv(RESTATIS_GENESIS_URL = "https://www-genesis.destatis.de/genesisWS/rest/2020") + + } + + # Set the default URL for the www.regionalstatistik.de database + if (!nzchar(Sys.getenv("RESTATIS_REGIO_URL"))) { + + Sys.setenv(RESTATIS_REGIO_URL = "https://www.regionalstatistik.de/genesisws/rest/2020/") + + } + + # Set the default URL for the ZENSUS 2022 database + if (!nzchar(Sys.getenv("RESTATIS_ZENSUS_URL"))) { + + Sys.setenv(RESTATIS_ZENSUS_URL = "https://ergebnisse.zensus2022.de/api/rest/2020") + + } } From d4488150cb2b5b5dd00294f5fac1d3e43db916ae Mon Sep 17 00:00:00 2001 From: buhly Date: Fri, 27 Sep 2024 20:49:54 +0200 Subject: [PATCH 2/4] small stuff --- R/gen_jobs.R | 2 +- R/zzz.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/gen_jobs.R b/R/gen_jobs.R index 59c9f8c..cd50195 100644 --- a/R/gen_jobs.R +++ b/R/gen_jobs.R @@ -118,7 +118,7 @@ gen_list_jobs <- function(database = c("genesis", "regio"), #' @param database Character string. Indicator if the GENESIS ('genesis') or regionalstatistik.de ('regio') database is called. Only one database can be addressed per function call. Default option is 'genesis'. #' @param area Character string. Indicator from which area of the database the results are called. In general, 'all' is the appropriate solution. Default option is 'all'. #' @param compress Boolean. Should empty rows and columns be discarded? Default is FALSE. -#' @param language Character string. Defines if the decimal mark and grouping mark of integers should be represented based on the European (e.g.: '100,5', '200.000,5') or American ('100.5', '200,000.5') system. Defaults to 'Sys.getenv("GENESIS_LANG")'. +#' @param language Character string. Defines if the decimal mark and grouping mark of integers should be represented based on the European (e.g.: '100,5', '200.000,5') or American ('100.5', '200,000.5') system. Defaults to 'Sys.getenv("RESTATIS_LANG")'. #' @param all_character Boolean. Should all variables be imported as 'character' variables? Avoids fuzzy data type conversions if there are leading zeros or other special characters. Defaults to TRUE. #' #' @return Returns a data.frame with the table content diff --git a/R/zzz.R b/R/zzz.R index 3701e32..d9e19a1 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -6,7 +6,7 @@ gen_regio_api <<- memoise::memoise(gen_regio_api) # Set the default language of the package - if (!nzchar(Sys.getenv("RESTATIS_LANG"))) Sys.setenv(GENESIS_LANG = "en") + if (!nzchar(Sys.getenv("RESTATIS_LANG"))) Sys.setenv(RESTATIS_LANG = "en") # Set the default URL for the GENESIS database if (!nzchar(Sys.getenv("RESTATIS_GENESIS_URL"))) { From e8d7a6ce32eb0e3fff0ddafb49102ca1167c3954 Mon Sep 17 00:00:00 2001 From: buhly Date: Thu, 5 Dec 2024 20:16:21 +0100 Subject: [PATCH 3/4] cleaning up dev to enable merge with main --- R/gen_api.R | 3 --- R/gen_cube.R | 2 +- R/gen_jobs.R | 2 +- R/gen_table.R | 2 +- R/zzz.R | 2 +- 5 files changed, 4 insertions(+), 7 deletions(-) diff --git a/R/gen_api.R b/R/gen_api.R index 64761b2..2ef8880 100644 --- a/R/gen_api.R +++ b/R/gen_api.R @@ -3,7 +3,6 @@ #' @description Low-level function to interact with the GENESIS API #' #' @param endpoint Character string. The endpoint of the API that is to be queried. -#' @param overwrite_url Character string. In certain cases it is required to set a custom URL for the respective API. By specifying the URL in this parameter, the API calls will be directed to this custom URL. But be aware, the URL has to lead to the same database (in this case: GENESIS), else there will be errors. Hence, use with caution. #' @param ... Further parameters passed on to the final API call. #' #' @importFrom httr2 `%>%` @@ -55,7 +54,6 @@ gen_genesis_api <- function(endpoint, #' @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 overwrite_url Character string. In certain cases it is required to set a custom URL for the respective API. By specifying the URL in this parameter, the API calls will be directed to this custom URL. But be aware, the URL has to lead to the same database (in this case: www.regionalstatistik.de), else there will be errors. Hence, use with caution. #' @param ... Further parameters passed on to the final API call. #' #' @importFrom httr2 `%>%` @@ -90,7 +88,6 @@ gen_regio_api <- function(endpoint, #' @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 overwrite_url Character string. In certain cases it is required to set a custom URL for the respective API. By specifying the URL in this parameter, the API calls will be directed to this custom URL. But be aware, the URL has to lead to the same database (in this case: Zensus 2022), else there will be errors. Hence, use with caution. #' @param ... Further parameters passed on to the final API call. #' #' @importFrom httr2 `%>%` diff --git a/R/gen_cube.R b/R/gen_cube.R index e90ee5f..d37d174 100644 --- a/R/gen_cube.R +++ b/R/gen_cube.R @@ -72,7 +72,7 @@ gen_cube_ <- function(name, classifyingvariable3 = NULL, classifyingkey3 = NULL, stand = NULL, - language = Sys.getenv("RESTATIS_LANG")) { + language = Sys.getenv("GENESIS_LANG")) { area <- match.arg(area) database <- match.arg(database) diff --git a/R/gen_jobs.R b/R/gen_jobs.R index cd50195..2c6454b 100644 --- a/R/gen_jobs.R +++ b/R/gen_jobs.R @@ -133,7 +133,7 @@ gen_download_job <- function(name, database = c("genesis", "regio"), area = c("all", "public", "user"), compress = FALSE, - language = Sys.getenv("RESTATIS_LANG"), + language = Sys.getenv("GENESIS_LANG"), all_character = TRUE) { #----------------------------------------------------------------------------- diff --git a/R/gen_table.R b/R/gen_table.R index 882bc53..6fb243b 100644 --- a/R/gen_table.R +++ b/R/gen_table.R @@ -73,7 +73,7 @@ gen_table_ <- function(name, classifyingvariable3 = NULL, classifyingkey3 = NULL, stand = NULL, - language = Sys.getenv("RESTATIS_LANG"), + language = Sys.getenv("GENESIS_LANG"), job = FALSE, all_character = TRUE) { diff --git a/R/zzz.R b/R/zzz.R index d9e19a1..10b28c0 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -6,7 +6,7 @@ gen_regio_api <<- memoise::memoise(gen_regio_api) # Set the default language of the package - if (!nzchar(Sys.getenv("RESTATIS_LANG"))) Sys.setenv(RESTATIS_LANG = "en") + if (!nzchar(Sys.getenv("GENESIS_LANG"))) Sys.setenv(RESTATIS_LANG = "en") # Set the default URL for the GENESIS database if (!nzchar(Sys.getenv("RESTATIS_GENESIS_URL"))) { From 189201f461f72c626abc9a099657a1b038355ce6 Mon Sep 17 00:00:00 2001 From: buhly Date: Fri, 6 Dec 2024 15:18:30 +0100 Subject: [PATCH 4/4] fix issue #45 --- R/utils_httr2.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/utils_httr2.R b/R/utils_httr2.R index a426db5..3535849 100644 --- a/R/utils_httr2.R +++ b/R/utils_httr2.R @@ -223,12 +223,12 @@ resp_check_data <- function(resp) { if (!(httr2::resp_content_type(resp) %in% c("application/zip", "text/csv", "application/json"))) { - stop("Encountered an invalid response type.", + stop(paste0("Encountered an invalid response type (", httr2::resp_content_type(resp), ")."), call. = FALSE) } - return <- httr2::resp_content_type(resp) + return(httr2::resp_content_type(resp)) }