Skip to content

Commit

Permalink
httr2 update
Browse files Browse the repository at this point in the history
  • Loading branch information
philip-khor committed Nov 18, 2023
1 parent a63b0f4 commit 552b624
Show file tree
Hide file tree
Showing 20 changed files with 4,880 additions and 362 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ BugReports: https://github.com/philip-khor/bnmr/issues
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Depends: R (>= 4.1.0)
Imports: httr,
glue,
jsonlite,
purrr,
purrr (>= 1.0.0),
ISOcodes,
tibble,
tidyr,
Expand Down
23 changes: 14 additions & 9 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,21 @@ export(get_volume_interbank_mm)
import(lubridate)
importFrom(assertthat,assert_that)
importFrom(glue,glue)
importFrom(httr,GET)
importFrom(httr,accept)
importFrom(httr,content)
importFrom(httr,http_error)
importFrom(httr,http_type)
importFrom(httr,status_code)
importFrom(httr,user_agent)
importFrom(httr2,req_headers)
importFrom(httr2,req_perform)
importFrom(httr2,req_url_path)
importFrom(httr2,req_user_agent)
importFrom(httr2,request)
importFrom(httr2,resp_body_json)
importFrom(httr2,resp_check_content_type)
importFrom(httr2,resp_is_error)
importFrom(httr2,resp_status)
importFrom(httr2,resp_status_desc)
importFrom(jsonlite,flatten)
importFrom(jsonlite,fromJSON)
importFrom(purrr,discard)
importFrom(purrr,list_rbind)
importFrom(purrr,map)
importFrom(purrr,map_dbl)
importFrom(purrr,map_dfr)
importFrom(rlang,.data)
importFrom(rlang,is_integerish)
importFrom(rlang,is_null)
Expand All @@ -127,3 +130,5 @@ importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tidyr,spread)
importFrom(tidyr,unnest)
importFrom(tidyr,unnest_wider)
importFrom(vctrs,vec_size)
2 changes: 1 addition & 1 deletion R/daily_fx_turnover.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
#' @source https://apikijangportal.bnm.gov.my/
#'
#'
get_daily_fx_turnover <- function() get_bnm_tbl("/fx-turn-over")
get_daily_fx_turnover <- function() get_bnm_data("/fx-turn-over")
4 changes: 2 additions & 2 deletions R/exchange_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ get_exchange_rate <- function(currency = NULL,
quote %in% c("rm", "fx")
)

get_bnm_tbl(path,
get_bnm_data(path,
query = list(
session = session,
quote = quote
Expand Down Expand Up @@ -64,7 +64,7 @@ get_renminbi <- function(type = "dar") {


if (type == "dar") {
rmb_tibble <- get_bnm_tbl(paths[[type]])
rmb_tibble <- get_bnm_data(paths[[type]])
rmb_tibble[["term"]] <- names(rmb_tibble[["deposit"]])
spread(unnest(rmb_tibble, cols = c(.data[["deposit"]])), "term", "deposit")
}
Expand Down
4 changes: 2 additions & 2 deletions R/financial_inclusion.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ get_financing_applied <- function(year = NULL) {
assertthat::is.count(year)
path <- paste0(path, "/", as.character(year))
}
get_bnm_tbl(path)
get_bnm_data(path)
}

#' @describeIn financial_inclusion Financing Approved
Expand All @@ -29,5 +29,5 @@ get_financing_approved <- function(year = NULL) {
assertthat::is.count(year)
path <- paste0(path, "/", as.character(year))
}
get_bnm_tbl(path)
get_bnm_data(path)
}
2 changes: 1 addition & 1 deletion R/interbank_swap.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
#' @source https://apikijangportal.bnm.gov.my/
#'
#'
get_interbank_swap <- function() get_bnm_tbl("/interbank-swap")
get_interbank_swap <- function() get_bnm_data("/interbank-swap")

155 changes: 81 additions & 74 deletions R/main.R
Original file line number Diff line number Diff line change
@@ -1,74 +1,81 @@
#' Access BNM API
#'
#' This function allows you to obtain data from the BNM API.
#' @param path Specifies the API path per https://apikijangportal.bnm.gov.my/
#' @param ... Additional arguments to httr::GET()
#' @examples
#' bnm_api("/welcome")
#' @noRd
#' @importFrom httr GET accept user_agent content http_type http_error status_code
#' @importFrom jsonlite fromJSON
#' @importFrom glue glue
#' @source https://apikijangportal.bnm.gov.my/, https://cran.r-project.org/web/packages/httr/vignettes/api-packages.html
#'

bnm_api <- function(path, ...) {
GET("https://api.bnm.gov.my",
path = glue("public{path}"),
...,
accept("application/vnd.BNM.API.v1+json"),
user_agent("http://github.com/philip-khor/bnmr/")
) -> resp

parsed <- fromJSON(content(resp, "text", encoding = "UTF-8"))

if (http_type(resp) != "application/json") {
stop("API did not return json", call. = FALSE)
}

if (http_error(resp)) {
stop(
sprintf(
"BNM API request failed [%s]\n%s\n<%s>",
status_code(resp),
parsed$message,
parsed$documentation_url
),
call. = TRUE
)
}

structure(
list(
content = parsed,
path = path,
response = resp
),
class = "bnm_api"
)
}

#' Get BNM Data as tibble
#'
#' @param path Specifies the API path per https://apikijangportal.bnm.gov.my/
#' @importFrom tibble as_tibble
#' @importFrom purrr discard
#' @noRd
get_bnm_tbl <- function(path, ...) {
as_tibble(discard(get_bnm_data(path, ...), is.null))
}

#' Get BNM Data
#'
#' This function allows you to obtain data from the BNM API.
#' @param path Specifies the API path per https://apikijangportal.bnm.gov.my/
#' @param ... Additional arguments to be passed to bnm_api
#' @keywords internal cats
#' @examples
#' get_bnm_data("/welcome")
#' @noRd
#' @source https://apikijangportal.bnm.gov.my/

get_bnm_data <- function(path, ...) {
bnm_api(path, ...)[["content"]][["data"]]
}
#' Access BNM API
#'
#' This function allows you to obtain data from the BNM API.
#' @param path Specifies the API path per https://apikijangportal.bnm.gov.my/
#' @param ... Additional arguments to httr::GET()
#' @examples
#' bnm_api("/welcome")
#' @noRd
#' @importFrom httr2 request req_url_path req_user_agent req_headers req_perform resp_check_content_type
#' resp_is_error resp_status resp_status_desc resp_body_json
#' @importFrom glue glue
#' @source https://apikijangportal.bnm.gov.my/, https://cran.r-project.org/web/packages/httr/vignettes/api-packages.html
#'
bnm_api_req <- function(path, ...) {
request("https://api.bnm.gov.my") |>
req_url_path(glue("public{path}")) |>
req_user_agent("http://github.com/philip-khor/bnmr/") |>
req_headers(Accept = "application/vnd.BNM.API.v1+json", ...)
}

bnm_api <- function(path, ...) {
bnm_api_req(path, ...) |>
req_perform() -> resp

try(resp_check_content_type(resp, "application/json"))

if (resp_is_error(resp)) {
stop(
sprintf(
"BNM API request failed [%s]\n%s\n<%s>",
resp_status(resp),
resp_status_desc(resp)
),
call. = TRUE
)
}

resp_body_json(resp, encoding = "UTF-8") -> parsed

structure(
list(
content = parsed,
path = path,
response = resp
),
class = "bnm_api"
)
}

#' Get BNM Data as tibble
#'
#' @param path Specifies the API path per https://apikijangportal.bnm.gov.my/
#' @importFrom tibble as_tibble
#' @importFrom purrr discard map list_rbind
#' @importFrom tidyr unnest_wider
#' @importFrom vctrs vec_size
#' @noRd
get_bnm_tbl <- function(path, ...) {
get_bnm_data(path, ...) |>
discard(is.null) -> dat
if (vec_size(dat) == 1) {
tibble(dat)
} else {
tibble(dat) |> unnest_wider(dat)
}
}

#' Get BNM Data
#'
#' This function allows you to obtain data from the BNM API.
#' @param path Specifies the API path per https://apikijangportal.bnm.gov.my/
#' @param ... Additional arguments to be passed to bnm_api
#' @keywords internal cats
#' @examples
#' get_bnm_data("/welcome")
#' @noRd
#' @source https://apikijangportal.bnm.gov.my/

get_bnm_data <- function(path, ...) {
bnm_api(path, ...)[["content"]][["data"]]
}
13 changes: 7 additions & 6 deletions R/monthly.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Get monthly data from endpoints with year/month/date params
#'
#' @importFrom purrr map_dfr
#' @importFrom purrr map list_rbind
#' @importFrom jsonlite flatten
#' @importFrom rlang list2 qq_show is_scalar_integerish
#' @keywords internal
Expand All @@ -24,25 +24,26 @@
if (is_null(year)) {
get_bnm_data(glue("{stub}"), query = args)
} else {
map_dfr(1:12, function(month) {
map(1:12, function(month) {
Sys.sleep(1)
flatten(get_bnm_tbl(glue(year_month_stubby), query = args))
})
flatten(get_bnm_data(glue(year_month_stubby), query = args))
}) |>
list_rbind()
}


} else if (!is_null(year)) {
stopifnot(is_scalar_integerish(year) && is_scalar_integerish(month))

get_bnm_tbl(glue(year_month_stubby), query = args)
get_bnm_data(glue(year_month_stubby), query = args)
} else {
stop("Please provide the year")
}
} else {
if (!is_null(year) || !is_null(month)) {
warning("Date and year/month combination provided; querying based on date")
}
get_bnm_tbl(glue("{stub}/date/{date}"), query = args)
get_bnm_data(glue("{stub}/date/{date}"), query = args)
}
}

Expand Down
Loading

0 comments on commit 552b624

Please sign in to comment.