From 19041e19fde62f05ed55eb70a73670e823165342 Mon Sep 17 00:00:00 2001 From: Alex Axthelm Date: Thu, 18 Apr 2024 18:45:27 +0200 Subject: [PATCH] add for empty data on get_currency_exchange_rates --- R/get_currency_exchange_rates.R | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/R/get_currency_exchange_rates.R b/R/get_currency_exchange_rates.R index d34b07c..452cbf0 100644 --- a/R/get_currency_exchange_rates.R +++ b/R/get_currency_exchange_rates.R @@ -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") %>%