-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
29 changed files
with
515 additions
and
515 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#' Specify request options | ||
#' | ||
#' @param .get A length-1 character vector representing whether the request is | ||
#' for the endpoint's `"data"`, `"criteria"`, `"labels"`, or `"schema"`. | ||
#' @param .url API base URL for request. | ||
#' @param .key API key for request. | ||
#' @param .paginate If `TRUE`, paginate the request. | ||
#' @export | ||
req_opts <- function(.get = "data", | ||
.url = get_env_url(), | ||
.key = get_env_key(), | ||
.paginate = TRUE) { | ||
if(.key != get_env_key()) { | ||
cli::cli_warn(c( | ||
"The {.arg .key} you provided is not your | ||
MEGAMATION_KEY environment variable.", | ||
"i" = "It is highly recommended that you run {.fun mm_set_creds}, | ||
and {.emph do not} supply {.arg .key}.", | ||
"i" = 'A typo like `kee = <your-secret>` | ||
will end up in the request URL as a filter.' | ||
)) | ||
} | ||
check_string(.get) | ||
.get <- rlang::arg_match(.get, c("criteria", "labels", "schema", "data")) | ||
|
||
structure( | ||
list( | ||
.get = .get, | ||
.url = .url, | ||
.key = .key, | ||
.paginate = .paginate | ||
), | ||
class = "megamation_req_opts" | ||
) | ||
} | ||
|
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,125 +1,80 @@ | ||
#' Parse body from Megamation API response | ||
#' | ||
#' `body_parse()` converts the raw bytes of an API response to an R list object. | ||
#' After converting bytes to characters, encoding is done to resolve | ||
#' a UTF-8 issue from Megamation's side. | ||
#' | ||
#' @param resp_body An API response body. | ||
#' @returns A list. | ||
#' @export | ||
body_parse <- function(resp_body) { | ||
resp_body |> | ||
rawToChar() |> | ||
stringi::stri_encode(from = "UTF-8", to = "UTF-8") |> | ||
jsonlite::fromJSON() | ||
} | ||
|
||
#' Extract and parse body from Megamation API response | ||
#' | ||
#' `resp_body_parse()` extracts the raw bytes from an API response and parses | ||
#' it, returning an R list object. | ||
#' `mm_resp_parse()` extracts the raw bytes from an API response and parses | ||
#' it, returning an R list object. After converting bytes to characters, encoding is done to resolve | ||
#' a UTF-8 issue from Megamation's side. | ||
#' | ||
#' @param resp An API response. | ||
#' @description The body of the response contains raw bytes. | ||
#' After converting these bytes to a string, encoding is done to resolve | ||
#' a UTF-8 issue from Megamation's side. | ||
#' @returns A list. | ||
#' @export | ||
resp_body_parse <- function(resp) { | ||
mm_resp_parse <- function(resp) { | ||
resp |> | ||
httr2::resp_body_raw() |> | ||
body_parse() | ||
rawToChar() |> | ||
stringi::stri_encode(from = "UTF-8", to = "UTF-8") |> | ||
jsonlite::fromJSON() | ||
} | ||
|
||
#' Create tibble from parsed response | ||
#' | ||
#' @description | ||
#' | ||
#' `parsed_to_tbl()` constructs a data frame of class [tbl_df]. | ||
#' The input should be the parsed API response body list. | ||
#' | ||
#' `parsed_to_tbl()` uses the following methods: | ||
#' | ||
#' * `to_tbl_data()` converts the Get Data parsed body, but only keeps the | ||
#' embedded data frame. | ||
#' * `to_tbl_criteria()` converts the Get Criteria or Labels parsed body. | ||
#' * `to_tbl_schema()` converts the Get Schema parsed body. | ||
#' | ||
#' `parsed_extract()` extracts a data frame from the parsed | ||
#' response body. | ||
#' | ||
#' @param parsed Parsed response body. | ||
#' @inheritParams mm_request | ||
#' @returns A data frame of class [tbl_df] containing either | ||
#' * Columns of the endpoint data. | ||
#' * Columns `list_name` and `value` representing the list given by the | ||
#' endpoint's Criteria, Labels, or Schema. If Schema, | ||
#' `value` is of type `list` because of the lists under names `required` and | ||
#' `properties`. | ||
#' @returns A data frame containing the endpoint data. | ||
#' | ||
#' @export | ||
parsed_to_tbl <- function(parsed, | ||
parsed_extract <- function(parsed, | ||
.get = c("data", "criteria", "labels", "schema")) { | ||
.get <- rlang::arg_match(.get) | ||
|
||
switch( | ||
.get, | ||
data = to_tbl_data(parsed), | ||
data = extract_data(parsed), | ||
labels = , | ||
criteria = to_tbl_criteria(parsed), | ||
schema = to_tbl_schema(parsed) | ||
criteria = extract_criteria(parsed), | ||
schema = extract_schema(parsed) | ||
) | ||
} | ||
|
||
#' @rdname parsed_to_tbl | ||
#' @rdname parsed_extract | ||
#' @export | ||
to_tbl_data <- function(parsed) { | ||
extract_data <- function(parsed) { | ||
parsed |> | ||
parsed_keep_df() |> | ||
tibble::as_tibble() | ||
} | ||
|
||
#' @rdname parsed_to_tbl | ||
#' @export | ||
to_tbl_criteria <- function(parsed) { | ||
tibble::tibble( | ||
list_name = names(parsed), | ||
value = unlist(parsed) | ||
) | ||
purrr::list_flatten() |> | ||
purrr::keep(\(x) is.data.frame(x)) |> | ||
purrr::pluck(1) | ||
} | ||
|
||
#' @rdname parsed_to_tbl | ||
#' @rdname parsed_extract | ||
#' @export | ||
to_tbl_schema <- function(parsed) { | ||
|
||
list_name <- type <- NULL | ||
|
||
properties <- parsed$properties | ||
|
||
tbl <- tibble::tibble( | ||
list_name = names(parsed), | ||
value = unname(parsed) | ||
) |> | ||
dplyr::filter(list_name != "properties") | ||
extract_criteria <- function(parsed) { | ||
not_cols <- c("Table", "Criteria", "Usage") | ||
|
||
properties <- tibble::tibble( | ||
column = names(properties), | ||
description = purrr::map_chr(properties, "description"), | ||
type = purrr::map(properties, "type") | ||
data.frame( | ||
field = names(parsed) |> tolower(), | ||
description = unlist(parsed) | ||
) |> | ||
tidyr::unnest(type) | ||
|
||
tbl |> | ||
dplyr::add_row( | ||
list_name = "col_properties", | ||
value = list(properties) | ||
) | ||
dplyr::filter(!field %in% !!not_cols) | ||
} | ||
|
||
#' @rdname parsed_to_tbl | ||
#' @rdname parsed_extract | ||
#' @export | ||
parsed_keep_df <- function(parsed) { | ||
parsed |> | ||
purrr::list_flatten() |> | ||
purrr::keep(\(x) is.data.frame(x)) |> | ||
purrr::pluck(1) | ||
extract_schema <- function(parsed) { | ||
p <- parsed$properties | ||
|
||
tibble::tibble( | ||
field = names(p) |> tolower(), | ||
description = purrr::map_chr(p, "description"), | ||
type = purrr::map(p, "type") | ||
) |> | ||
tidyr::unnest(type) |> | ||
as.data.frame() | ||
} | ||
|
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.