Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug (issue #35) #36

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions R/gen_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
9 changes: 8 additions & 1 deletion R/gen_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -103,7 +110,7 @@ gen_table_ <- function(name,
#-----------------------------------------------------------------------------
# Data download

if(database == "zensus"){
if (database == "zensus"){

response <- gen_zensus_api("data/tablefile",
name = name,
Expand Down
14 changes: 11 additions & 3 deletions R/utils_httr2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading