paws icon indicating copy to clipboard operation
paws copied to clipboard

Enhancements to service construction?

Open hadley opened this issue 4 years ago • 1 comments

Instead of

svc <- acm(
  config = list(
    credentials = list(
      creds = list(
        access_key_id = "string",
        secret_access_key = "string",
        session_token = "string"
      ),
      profile = "string"
    ),
    endpoint = "string",
    region = "string"
  )
)

Could we have this?

svc <- acm(
  credentials = list(
    creds = list(
      access_key_id = "string",
      secret_access_key = "string",
      session_token = "string"
    ),
    profile = "string"
  ),
  endpoint = "string",
  region = "string"
)

This makes life easier because auto-complete works, and you could document the individual parameters with a little more detail. You could make the change backward compatible by continuing to provide the config parameter and then combining it with the list generated by the other parameters.

It would also be useful if NULL values were automatically dropped, so that if you're wrapping in a function, you can default those arguments to NULL in your function.

hadley avatar May 19 '21 13:05 hadley

Yeah, that sounds good. I think that should be pretty straightforward.

davidkretch avatar May 20 '21 23:05 davidkretch

@hadley @davidkretch sorry for taking so long to address this issue.

Original

svc <- acm(
  config = list(
    credentials = list(
      creds = list(
        access_key_id = "string",
        secret_access_key = "string",
        session_token = "string"
      ),
      profile = "string"
    ),
    endpoint = "string",
    region = "string"
  )
)

Proposal 1

As suggested by @hadley

svc <- acm(
  credentials = list(
    creds = list(
      access_key_id = "string",
      secret_access_key = "string",
      session_token = "string"
    ),
    profile = "string"
  ),
  endpoint = "string",
  region = "string"
)

My concerns with this approach would be:

  • breaking change to dependent packages
  • anytime we do an update to the Config struct in paws.common for new parameters we will need to do a paws regen to pick up the new parameter. For example PR https://github.com/paws-r/paws/pull/636 adds sts_regional_endpoint.

However we could get around this by keeping the config parameter plus it would prevent breaking dependent packages.

# modified to keep the config parameter
svc <- acm(
  credentials = list(
    creds = list(
      access_key_id = "string",
      secret_access_key = "string",
      session_token = "string"
    ),
    profile = "string"
  ),
  endpoint = "string",
  region = "string",
  config = list()
)

Proposal 2

svc <- acm(config = list(), ...)

This allows the config parameters to be called at "root" for example:

svc <- acm(
  access_key_id = "string",
  secret_access_key = "string",
  session_token = "string",
  profile = "string",
  endpoint = "string",
  region = "string"
)

The down side to this it doesn't address the auto-complete functionality, but it does make initialising the service a little easier. I have a PR https://github.com/paws-r/paws/pull/639 toying with this idea.

What's everyone view on this?

DyfanJones avatar Jun 28 '23 22:06 DyfanJones

I think you should keep the config argument, and just combine with credentials, endpoint, etc. I don't think there's much benefit to using ... in this way, as it's hard to document.

hadley avatar Jun 29 '23 12:06 hadley

Ok the only issue we have now is credentials is a list parameter and is doesn't support auto-compelete. After chatting with @davidkretch paws.common has a nifty class called struct that does help with auto-complete

get_service_parameter <- function(param) {
  firstup <- function(x) {
    substr(x, 1, 1) <- toupper(substr(x, 1, 1))
    x
  }
  fn <- get(param, envir = getNamespace("paws.common"))
  elements <- fn()
  elements <- elements[grep("provider", names(elements), perl = T, invert = T)]
  
  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(
      struct,
      get_service_parameter(param)
    )
  )
}

SetConfig <- build_service_parameter("Config")
SetCredentials <- build_service_parameter("Credentials")
SetCreds <- build_service_parameter("Creds")

Screenshot 2023-06-30 at 15 57 06

It will also allow for mix lists and structs together

Screenshot 2023-06-30 at 16 05 03

Also it is handy when they are nested together.

Screenshot 2023-06-30 at 16 07 54

I believe these could be possible helper functions.

What is everyones thoughts on this?

DyfanJones avatar Jun 30 '23 15:06 DyfanJones

Update of progress:

The helper functions will snake case to be consistent with the rest of paws exported functions and methods.

Short term the helper functions can be accessed by loading paws.common into session.

library(paws.common)
s3 <- paws::s3(config(credentials(profile = "paws")))
s3$list_buckets()

Long term we would want to re-export the helper functions to all paws service packages so that paws.common doesn't need to loaded explicitly. Plus in combination with the extra service parameters.

s3 <- paws::s3(config(credentials(profile = "paws")))
s3$list_buckets()

# or

s3 <- paws::s3(credentials = credentials(profile = "paws"))
s3$list_buckets()

# or

s3 <- paws::s3(credentials = list(profile = "paws"))
s3$list_buckets()

DyfanJones avatar Jul 03 '23 17:07 DyfanJones

paws v-0.4.0 has now been released to the cran. I will close this ticket for now.

DyfanJones avatar Sep 15 '23 16:09 DyfanJones