Skip to content

Commit

Permalink
Merge pull request #639 from DyfanJones/config_flattern
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones authored Jul 4, 2023
2 parents 71c6594 + c3fb7d8 commit 69e98ed
Show file tree
Hide file tree
Showing 20 changed files with 684 additions and 37 deletions.
3 changes: 2 additions & 1 deletion make.paws/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: make.paws
Type: Package
Title: Generate Paws AWS SDKs for R
Version: 0.5.1
Version: 0.5.2
Authors@R: c(
person("David", "Kretch", email = "david.kretch@gmail.com", role = "aut"),
person("Adam", "Banker", email = "adam.banker39@gmail.com", role = "aut"),
Expand Down Expand Up @@ -66,3 +66,4 @@ Collate:
'aws_services.R'
'sdk_helper.R'
'cran_sub_category.R'
'paws_helper_fun.R'
1 change: 1 addition & 0 deletions make.paws/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(paws_release_sub_cat)
export(paws_release_cat)
export(paws_release)
export(paws_update_version)
export(paws_service_param_fn)
import(data.table)
import(fs)
importFrom(paws.common,is_empty)
Expand Down
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
22 changes: 14 additions & 8 deletions make.paws/R/docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,23 @@ comment_bold <- function(x){

# create roxygen2 list from list
comment_list_item <- function(items = list()){
items <- sprintf(
"\\item{%s:} {%s}", comment_bold(names(items)), items
)
return(items)
items_names <- names(items)
items_list <- setNames(character(length(items_names)), items_names)
for (i in items_names) {
if (is.list(items[[i]])) {
items_list[i] <- sprintf("\\item{%s:} {%s}", comment_bold(i), comment_list_itemize(items[[i]]))
} else {
items_list[i] <- sprintf("\\item{%s:} {%s}", comment_bold(i), items[[i]])
}
}
return(paste(items_list, collapse = "\n"))
}

comment_list_itemize <- function(items){
return(paste(
"\\itemize{", paste(comment_list_item(items), collapse = "\n"), "}",
sep = "\n"
)
paste(
"\\itemize{",
comment_list_item(items),
"}", sep = "\n"
)
}

Expand Down
64 changes: 64 additions & 0 deletions make.paws/R/paws_helper_fun.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.param_not_used <- c(
"expiration",
"force_refresh",
"endpoint_resolver",
"enforce_should_retry_check",
"disable_ssl",
"max_retries",
"retryer",
"disable_param_validation",
"disable_compute_checksums",
"s3_disable_100_continue",
"s3_use_accelerate",
"s3_disable_content_md5_validation",
"ec2_metadata_disable_timeout_override",
"use_dual_stack",
"sleep_delay",
"disable_rest_protocol_uri_cleaning",
"provider",
"provider_name"
)

firstup <- function(x) {
substr(x, 1, 1) <- toupper(substr(x, 1, 1))
return(x)
}

get_service_parameter <- function(param) {
fn <- get(param, envir = getNamespace("paws.common"))
elements <- as.list(formals(fn))
elements <- elements[!(names(elements) %in% .param_not_used)]

for (nms in names(elements)) {
if (is.list(elements[[nms]])) {
elements[[nms]] <- get_service_parameter(firstup(nms))
}
}
return(elements)
}

build_service_parameter <- function(param) {
return(do.call(
paws.common:::struct,
get_service_parameter(param)
))
}

#' @title Create paws serice helper functions
#' @param dir directory of paws.common
#' @export
paws_service_param_fn <- function(dir = "../paws.common") {
file <- system.file(
file.path("templates","service_parameter_helper.R"), package = "make.paws"
)
r_file <- readLines(file)

for (param in c("config", "credentials", "creds")) {
line <- grep(sprintf("\\{\\{%s\\}\\}", param), r_file)
fn_body <- deparse(build_service_parameter(firstup(param)))
fn_body[[1]] <- sprintf("%s <- %s", param, fn_body[[1]])
r_file[[line]] <- paste(fn_body, collapse = "\n")
}
writeLines(r_file, file.path(dir, "R", "service_parameter_helper.R"))
roxygen2::roxygenise(dir)
}
91 changes: 73 additions & 18 deletions make.paws/R/service.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NULL
service_file_template <- template(
`
# This file is generated by make.paws. Please do not edit here.
#' @importFrom paws.common new_handlers new_service set_config
#' @importFrom paws.common new_handlers new_service set_config merge_config
NULL

#' ${title}
Expand All @@ -23,7 +23,15 @@ service_file_template <- template(
#'
#' @rdname ${service}
#' @export
${service} <- function(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 @@ -99,33 +107,68 @@ 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",
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."
credentials = list(
creds = 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.",
endpoint = "The complete URL to use for the constructed client.",
region = "The AWS Region used in instantiating the client."
),
anonymous = "Set anonymous credentials.",
endpoint = "The complete URL to use for the constructed client.",
region = "The AWS Region used in instantiating the client.",
close_connection = "Immediately close all HTTP connections.",
timeout = paste(
"The time in seconds till a timeout exception is thrown when attempting",
"to make a connection. The default is 60 seconds."
),
s3_force_path_style = paste(
"Set this to `true` to force the request to use path-style addressing,",
"i.e., `http://s3.amazonaws.com/BUCKET/KEY`."
"i.e. `http://s3.amazonaws.com/BUCKET/KEY`."
),
sts_regional_endpoint = paste(
"Set sts regional endpoint resolver to regional or legacy",
"\\url{https://docs.aws.amazon.com/sdkref/latest/guide/feature-sts-regionalized-endpoints.html}"
)
)
)
desc <- c(desc, comment_list_itemize(config))
desc <- comment(paste(desc, collapse = "\n"), "#'")
paste("@param", param, desc, sep = "\n")
param <- paste("@param", param, desc, sep = "\n")

kwargs <- comment(paste("credentials", collapse = "\n"), "#'")
desc <- "Optional credentials shorthand for the config parameter"
credentials <- list(
creds = 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(credentials))
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"), "#'")
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 @@ -149,8 +192,20 @@ service_syntax <- function(api) {
region = "string",
close_connection = "logical",
timeout = "numeric",
s3_force_path_style = "logical"
)
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
76 changes: 76 additions & 0 deletions make.paws/inst/templates/service_parameter_helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
################################################################
# Generated by make.paws: do not edit by hand
# Please edit: make.paws/inst/templates/set_service_parameter.R
################################################################

#' @title Set service parameters
#' @description
#' Help functions for setting the parameters for services
#'
#' @param credentials \code{credentials()} or \code{list} is same format.
#' \itemize{
#' \item{\strong{creds}:} {\code{creds()} or \code{list} in same format.
#' \itemize{
#' \item{\strong{access_key_id}:} {AWS access key ID}
#' \item{\strong{secret_access_key}:} {AWS secret access key}
#' \item{\strong{session_token}:} {AWS temporary session token}
#' \item{\strong{access_token}:} {The token issued by the \code{CreateToken} API call. For more information, see
#' \href{https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/API_CreateToken.html}{CreateToken}
#' in the `IAM Identity Center OIDC API Reference Guide`.}
#' }}
#' \item{\strong{profile}:} {The name of a profile to use. If not given, then the default profile is used.}
#' \item{\strong{anonymous}:} {Set anonymous credentials.}
#' }
#' @param endpoint The complete URL to use for the constructed client.
#' @param region The AWS Region used in instantiating the client.
#' @param close_connection Immediately close all HTTP connections.
#' @param connect_timeout The time in seconds till a timeout exception is thrown
#' when attempting to make a connection. The default is 60 seconds.
#' @param s3_force_path_style Set this to `true` to force the request to use path-style
#' addressing, i.e. `http://s3.amazonaws.com/BUCKET/KEY`.
#' @param sts_regional_endpoint Set sts regional endpoint resolver to regional or
#' legacy \url{https://docs.aws.amazon.com/sdkref/latest/guide/feature-sts-regionalized-endpoints.html}
#' @param creds \code{creds()} or \code{list} in same format.
#' \itemize{
#' \item{\strong{access_key_id}:} {AWS access key ID}
#' \item{\strong{secret_access_key}:} {AWS secret access key}
#' \item{\strong{session_token}:} {AWS temporary session token}
#' \item{\strong{access_token}:} {The token issued by the \code{CreateToken} API call. For more information, see
#' \href{https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/API_CreateToken.html}{CreateToken}
#' in the `IAM Identity Center OIDC API Reference Guide`.
#' }}
#' @param profile The name of a profile to use. If not given, then the default profile is used.
#' @param anonymous Set anonymous credentials.
#' @param access_key_id AWS access key ID
#' @param secret_access_key AWS secret access key
#' @param session_token AWS temporary session token
#' @param access_token The token issued by the \code{CreateToken} API call. For more information, see
#' \href{https://docs.aws.amazon.com/singlesignon/latest/OIDCAPIReference/API_CreateToken.html}{CreateToken}
#' in the `IAM Identity Center OIDC API Reference Guide`.
#' @return list set of parameter variables for paws services.
#' @examples
#' # set service parameter access_key_id and secret_access_key
#'
#' config(credentials(creds("dummy", "secret")))
#'
#' # set service parameter access_key_id and secret_access_key using using lists
#' config(
#' credentials = list(
#' creds = list(
#' access_key_id = "dummy",
#' secret_access_key = "secret"
#' )
#' )
#' )
#'
#' @name set_service_parameter
#' @export
{{config}}

#' @rdname set_service_parameter
#' @export
{{credentials}}

#' @rdname set_service_parameter
#' @export
{{creds}}
14 changes: 14 additions & 0 deletions make.paws/man/paws_service_param_fn.Rd

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

6 changes: 6 additions & 0 deletions make.paws/tests/testthat/test_docs.R
Original file line number Diff line number Diff line change
Expand Up @@ -705,3 +705,9 @@ test_that("comment_list_itemize", {
expect <- "\\itemize{\n\\item{\\strong{foo}:} {bar}\n}"
expect_equal(comment_list_itemize(config), expect)
})

test_that("comment_list_itemize nested list", {
config <- list(foo = "bar", baz = list(qux = "ham"))
expect <- "\\itemize{\n\\item{\\strong{foo}:} {bar}\n\\item{\\strong{baz}:} {\\itemize{\n\\item{\\strong{qux}:} {ham}\n}}\n}"
expect_equal(comment_list_itemize(config), expect)
})
1 change: 1 addition & 0 deletions paws.common/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Collate:
'tags.R'
'queryutil.R'
'request.R'
'service_parameter_helper.R'
'signer_v4.R'
'signer_query.R'
'signer_s3.R'
Expand Down
Loading

0 comments on commit 69e98ed

Please sign in to comment.