Skip to content

Commit

Permalink
add for empty data on get_currency_exchange_rates
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAxthelm committed Apr 18, 2024
1 parent 1451b89 commit 19041e1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions R/get_currency_exchange_rates.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,29 @@ get_currency_exchange_rates <-
year
)

httr2::request(url) %>%
raw_data <- httr2::request(url) %>%
httr2::req_headers("Accept" = "application/json") %>%
httr2::req_timeout(max_seconds) %>%
httr2::req_retry(
max_seconds = max_seconds,
is_transient = ~ httr2::resp_content_type(.x) != "application/json"
) %>%
) %>%
httr2::req_error(
is_error = ~ httr2::resp_content_type(.x) != "application/json",
body = ~ cli::format_inline("IMF API did not return a valid response after {.val {max_seconds}} seconds.")
) %>%
) %>%
httr2::req_perform() %>%
httr2::resp_body_json(simplifyVector = TRUE) %>%
.[["CompactData"]] %>%
.[["DataSet"]] %>%
.[["Series"]] %>%
.[["Series"]]

if (is.null(raw_data)) {
log_error("No data found for the specified quarter: {quarter}.")
stop("No data found for the specified quarter.")
}

raw_data %>%
dplyr::rowwise() %>%
dplyr::mutate(Obs = list(as.data.frame(.data$Obs))) %>%
tidyr::unnest(cols = "Obs") %>%
Expand Down

0 comments on commit 19041e1

Please sign in to comment.