Skip to content

Commit

Permalink
initial implementation of proposal 1 (paws-r#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones committed Jun 29, 2023
1 parent e9e4272 commit dd78c7e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 30 deletions.
9 changes: 7 additions & 2 deletions make.paws/R/cran_collection.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ get_client_docs <- function(path, service) {

collection_client_template <- template(
`
${service} <- function(config = list(), ...) {
${package}::${service}(config, ...)
${service} <- function(config = list(), credentials = list(), endpoint = NULL, region = NULL) {
${package}::${service}(
config = config,
credentials = credentials,
endpoint = endpoint,
region = region
)
}
`
)
Expand Down
54 changes: 46 additions & 8 deletions make.paws/R/service.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ service_file_template <- template(
#'
#' @rdname ${service}
#' @export
${service} <- function(config = list(), ...) {
config <- merge_config(config, list(...))
${service} <- function(config = list(), credentials = list(), endpoint = NULL, region = NULL) {
config <- merge_config(
config,
list(
credentials = credentials,
endpoint = endpoint,
region = region
)
)
svc <- .${service}$operations
svc <- set_config(svc, config)
return(svc)
Expand Down Expand Up @@ -100,8 +107,7 @@ service_description <- function(api) {

# Return the documentation for the parameters to the service function.
service_params <- function() {
param <- "config"
param <- comment(paste(param, collapse = "\n"), "#'")
param <- comment(paste("config", collapse = "\n"), "#'")
desc <- "Optional configuration of credentials, endpoint, and/or region."
config <- list(
access_key_id = "AWS access key ID",
Expand Down Expand Up @@ -132,10 +138,31 @@ service_params <- function() {
desc <- comment(paste(desc, collapse = "\n"), "#'")
param <- paste("@param", param, desc, sep = "\n")

kwargs <- comment(paste("...", collapse = "\n"), "#'")
desc <- "Optional configuration shorthand for the config parameter"
kwargs <- comment(paste("credentails", collapse = "\n"), "#'")
desc <- "Optional credentials shorthand for the config parameter"
credentails <- list(
access_key_id = "AWS access key ID",
secret_access_key = "AWS secret access key",
session_token = "AWS temporary session token",
profile = paste(
"The name of a profile to use. If not given, then the default profile",
"is used."
),
anonymous = "Set anonymous credentials."
)
desc <- c(desc, comment_list_itemize(credentails))
desc <- comment(paste(desc, collapse = "\n"), "#'")
param <- paste(param, "#' @param", kwargs, desc, sep = "\n")

endpoint <- comment(paste("endpoint", collapse = "\n"), "#'")
desc <- "Optional shorthand for complete URL to use for the constructed client."
desc <- comment(paste(desc, collapse = "\n"), "#'")
paste(param, "#' @param", kwargs, desc, sep = "\n")
param <- paste(param, "#' @param", endpoint, desc, sep = "\n")

region <- comment(paste("region", collapse = "\n"), "#'")
desc <- "Optional shorthand for AWS Region used in instantiating the client."
desc <- comment(paste(desc, collapse = "\n"), "#'")
return(paste(param, "#' @param", region, desc, sep = "\n"))
}

# Return the documentation for the service syntax.
Expand All @@ -161,7 +188,18 @@ service_syntax <- function(api) {
timeout = "numeric",
s3_force_path_style = "logical",
sts_regional_endpoint = "string"
)
),
credentials = list(
creds = list(
access_key_id = "string",
secret_access_key = "string",
session_token = "string"
),
profile = "string",
anonymous = "logical"
),
endpoint = "string",
region = "string"
)
```',
service)
Expand Down
41 changes: 23 additions & 18 deletions paws.common/R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ get_sts_regional_endpoint <- function(profile = "") {
"sts_regional_endpoint" = get_sts_regional_endpoint
)

# Ensures config is built correctly from service parameters
build_config <- function(cfg){
add_list <- function(x) if(length(x) == 0) NULL else x

Expand All @@ -391,32 +392,36 @@ build_config <- function(cfg){

cred_names <- names(Creds())
credentails_names <- names(Credentials())
config_names <- names(Config())

for (nm in cred_names) {
creds[[nm]] <- cfg[[nm]]
}
for (nm in credentails_names) {
credentials[[nm]] <- cfg[[nm]]
}
for (nm in config_names) {
config[[nm]] <- cfg[[nm]]
cred_names <- cred_names[cred_names != "provider_name"]
credentails_names <- credentails_names[credentails_names != "provider"]

for (cfg_name in names(cfg)) {
if (cfg_name == "credentials") {
for (credentails_name in credentails_names) {
if (credentails_name == "creds") {
for (cred_name in cred_names) {
creds[[cred_name]] <- cfg[[cfg_name]][[credentails_name]][[cred_name]]
}
credentials[[credentails_name]] <- add_list(creds)
} else {
credentials[[credentails_name]] <- cfg[[cfg_name]][[credentails_name]]
}
}
config[[cfg_name]] <- add_list(credentials)
} else {
config[[cfg_name]] <- cfg[[cfg_name]]
}
}

credentials$creds <- add_list(creds)
config$credentials <- add_list(credentials)

return(config)
}


#' @title Merges config lists for paws services
#' @description Allows config list to be flatten from shorthand.
#' @param orig_cfg Original config list
#' @param flat_cfg Flatten config list
#' @param param_cfg Config list developed from service parameters
#' @keywords internal
#' @export
merge_config <- function(orig_cfg, flat_cfg) {
built_cfg <- build_config(flat_cfg)
merge_config <- function(orig_cfg, param_cfg) {
built_cfg <- build_config(param_cfg)
return(modifyList(orig_cfg, built_cfg))
}
4 changes: 2 additions & 2 deletions paws.common/man/merge_config.Rd

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

0 comments on commit dd78c7e

Please sign in to comment.