Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: A new user experience for connectapi #305

Open
nealrichardson opened this issue Sep 12, 2024 · 0 comments
Open

RFC: A new user experience for connectapi #305

nealrichardson opened this issue Sep 12, 2024 · 0 comments

Comments

@nealrichardson
Copy link
Collaborator

Follow pattern of posit-sdk: base Resources and Resource classes, containers and object inheriting from them.

Resources (collections) have an as.data.frame() method to return a data frame (like the get_content() etc. functions do now). Otherwise they are just interfaces.

Assuming R6, you could chain things together with $, like client$users$get_by_id(1)$update(first_name = "Jane"), but that's not idiomatic R. So we can add some helper functions like this:

users <- function(object) {
  object$users
}

# actually, update is a S3 generic in stats, so you'd do this with methods
update <- function(object, ...) {
  object$update(...)
}

Then you can pipe.

client |>
  users() |>
  create(first_name = "John", last_name = "Doe")

client |>
  users() |>
  get_by_id(1) |>
  update(first_name = "Jane")

client |>
  content() |>
  as.data.frame()

client |>
  content() |>
  get_all() # returns a list

Defer all requests until actually needed. I.e. client |> users() doesn't request all users because you don't need them if you're then going to do create() or get_by_id().

The reason to use R6 would be for memoizing the requests. For example, if you do client |> users() |> get_by_id(1), we still haven't made any HTTP requests. Only if you do client |> users() |> get_by_id(1) |> first_name() would we actually make the request, then cache the response data in the object, so that if we then did last_name(), we wouldn't have to request again. And if we instead did client |> users() |> get_by_id(1) |> update(first_name == "Jane"), we would only make the PATCH request, no GET.

We could also handle this caching at the HTTP layer, and then we wouldn't necessarily benefit from R6. https://enpiar.com/r/httpcache/ is a package I wrote for this a while back that we could use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant