Skip to content

Commit 9fd4116

Browse files
refactor: remove unnecessary get_connect(), get_content(), get_variant() R6 methods (#466)
1 parent 2522605 commit 9fd4116

22 files changed

+140
-215
lines changed

R/connect.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ Connect <- R6::R6Class(
3636
#' @field using_auth Indicates that the API key is added to each HTTP call.
3737
using_auth = TRUE,
3838

39-
#' @description Return this connect.
40-
get_connect = function() {
41-
self
42-
},
43-
4439
#' @description Initialize a new connect.
4540
#' @param server The base URL of your Posit Connect server.
4641
#' @param api_key Your Posit Connect API key.

R/content.R

Lines changed: 60 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,17 @@ Content <- R6::R6Class(
2222
# at least guid, url, title to be functional
2323
self$content <- content
2424
},
25-
#' @description Returns the `Connect` instance.
26-
get_connect = function() {
27-
self$connect
28-
},
29-
#' @description Returns the underlying content data.
30-
get_content = function() {
31-
self$content
32-
},
25+
3326
#' @description Obtain the content data from the Connect server.
3427
get_content_remote = function() {
35-
new_content_details <- self$get_connect()$content(self$get_content()$guid)
28+
new_content_details <- self$connect$content(self$content$guid)
3629
self$content <- new_content_details
37-
self$get_content()
30+
self$content
3831
},
3932
#' @description Return the set of content bundles.
4033
get_bundles = function() {
41-
url <- v1_url("content", self$get_content()$guid, "bundles")
42-
self$get_connect()$GET(url)
34+
url <- v1_url("content", self$content$guid, "bundles")
35+
self$connect$GET(url)
4336
},
4437
#' @description Download the source archive for a content bundle.
4538
#' @param bundle_id The bundle identifer.
@@ -52,12 +45,12 @@ Content <- R6::R6Class(
5245
) {
5346
url <- v1_url(
5447
"content",
55-
self$get_content()$guid,
48+
self$content$guid,
5649
"bundles",
5750
bundle_id,
5851
"download"
5952
)
60-
self$get_connect()$GET(
53+
self$connect$GET(
6154
url,
6255
httr::write_disk(filename, overwrite = overwrite),
6356
parser = "raw"
@@ -67,20 +60,20 @@ Content <- R6::R6Class(
6760
#' @description Delete a content bundle.
6861
#' @param bundle_id The bundle identifer.
6962
bundle_delete = function(bundle_id) {
70-
url <- v1_url("content", self$get_content()$guid, "bundles", bundle_id)
71-
self$get_connect()$DELETE(url)
63+
url <- v1_url("content", self$content$guid, "bundles", bundle_id)
64+
self$connect$DELETE(url)
7265
},
7366
#' @description Get this (remote) content item.
7467
internal_content = function() {
75-
url <- unversioned_url("applications", self$get_content()$guid)
76-
self$get_connect()$GET(url)
68+
url <- unversioned_url("applications", self$content$guid)
69+
self$connect$GET(url)
7770
},
7871
#' @description Update this content item.
7972
#' @param ... Content fields.
8073
update = function(...) {
81-
con <- self$get_connect()
74+
con <- self$connect
8275
error_if_less_than(con$version, "1.8.6")
83-
url <- v1_url("content", self$get_content()$guid)
76+
url <- v1_url("content", self$content$guid)
8477
body <- rlang::list2(...)
8578
if (length(body)) {
8679
# Only need to make a request if there are changes
@@ -90,13 +83,13 @@ Content <- R6::R6Class(
9083
},
9184
#' @description Delete this content item.
9285
danger_delete = function() {
93-
con <- self$get_connect()
94-
url <- v1_url("content", self$get_content()$guid)
86+
con <- self$connect
87+
url <- v1_url("content", self$content$guid)
9588
con$DELETE(url)
9689
},
9790
#' @description Return the URL for this content.
9891
get_url = function() {
99-
self$get_content()$content_url
92+
self$content$content_url
10093
},
10194
#' @description Return the URL for this content in the Posit Connect dashboard.
10295
#' @param pane The pane in the dashboard to link to.
@@ -170,13 +163,13 @@ Content <- R6::R6Class(
170163
warn_experimental("job")
171164
url <- unversioned_url(
172165
"applications",
173-
self$get_content()$guid,
166+
self$content$guid,
174167
"job",
175168
key
176169
)
177-
res <- self$get_connect()$GET(url)
170+
res <- self$connect$GET(url)
178171

179-
content_guid <- self$get_content()$guid
172+
content_guid <- self$content$guid
180173
purrr::map(
181174
list(res),
182175
~ purrr::list_modify(.x, app_guid = content_guid)
@@ -198,40 +191,40 @@ Content <- R6::R6Class(
198191
warn_experimental("variants")
199192
url <- unversioned_url(
200193
"applications",
201-
self$get_content()$guid,
194+
self$content$guid,
202195
"variants"
203196
)
204-
self$get_connect()$GET(url)
197+
self$connect$GET(url)
205198
},
206199
#' @description Set a tag for this content.
207200
#' @param tag_id The tag identifier.
208201
tag_set = function(tag_id) {
209-
self$get_connect()$set_content_tag(
210-
self$get_content()$guid,
202+
self$connect$set_content_tag(
203+
self$content$guid,
211204
tag_id = tag_id
212205
)
213206
},
214207
#' @description Remove a tag for this content.
215208
#' @param tag_id The tag identifier.
216209
tag_delete = function(tag_id) {
217210
# note that deleting the parent tag deletes all children
218-
self$get_connect()$remove_content_tag(
219-
self$get_content()$guid,
211+
self$connect$remove_content_tag(
212+
self$content$guid,
220213
tag_id = tag_id
221214
)
222215
},
223216
#' @description The tags for this content.
224217
tags = function() {
225-
url <- v1_url("content", self$get_content()$guid, "tags")
226-
self$get_connect()$GET(url)
218+
url <- v1_url("content", self$content$guid, "tags")
219+
self$connect$GET(url)
227220
},
228221
#' @description Add a principal to the ACL for this content.
229222
#' @param principal_guid GUID for the target user or group.
230223
#' @param principal_type Acting on user or group.
231224
#' @param role The kind of content access.
232225
permissions_add = function(principal_guid, principal_type, role) {
233-
url <- v1_url("content", self$get_content()$guid, "permissions")
234-
self$get_connect()$POST(
226+
url <- v1_url("content", self$content$guid, "permissions")
227+
self$connect$POST(
235228
url,
236229
body = list(
237230
principal_guid = principal_guid,
@@ -246,8 +239,8 @@ Content <- R6::R6Class(
246239
#' @param principal_type Acting on user or group.
247240
#' @param role The kind of content access.
248241
permissions_update = function(id, principal_guid, principal_type, role) {
249-
url <- v1_url("content", self$get_content()$guid, "permissions", id)
250-
self$get_connect()$PUT(
242+
url <- v1_url("content", self$content$guid, "permissions", id)
243+
self$connect$PUT(
251244
url,
252245
body = list(
253246
principal_guid = principal_guid,
@@ -259,28 +252,28 @@ Content <- R6::R6Class(
259252
#' @description Remove an entry from the ACL for this content.
260253
#' @param id The target identifier.
261254
permissions_delete = function(id) {
262-
url <- v1_url("content", self$get_content()$guid, "permissions", id)
263-
self$get_connect()$DELETE(url)
255+
url <- v1_url("content", self$content$guid, "permissions", id)
256+
self$connect$DELETE(url)
264257
},
265258
#' @description Obtain some or all of the ACL for this content.
266259
#' @param id The target identifier.
267260
#' @param add_owner Include the content owner in the result set.
268261
permissions = function(id = NULL, add_owner = FALSE) {
269-
guid <- self$get_content()$guid
262+
guid <- self$content$guid
270263
if (is.null(id)) {
271-
url <- v1_url("content", self$get_content()$guid, "permissions")
264+
url <- v1_url("content", self$content$guid, "permissions")
272265
} else {
273-
url <- v1_url("content", self$get_content()$guid, "permissions", id)
266+
url <- v1_url("content", self$content$guid, "permissions", id)
274267
}
275-
res <- self$get_connect()$GET(url)
268+
res <- self$connect$GET(url)
276269
# NOTE: the default for the low-level functions is to map to the API
277270
# as close as possible. This differs from the "cleaner UX" functions
278271
if (add_owner) {
279272
owner_entry <- list(
280273
id = NA_character_,
281274
content_guid = guid,
282275
# TODO: what if groups can own content?
283-
principal_guid = self$get_content()$owner_guid,
276+
principal_guid = self$content$owner_guid,
284277
principal_type = "user",
285278
role = "owner"
286279
)
@@ -290,14 +283,14 @@ Content <- R6::R6Class(
290283
},
291284
#' @description Return the environment variables set for this content.
292285
environment = function() {
293-
url <- v1_url("content", self$get_content()$guid, "environment")
294-
self$get_connect()$GET(url)
286+
url <- v1_url("content", self$content$guid, "environment")
287+
self$connect$GET(url)
295288
},
296289
#' @description Adjust the environment variables set for this content.
297290
#' @param ... Environment variable names and values. Use `NA` as the value
298291
#' to unset variables.
299292
environment_set = function(...) {
300-
url <- v1_url("content", self$get_content()$guid, "environment")
293+
url <- v1_url("content", self$content$guid, "environment")
301294
# post with
302295
# key = NA to remove
303296
vals <- rlang::list2(...)
@@ -307,12 +300,12 @@ Content <- R6::R6Class(
307300
})
308301
names(body) <- NULL
309302

310-
self$get_connect()$PATCH(path = url, body = body)
303+
self$connect$PATCH(path = url, body = body)
311304
},
312305
#' @description Overwrite the environment variables set for this content.
313306
#' @param ... Environment variable names and values.
314307
environment_all = function(...) {
315-
url <- v1_url("content", self$get_content()$guid, "environment")
308+
url <- v1_url("content", self$content$guid, "environment")
316309

317310
vals <- rlang::list2(...)
318311
if (length(vals) == 0) {
@@ -326,23 +319,23 @@ Content <- R6::R6Class(
326319
names(body) <- NULL
327320
}
328321

329-
self$get_connect()$PUT(path = url, body = body)
322+
self$connect$PUT(path = url, body = body)
330323
},
331324
#' @description Deploy this content
332325
#' @param bundle_id Target bundle identifier.
333326
deploy = function(bundle_id = NULL) {
334327
body <- list(bundle_id = bundle_id)
335-
self$get_connect()$POST(
336-
v1_url("content", self$get_content()$guid, "deploy"),
328+
self$connect$POST(
329+
v1_url("content", self$content$guid, "deploy"),
337330
body = body
338331
)
339332
},
340333
#' @description Adjust Git polling.
341334
#' @param enabled Polling enabled.
342335
repo_enable = function(enabled = TRUE) {
343336
warn_experimental("repo_enable")
344-
self$get_connect()$PUT(
345-
unversioned_url("applications", self$get_content()$guid, "repo"),
337+
self$connect$PUT(
338+
unversioned_url("applications", self$content$guid, "repo"),
346339
body = list(
347340
enabled = enabled
348341
)
@@ -354,8 +347,8 @@ Content <- R6::R6Class(
354347
#' @param subdirectory Git repository directory
355348
repo_set = function(repository, branch, subdirectory) {
356349
warn_experimental("repo_set")
357-
self$get_connect()$POST(
358-
unversioned_url("applications", self$get_content()$guid, "repo"),
350+
self$connect$POST(
351+
unversioned_url("applications", self$content$guid, "repo"),
359352
body = list(
360353
repository = repository,
361354
branch = branch,
@@ -371,18 +364,18 @@ Content <- R6::R6Class(
371364
#' @param ... Unused.
372365
print = function(...) {
373366
cat("Posit Connect Content: \n")
374-
cat(" Content GUID: ", self$get_content()$guid, "\n", sep = "")
367+
cat(" Content GUID: ", self$content$guid, "\n", sep = "")
375368
cat(
376369
" Content URL: ",
377-
self$get_content()$dashboard_url,
370+
self$content$dashboard_url,
378371
"\n",
379372
sep = ""
380373
)
381-
cat(" Content Title: ", self$get_content()$title, "\n", sep = "")
374+
cat(" Content Title: ", self$content$title, "\n", sep = "")
382375
cat("\n")
383376
cat(
384377
'content_item(client, guid = "',
385-
self$get_content()$guid,
378+
self$content$guid,
386379
'")',
387380
"\n",
388381
sep = ""
@@ -521,7 +514,7 @@ Environment <- R6::R6Class(
521514
get_environment <- function(content) {
522515
validate_R6_class(content, "Content")
523516
content_data <- content$get_content_remote()
524-
connect_client <- content$get_connect()
517+
connect_client <- content$connect
525518
return(Environment$new(connect_client, content_data))
526519
}
527520

@@ -594,7 +587,7 @@ content_item <- function(connect, guid) {
594587
# TODO : think about how to handle if GUID does not exist
595588
validate_R6_class(connect, "Connect")
596589

597-
res <- connect$get_connect()$content(guid)
590+
res <- connect$content(guid)
598591

599592
Content$new(connect = connect, content = res)
600593
}
@@ -617,7 +610,7 @@ content_title <- function(connect, guid, default = "Unknown Content") {
617610

618611
content_title <- tryCatch(
619612
{
620-
res <- suppressMessages(connect$get_connect()$content(guid))
613+
res <- suppressMessages(connect$content(guid))
621614
# TODO: What about length 0?
622615
if (is.null(res$title)) {
623616
return(default)
@@ -991,7 +984,7 @@ content_delete <- function(content, force = FALSE) {
991984

992985
message(glue::glue("Deleting content '{cn$title}' ({cn$guid})"))
993986
res <- content$danger_delete()
994-
content$get_connect()$raise_error(res)
987+
content$connect$raise_error(res)
995988

996989
return(content)
997990
}
@@ -1181,7 +1174,7 @@ delete_bundle <- function(content, bundle_id) {
11811174
"Deleting bundle {bundle_id} for content '{cn$title}' ({cn$guid})"
11821175
))
11831176
res <- content$bundle_delete(bundle_id)
1184-
content$get_connect()$raise_error(res)
1177+
content$connect$raise_error(res)
11851178
return(content)
11861179
}
11871180

@@ -1351,7 +1344,7 @@ get_user_permission <- function(content, guid, add_owner = TRUE) {
13511344
#' @rdname permissions
13521345
#' @export
13531346
get_my_permission <- function(content, add_owner = TRUE) {
1354-
my_guid <- content$get_connect()$GET("me")$guid
1347+
my_guid <- content$connect$GET("me")$guid
13551348
get_user_permission(content, my_guid, add_owner = add_owner)
13561349
}
13571350

0 commit comments

Comments
 (0)