Skip to content

Commit

Permalink
Refactor existing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonthegeek committed Mar 27, 2024
1 parent 614e0bf commit 2ad30d6
Show file tree
Hide file tree
Showing 23 changed files with 266 additions and 215 deletions.
31 changes: 14 additions & 17 deletions R/as_bk_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,19 @@ S7::method(as_bk_data, class_security_schemes) <- function(x) {
if (is.na(description)) {
return(
switch(type,
api_key = .security_scheme_description_api_key(),
api_key = .security_scheme_description_api_key,
NA_character_
)
)
}
return(description) # nocov
}

.security_scheme_description_api_key <- function() {
paste(
"An API key provided by the API provider.",
"This key is not clearly documented in the API description.",
"Check the API documentation for details."
)
}
.security_scheme_description_api_key <- paste(
"An API key provided by the API provider.",
"This key is not clearly documented in the API description.",
"Check the API documentation for details."
)

.security_scheme_collection_finalize <- function(security_schemes) {
security_scheme_data <- c(
Expand All @@ -85,14 +83,14 @@ S7::method(as_bk_data, class_security_schemes) <- function(x) {
security_arg_list = .collapse_comma(
glue("{security_args} = {security_args}")
),
security_arg_helps = .security_arg_help_generate(
security_arg_helps = .generate_security_arg_help(
security_schemes,
security_args
)
))
}

.security_arg_help_generate <- function(security_schemes, security_args) {
.generate_security_arg_help <- function(security_schemes, security_args) {
security_arg_description <- set_names(
map_chr(security_schemes, "description"),
map_chr(security_schemes, "arg_name")
Expand All @@ -102,13 +100,15 @@ S7::method(as_bk_data, class_security_schemes) <- function(x) {
map2(
security_arg_description,
security_args,
function(arg_description, arg_name) {
list(name = arg_name, description = arg_description)
}
.security_arg_description_clean
)
)
}

.security_arg_description_clean <- function(arg_description, arg_name) {
list(name = arg_name, description = arg_description)
}

S7::method(as_bk_data, class_security_scheme_details) <- function(x) {
map(x, as_bk_data)
}
Expand All @@ -118,10 +118,7 @@ S7::method(as_bk_data, class_api_key_security_scheme) <- function(x) {
return(
list(
parameter_name = x@parameter_name,
arg_name = stringr::str_remove(
to_snake_case(x@parameter_name),
"^x_"
),
arg_name = str_remove(to_snake_case(x@parameter_name), "^x_"),
location = x@location,
type = "api_key",
api_key = TRUE
Expand Down
92 changes: 0 additions & 92 deletions R/generate.R

This file was deleted.

11 changes: 8 additions & 3 deletions R/generate_pkg_agent.R → R/generate_pkg-agent.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ generate_pkg_agent <- function(path = ".") {
}
pkg_desc <- desc(file = path)
pkg_name <- pkg_desc$get_field("Package")
pkg_url <- .get_pkg_url(pkg_desc)
return(
glue("{pkg_name}{pkg_url}")
)
}

.get_pkg_url <- function(pkg_desc) {
pkg_url_glue <- ""
pkg_url <- pkg_desc$get_urls()
if (length(pkg_url)) {
pkg_url_glue <- glue(
" ({pkg_url[[1]]})"
)
}
return(
glue("{pkg_name}{pkg_url_glue}")
)
return(pkg_url_glue)
}
31 changes: 31 additions & 0 deletions R/generate_pkg-call.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.generate_call <- function(config, api_definition, pkg_agent, security_data) {
touched_files <- c(
.generate_call_r(config, api_definition, pkg_agent, security_data),
.generate_call_test(config$api_abbr)
)
return(touched_files)
}

.generate_call_r <- function(config, api_definition, pkg_agent, security_data) {
.bk_use_template(
template = "010-call.R",
data = list(
api_title = config$api_title,
api_abbr = config$api_abbr,
base_url = api_definition@servers@url,
pkg_agent = pkg_agent,
has_security = security_data$has_security,
security_arg_helps = security_data$security_arg_helps,
security_signature = security_data$security_signature,
security_arg_list = security_data$security_arg_list
)
)
}

.generate_call_test <- function(api_abbr) {
.bk_use_template(
template = "test-010-call.R",
dir = "tests/testthat",
data = list(api_abbr = api_abbr)
)
}
63 changes: 63 additions & 0 deletions R/generate_pkg-prepare.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' Error if not in package
#'
#' @inheritParams .is_pkg
#'
#' @return `NULL`, invisibly.
#' @keywords internal
.assert_is_pkg <- function(base_path = usethis::proj_get()) {
if (.is_pkg(base_path)) {
return(invisible(NULL))
}
cli::cli_abort(c(
"Can't generate package files outside of a package.",
x = "{.path {base_path}} is not inside a package."
))
}

#' Check whether we're in a package
#'
#' Inspired by usethis:::is_package.
#'
#' @param base_path The root URL of the current project.
#'
#' @return `TRUE` if the project is a package, `FALSE` if not.
#' @keywords internal
.is_pkg <- function(base_path = usethis::proj_get()) {
root_file <- rlang::try_fetch(
rprojroot::find_package_root_file(path = base_path),
error = function(cnd) NULL
)
!is.null(root_file)
}

.read_config <- function(config_file = "_beekeeper.yml") {
config <- yaml::read_yaml(config_file)
config <- .stabilize_config(config)
return(config)
}

.stabilize_config <- function(config) {
config$api_title <- stabilize_string(config$api_title)
config$api_abbr <- stabilize_string(config$api_abbr)
config$api_version <- stabilize_string(config$api_version)
config$rapid_file <- stabilize_string(config$rapid_file)
config$updated_on <- parse_date_time(
config$updated_on,
orders = c("ymd HMS", "ymd H", "ymd")
)
return(config)
}

.read_api_definition <- function(config_file, rapid_file) {
readRDS(
path(path_dir(config_file), rapid_file)
)
}

.prepare_r <- function() {
use_directory("R")
use_testthat()
quietly(use_httptest2)()
use_package("nectar")
use_package("beekeeper", type = "Suggests")
}
6 changes: 3 additions & 3 deletions R/security.R → R/generate_pkg-security.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
.generate_security <- function(api_abbr, security_schemes) {
security_data <- as_bk_data(security_schemes)
if (length(security_data)) {
.bk_use_template(
security_data$security_file_path <- .bk_use_template(
template = "020-security.R",
data = c(security_data, api_abbr = api_abbr)
)
security_data$security_signature <- .security_signature_generate(
security_data$security_signature <- .generate_security_signature(
security_data$security_arg_names, api_abbr
)
}
return(security_data)
}

.security_signature_generate <- function(security_arg_names, api_abbr) {
.generate_security_signature <- function(security_arg_names, api_abbr) {
env_vars <- toupper(glue("{api_abbr}_{security_arg_names}"))
return(
.collapse_comma_newline(c("", glue(
Expand Down
27 changes: 27 additions & 0 deletions R/generate_pkg-template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Use a template in this package
#'
#' @param template The name of the template.
#' @param dir The directory where the file should be created.
#' @param data A list of variables to apply to the template.
#'
#' @return The path to the generated or updated file, invisibly.
#' @keywords internal
.bk_use_template <- function(template,
dir = c("R", "tests/testthat"),
data) {
dir <- match.arg(dir)
target <- proj_path(dir, template)
save_as <- path_rel(target, proj_path())

use_template(
template = template,
save_as = save_as,
data = data,
package = "beekeeper"
)
capture.output({
style_file(target)
})

return(invisible(target))
}
32 changes: 32 additions & 0 deletions R/generate_pkg_main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Use a beekeeper config file to generate code
#'
#' Creates or updates package files based on the information in a beekeeper
#' config file (generated by [use_beekeeper()] or manually). The files enforce
#' an opinionated framework for API packages.
#'
#' @param config_file The path to a beekeeper yaml file.
#' @param pkg_agent A string to identify this package, for use in the
#' `user_agent` argument of [nectar::call_api()].
#'
#' @return A character vector of paths to files that were added or updated,
#' invisibly.
#' @export
generate_pkg <- function(config_file = "_beekeeper.yml",
pkg_agent = generate_pkg_agent(config_file)) {
.assert_is_pkg()
config <- .read_config(config_file)
api_definition <- .read_api_definition(config_file, config$rapid_file)
.prepare_r()
touched_files <- .generate_pkg_impl(config, api_definition, pkg_agent)
return(invisible(touched_files))
}

.generate_pkg_impl <- function(config, api_definition, pkg_agent) {
security_data <- .generate_security(
config$api_abbr,
api_definition@components@security_schemes
)
call_files <- .generate_call(config, api_definition, pkg_agent, security_data)
touched_files <- c(call_files, security_data$security_file_path)
return(invisible(touched_files))
}
Loading

0 comments on commit 2ad30d6

Please sign in to comment.