-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathutils.R
More file actions
221 lines (192 loc) · 6.33 KB
/
utils.R
File metadata and controls
221 lines (192 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Helpers to make it easier to identify where we're calling public APIs and not
v1_url <- function(...) {
paste("v1", ..., sep = "/")
}
unversioned_url <- function(...) {
paste(..., sep = "/")
}
valid_page_size <- function(x, min = 1, max = 500) {
# This could be changed to error if x is outside the range
min(max(min, x), max)
}
generate_R6_print_output <- # nolint: object_name_linter
function() {
con <- Connect$new(server = "http://test_host", api_key = "test_key")
bnd <- Bundle$new(path = "/test/path")
ex_content <- list(guid = "content-guid", title = "content-title", url = "http://content-url")
cnt1 <- Content$new(connect = con, ex_content)
ex_task <- list(task_id = "task-id")
tsk1 <- ContentTask$new(connect = con, content = ex_content, task = ex_task)
ex_vanity <- list(path_prefix = "vanity-prefix")
van1 <- Vanity$new(connect = con, content = ex_content, vanity = ex_vanity)
obj_list <- list(con, bnd, cnt1, tsk1, van1)
unlist(mapply(
function(.x, .y) {
c(
"----------------------------",
.y,
"----------------------------",
capture.output(print(.x))
)
},
.x = obj_list,
.y = lapply(obj_list, function(x) {
class(x)[[1]]
}),
SIMPLIFY = FALSE
))
}
is_R6_class <- # nolint: object_name_linter
function(instance, class) {
return(R6::is.R6(instance) && inherits(instance, class))
}
validate_R6_class <- # nolint: object_name_linter
function(instance, class) {
obj <- rlang::enquo(instance)
if (!R6::is.R6(instance) || !inherits(instance, class)) {
stop(paste(rlang::quo_text(obj), "must be an R6", glue::glue_collapse(class, sep = " or "), "object"))
}
invisible(TRUE)
}
# super useful examples
# https://github.com/tidyverse/tibble/blob/master/R/compat-lifecycle.R
warn_experimental <- function(name) {
if (rlang::is_true(rlang::peek_option("connectapi_disable_experimental_warnings"))) {
return(invisible(NULL))
}
warn_once(
msg = glue::glue("The `{name}` function is experimental and subject to change without warning in a future release"),
id = paste0(name, "-experimental")
)
}
scoped_experimental_silence <- function(frame = rlang::caller_env()) {
rlang::local_options(
.frame = frame,
connectapi_disable_experimental_warnings = TRUE
)
}
warn_dire <- function(name) {
if (rlang::is_true(rlang::peek_option("connectapi_disable_dire_warnings"))) {
return(invisible(NULL))
}
warn_once(
msg = glue::glue("DO NOT USE IN PRODUCTION - The {name} function is for internal testing purposes only"),
id = paste0(name, "-dire")
)
}
scoped_dire_silence <- function(frame = rlang::caller_env()) {
rlang::local_options(
.frame = frame,
connectapi_disable_dire_warnings = TRUE
)
}
warn_once <- function(msg, id = msg, ...) {
if (rlang::is_true(rlang::peek_option("connectapi_disable_warnings"))) {
return(invisible(NULL))
}
rlang::warn(msg, .frequency = "once", .frequency_id = id, ...)
}
check_connect_license <- function(url) {
if (is_R6_class(url, "Connect")) {
url <- url$server
}
res <- httr::GET(glue::glue("{url}/__ping__"))
if (res$status_code == 402) {
stop(glue::glue("ERROR: The Connect server's license is expired ({url})"))
}
invisible(res)
}
simplify_version <- function(version) {
sub("([0-9]+\\.[0-9]+\\.[0-9]+).*", "\\1", version)
}
safe_server_settings <- function(client) {
srv <- tryCatch(
{
client$server_settings()
},
error = function(e) {
message(
glue::glue("Problem talking to Posit Connect at {client$server}/__api__/server_settings")
)
stop(e)
}
)
return(srv)
}
safe_server_version <- function(client) {
version <- safe_server_settings(client)$version
if (is.null(version) || nchar(version) == 0) {
message("Version information is not exposed by this Posit Connect instance.")
version <- NA
}
version
}
error_if_less_than <- function(using_version, tested_version) {
if (is.na(using_version)) {
msg <- glue::glue(
"WARNING: This feature requires Posit Connect version {tested_version} ",
"but the server version is not exposed by this Posit Connect instance. ",
"You may experience errors when using this functionality."
)
warn_once(msg)
return(invisible())
}
comp <- compare_connect_version(using_version, tested_version)
if (comp < 0) {
msg <- glue::glue(
"ERROR: This feature requires Posit Connect version {tested_version} ",
"but you are using {using_version}. Please upgrade Posit Connect."
)
stop(msg)
}
invisible()
}
compare_connect_version <- function(using_version, tested_version) {
as.integer(
compareVersion(simplify_version(using_version), simplify_version(tested_version))
)
}
warn_untested_connect <- function(using_version, minimum_tested_version = "1.8.8.2") {
comp <- compare_connect_version(using_version, minimum_tested_version)
if (!is.na(using_version) && (comp < 0)) {
warn_once(glue::glue(
"You are using an older version of Posit Connect ",
"({using_version}) than is tested ({minimum_tested_version}). ",
"Some APIs may not function as expected."
), id = "old-connect")
}
invisible()
}
token_hex <- function(n) {
raw <- as.raw(sample(0:255, n, replace = TRUE))
paste(as.character(raw), collapse = "")
}
# Checks to see if an http status contains a 404 error, and that that 404
# response does not contain an error code indicating that an endpoint was called
# but encountered a 404 for some other reason.
endpoint_does_not_exist <- function(res) {
httr::status_code(res) == "404" &&
!("code" %in% names(httr::content(res, as = "parsed")))
}
get_product <- function() {
posit_product <- Sys.getenv("POSIT_PRODUCT")
if (posit_product != "") {
return(posit_product)
}
Sys.getenv("RSTUDIO_PRODUCT")
}
# Returns `TRUE` if we're running locally (no product env var set),
# else `FALSE`.
is_local <- function() {
get_product() == ""
}
# Returns `TRUE` if we're running on Connect as determined by the
# `POSIT_PRODUCT` or `RSTUDIO_PRODUCT` env var, else `FALSE`.
on_connect <- function() {
get_product() == "CONNECT"
}
# Returns `TRUE` if we're running on Workbench as determined by the
# `POSIT_PRODUCT` or `RSTUDIO_PRODUCT` env var, else `FALSE`.
on_workbench <- function() {
get_product() == "WORKBENCH"
}