From b6b4f42afdcfdcf7b096b334a3f02645a4d5dd9a Mon Sep 17 00:00:00 2001 From: buhly Date: Wed, 7 Aug 2024 18:13:00 +0200 Subject: [PATCH 1/3] fix bug (issue #35) --- R/gen_table.R | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/R/gen_table.R b/R/gen_table.R index ebfabd5..cf628b5 100644 --- a/R/gen_table.R +++ b/R/gen_table.R @@ -78,6 +78,13 @@ gen_table_ <- function(name, #----------------------------------------------------------------------------- # Parameter processing + if (missing(database)) { + + stop("It is mandatory to specifiy the 'database' parameter for 'gen_table()'.", + call. = FALSE) + + } + database <- match.arg(database) area <- match.arg(area) From 6fcc84338752d9194fb8e1195de2915a89c85fbf Mon Sep 17 00:00:00 2001 From: buhly Date: Sun, 11 Aug 2024 16:41:07 +0200 Subject: [PATCH 2/3] fix bugs in gen_table --- R/gen_table.R | 2 +- R/utils_httr2.R | 14 +++++++++++--- README.Rmd | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/R/gen_table.R b/R/gen_table.R index cf628b5..38a3225 100644 --- a/R/gen_table.R +++ b/R/gen_table.R @@ -110,7 +110,7 @@ gen_table_ <- function(name, #----------------------------------------------------------------------------- # Data download - if(database == "zensus"){ + if (database == "zensus"){ response <- gen_zensus_api("data/tablefile", name = name, diff --git a/R/utils_httr2.R b/R/utils_httr2.R index 4e5929c..73fb9f6 100644 --- a/R/utils_httr2.R +++ b/R/utils_httr2.R @@ -276,21 +276,29 @@ return_table_object <- function(response, "Consider making a range of smaller requests or use the \n", "option to create a job by setting the 'job' parameter \n", "of 'gen_table()' to TRUE. You can then download the job \n", - "later (use the function 'gen_list_jobs()' to check its status).") + "later (use the function 'gen_list_jobs()' to check its status) \n", + "and download it using gen_download_job().") stop(error_message, call. = FALSE) } else if (response_parsed$Status$Code == 99) { - message <- paste0("You have requested successfully created a job with \n", + message <- paste0("You have successfully created a job with \n", "your request. Use the function 'gen_list_jobs()' ", "to check its status and download it once completed.") message(message) + } else if (response_parsed$Status$Code == 104) { + + stop("There are no results for your request. Please check if the requested table code is valid for the database selected.", + call. = FALSE) + } else { - stop("There has been an error with your request (not parseable response type 'application/json').\n Please try again later or contact the package maintainer.", + stop(paste0("There has been an error with your request (API error code: '", + response_parsed$Status$Code, + "').\n Please try again later or contact the package maintainer."), call. = FALSE) } diff --git a/README.Rmd b/README.Rmd index 1591c2e..55d6e44 100644 --- a/README.Rmd +++ b/README.Rmd @@ -72,7 +72,7 @@ In a short overview, there are functions divided in two main parts, searching fo #### Other functions - **gen_logincheck()**: Perform a logincheck to test your credentials -- **gen_qualitysigns()**: Get a list of quality signs (special characters) found in the API's tables +- **gen_signs()**: Get a list of quality signs (special characters) found in the API's tables - **gen_update_evas()**: Manually scrape a newer version of the EVAS numbers (official statistic IDs) ### Caching From 92a9c1b70592c1b21e19e4e5aaa0e10f784e7801 Mon Sep 17 00:00:00 2001 From: buhly Date: Fri, 30 Aug 2024 11:03:54 +0200 Subject: [PATCH 3/3] begin move to POST --- R/gen_api.R | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/R/gen_api.R b/R/gen_api.R index 6410c3a..12b4340 100644 --- a/R/gen_api.R +++ b/R/gen_api.R @@ -14,12 +14,42 @@ #' httr2::resp_body_json() #' } #' +# gen_api <- function(endpoint, ...) { +# +# httr2::request("https://www-genesis.destatis.de/genesisWS/rest/2020") %>% +# httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>% +# httr2::req_url_path_append(endpoint) %>% +# httr2::req_url_query(!!!gen_auth_get(database = "genesis"), ...) %>% +# httr2::req_retry(max_tries = 3) %>% +# httr2::req_perform() +# +# } + gen_api <- function(endpoint, ...) { - httr2::request("https://www-genesis.destatis.de/genesisWS/rest/2020") %>% - httr2::req_user_agent("https://github.com/CorrelAid/restatis") %>% + url <- "https://www-genesis.destatis.de/genesisWS/rest/2020" + user_agent <- "https://github.com/CorrelAid/restatis" + + 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_url_query(!!!gen_auth_get(database = "genesis"), ...) %>% + 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()