-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a63b0f4
commit 552b624
Showing
20 changed files
with
4,880 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.