Skip to content

Commit

Permalink
feat: initial support for start and end period
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Jan 14, 2024
1 parent 6761f6c commit b75dc3b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ importFrom(httr2,req_error)
importFrom(httr2,req_headers)
importFrom(httr2,req_perform)
importFrom(httr2,req_url_path_append)
importFrom(httr2,req_url_query)
importFrom(httr2,req_user_agent)
importFrom(httr2,request)
importFrom(httr2,resp_body_json)
Expand Down
38 changes: 34 additions & 4 deletions R/api.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@
#'
#' @param flow character(1) flow to query
#' @param key character(1) key to query. Default `NULL`.
#' @param start_period character(1) start date of the data. Supported formats:
#' - YYYY for annual data (e.g., "2019")
#' - YYYY-S[1-2] for semi-annual data (e.g., "2019-S1")
#' - YYYY-Q[1-4] for quarterly data (e.g., "2019-Q1")
#' - YYYY-MM for monthly data (e.g., "2019-01")
#' - YYYY-W[01-53] for weekly data (e.g., "2019-W01")
#' - YYYY-MM-DD for daily and business data (e.g., "2019-01-01")
#' If `NULL`, no start date restriction is applied (data retrieved from the
#' earliest available date). Default `NULL`.
#' @param end_period character(1) end date of the data, in the same format as
#' start_period. If `NULL`, no end date restriction is applied (data
#' retrieved up to the most recent available date). Default `NULL`.
#' @references <https://www.bundesbank.de/en/statistics/time-series-databases/help-for-sdmx-web-service/web-service-interface-data>
#' @family data
#' @export
#' @examples
#' # fetch all data for a given flow and key
#' bb_data("BBSIS", "D.I.ZST.ZI.EUR.S1311.B.A604.R10XX.R.A.A._Z._Z.A")
bb_data <- function(flow, key = NULL) {
#'
#' # specified period (start date-end date) for daily data
#' bb_data(
#' "BBSIS", "D.I.ZST.ZI.EUR.S1311.B.A604.R10XX.R.A.A._Z._Z.A",
#' start_period = "2020-01-01",
#' end_period = "2020-08-01"
#' )
#' # or only specify the start date
#' bb_data(
#' "BBSIS", "D.I.ZST.ZI.EUR.S1311.B.A604.R10XX.R.A.A._Z._Z.A",
#' start_period = "2020-01-01"
#' )
bb_data <- function(flow, key = NULL, start_period = NULL, end_period = NULL) {
stopifnot(is.character(flow), is.null(key) || is.character(key))
flow <- toupper(flow)
if (is.null(key)) {
Expand All @@ -16,7 +41,11 @@ bb_data <- function(flow, key = NULL) {
key <- toupper(key)
resource <- sprintf("data/%s/%s", flow, key)
}
body <- bb_make_request(resource)
body <- bb_make_request(
resource = resource,
startPeriod = start_period,
endPeriod = end_period
)

entries <- body |>
xml2::xml_find_all("//generic:Obs[generic:ObsValue]")
Expand Down Expand Up @@ -83,7 +112,7 @@ bb_concept_scheme <- function(id = NULL) {
bb_error_body <- function(resp) {
body <- resp_body_json(resp)
message <- body$title
docs <- "See docs at <https://www.bundesbank.de/en/statistics/time-series-databases/help-for-sdmx-web-service/status-codes/status-codes-855918>"
docs <- "See docs at <https://www.bundesbank.de/en/statistics/time-series-databases/help-for-sdmx-web-service/status-codes/status-codes-855918>" # nolint
c(message, docs)
}

Expand All @@ -97,11 +126,12 @@ bb_metadata <- function(resource, id = NULL) {
bb_make_request(resource)
}

bb_make_request <- function(resource) {
bb_make_request <- function(resource, ...) {
request("https://api.statistiken.bundesbank.de/rest") |>
req_user_agent("worldbank (https://m-muecke.github.io/worldbank)") |>
req_headers(`Accept-Language` = "en") |>
req_url_path_append(resource) |>
req_url_query(...) |>
req_error(body = bb_error_body) |>
req_perform() |>
resp_body_xml()
Expand Down
1 change: 1 addition & 0 deletions R/bundesbank-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @importFrom httr2 req_headers
#' @importFrom httr2 req_perform
#' @importFrom httr2 req_url_path_append
#' @importFrom httr2 req_url_query
#' @importFrom httr2 req_user_agent
#' @importFrom httr2 request
#' @importFrom httr2 resp_body_json
Expand Down
31 changes: 30 additions & 1 deletion man/bb_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b75dc3b

Please sign in to comment.