Skip to content

Commit

Permalink
Merge pull request #53 from CorrelAid/dev
Browse files Browse the repository at this point in the history
Merge dev into main to prepare CRAN release
  • Loading branch information
yannikbuhl authored Jan 22, 2025
2 parents 9a0eb4c + d7fd5fe commit dfaa4d8
Show file tree
Hide file tree
Showing 59 changed files with 943 additions and 917 deletions.
3 changes: 0 additions & 3 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@
^pkgdown$
^codecov\.yml$
^cran-comments\.md$
<<<<<<< HEAD
=======
^CRAN-SUBMISSION$
>>>>>>> main
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: restatis
Title: R Wrapper to Access a Wide Range of Germany's Federal Statistical System
Databases Based on the GENESIS Web Service RESTful API of the German Federal
Statistical Office (Statistisches Bundesamt/Destatis)
Version: 0.2.0
Version: 0.3.0
Authors@R: c(
person("Yannik", "Buhl", , "[email protected]", role = c("aut", "cre")),
person("Zoran", "Kovacevic", role = "aut",
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
YEAR: 2024
YEAR: 2025
COPYRIGHT HOLDER: restatis authors
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MIT License

Copyright (c) 2022 restatis authors
Copyright (c) 2025 restatis authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# restatis 0.3.0

* Added the feature that users can now adjust the API URLs via environment variables
* Added the feature of turning on and off caching via options()
* Added pagelength as a function parameter where available
* Added the "..." parameter to gen_table() and gen_cube()
* Refactored some of the internal functions
* Bugfixes

# restatis 0.2.0

* Added support for regionalstatistik.de and the Zensus 2022 database
Expand Down
32 changes: 17 additions & 15 deletions R/gen_alternative_terms.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param term Character string. Maximum length of 15 characters. Term or word for which you are searching for alternative or related terms. Use of '*' as a placeholder is possible to generate broader search areas.
#' @param similarity Boolean. Indicator if the output of the function should be sorted based on a Levenshtein edit distance based on the \code{adist()} function. Default is 'TRUE'.
#' @param database Character string. Indicator if the GENESIS ('genesis'), Zensus 2022 ('zensus') or regionalstatistik.de ('regio') database is called. Default option is 'all'.
#' @param pagelength Integer. Maximum length of results or objects (e.g., number of tables). Defaults to 500.
#' @param verbose Boolean. Indicator if the output of the function should include detailed messages and warnings. Default option is 'TRUE'. Set the parameter to 'FALSE' to suppress additional messages and warnings.
#' @param ... Additional parameters for the API call. These parameters are only affecting the call itself, no further processing. For more details see `vignette("additional_parameter")`.
#'
Expand All @@ -27,49 +28,50 @@
gen_alternative_terms <- function(term = NULL,
similarity = TRUE,
database = c("all", "genesis", "zensus", "regio"),
pagelength = 500,
verbose = TRUE,
...) {

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

gen_fun <- test_database_function(database,
error.input = TRUE,
text = verbose)
database_vector <- test_database_function(database,
error.input = TRUE,
text = verbose)

check_function_input(term = term,
similarity = similarity,
pagelength = pagelength,
caller = caller,
verbose = verbose)

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

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

if (verbose) {

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

message(info)

}

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

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

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

results_json <- test_if_json(results_raw)

if (length(results_json$List) == 0 & length(gen_fun) == 1) {
if (length(results_json$List) == 0 & length(database_vector) == 1) {

stop("No related terms found for your code.", call. = FALSE)

} else if (length(results_json$List) == 0 & length(gen_fun) > 1) {
} else if (length(results_json$List) == 0 & length(database_vector) > 1) {

termslist <- "No related terms found for your code."

Expand Down Expand Up @@ -115,7 +117,7 @@ gen_alternative_terms <- function(term = NULL,
}

attr(list_resp, "Term") <- term
attr(list_resp, "Database") <- rev_database_function(db)
attr(list_resp, "Database") <- db
attr(list_resp, "Language") <- results_json$Parameter$language
attr(list_resp, "Pagelength") <- results_json$Parameter$pagelength
attr(list_resp, "Copyright") <- results_json$Copyright
Expand Down
151 changes: 66 additions & 85 deletions R/gen_api.R
Original file line number Diff line number Diff line change
@@ -1,132 +1,113 @@
#' gen_genesis_api
#' gen_api
#'
#' @description Low-level function to interact with the GENESIS API
#' @description Wrapper function to either use cached version of gen_api or un-cached version
#'
#' @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 `%>%`
#' @param ... Parameters passed on to the API call
#' @param use_cache Get the option value on whether the call should be cached or not
#'
#' @noRd
#'
#' @examples
#' \dontrun{
#' gen_genesis_api("helloworld/logincheck") %>%
#' gen_api(endpoint = "helloworld/logincheck", database = "genesis") %>%
#' httr2::resp_body_json()
#' }
#'
gen_genesis_api <- function(endpoint,
...) {

url <- Sys.getenv("RESTATIS_GENESIS_URL")

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

body_parameters <- list(...)
gen_api <- function(...,
use_cache = getOption("restatis.use_cache", TRUE)) {

if (length(body_parameters) > 0) {
if (isTRUE(use_cache)) {

req <- httr2::request(url) %>%
httr2::req_body_form(!!!body_parameters)
return(.gen_api_cached(...))

} else {

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

}

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()

}

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

#' gen_regio_api
#' .gen_api_core
#'
#' @description Low-level function to interact with the regionalstatistik.de 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 `%>%`
#'
#' @noRd
#'
#' @examples
#' \dontrun{
#' gen_regio_api("helloworld/logincheck") %>%
#' httr2::resp_body_json()
#' }
#'
gen_regio_api <- function(endpoint,
.gen_api_core <- function(endpoint,
database,
...) {

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

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()
# Define URLs

}
if (database == "genesis") {

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

#' 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,
...) {
} else if (database == "zensus") {

url <- Sys.getenv("RESTATIS_ZENSUS_URL")

} else if (database == "regio") {

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

}

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

body_parameters <- list(...)
#-----------------------------------------------------------------------------

if (length(body_parameters) > 0) {
# First try to request with POST
# If POST errors, try GET

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

} else {
error = function(cnd) {

req <- httr2::request(url) %>%
httr2::req_body_form(!!!list("foo" = "bar"))
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()

}
}, {

body_parameters <- list(...)

if (length(body_parameters) > 0) {

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

} else {

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 = database)$username,
"password" = gen_auth_get(database = database)$password) %>%
httr2::req_retry(max_tries = 3) %>%
httr2::req_perform()

})

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

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()

}
Loading

0 comments on commit dfaa4d8

Please sign in to comment.