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") %>%