File: utils.R

package info (click to toggle)
r-cran-httr2 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,684 kB
  • sloc: sh: 13; makefile: 2
file content (344 lines) | stat: -rw-r--r-- 7,658 bytes parent folder | download
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
bullets_with_header <- function(header, x) {
  if (length(x) == 0) {
    return()
  }

  cli::cat_line(cli::format_inline("{.strong {header}}"))
  bullets(x)
}

bullets <- function(x) {
  as_simple <- function(x) {
    if (is.atomic(x) && length(x) == 1) {
      if (is.character(x)) {
        paste0('"', x, '"')
      } else {
        format(x)
      }
    } else {
      if (is_redacted_sentinel(x)) {
        format(x)
      } else {
        paste0("<", class(x)[[1L]], ">")
      }
    }
  }
  vals <- map_chr(x, as_simple)
  names <- format(names(x))
  names <- gsub(" ", "\u00a0", names, fixed = TRUE)

  for (i in seq_along(x)) {
    cli::cat_line(cli::format_inline("* {.field {names[[i]]}}: {vals[[i]]}"))
  }
}

modify_list <- function(
  .x,
  ...,
  .ignore_case = FALSE,
  error_call = caller_env()
) {
  dots <- list2(...)
  if (length(dots) == 0) {
    return(.x)
  }

  if (!is_named(dots)) {
    cli::cli_abort(
      "All components of {.arg ...} must be named.",
      call = error_call
    )
  }

  if (.ignore_case) {
    out <- .x[!tolower(names(.x)) %in% tolower(names(dots))]
  } else {
    out <- .x[!names(.x) %in% names(dots)]
  }

  out <- c(out, compact(dots))

  if (length(out) == 0) {
    names(out) <- NULL
  }

  out
}


sys_sleep <- function(seconds, task, fps = 10, progress = NULL) {
  check_number_decimal(seconds)
  check_string(task)
  check_number_decimal(fps)
  progress <- progress %||% getOption("httr2_progress", !is_testing())
  check_bool(progress, allow_null = TRUE)

  if (seconds == 0) {
    return(invisible())
  }

  if (!progress) {
    cli::cli_alert("Waiting {round(seconds, 2)}s {task}")
    Sys.sleep(seconds)
    return(invisible())
  }

  start <- cur_time()
  signal("", class = "httr2_sleep", seconds = seconds)

  cli::cli_progress_bar(
    format = "Waiting {ceiling(seconds)}s {task} {cli::pb_bar}",
    total = seconds * fps
  )

  while (
    {
      left <- start + seconds - cur_time()
      left > 0
    }
  ) {
    Sys.sleep(min(1 / fps, left))
    cli::cli_progress_update(set = (seconds - left) * fps)
  }
  cli::cli_progress_done()

  invisible()
}

# allow mocking
Sys.sleep <- NULL

cur_time <- function() proc.time()[[3]]

is_error <- function(x) inherits(x, "error")

unix_time <- function() as.integer(Sys.time())

is_testing <- function() {
  identical(Sys.getenv("TESTTHAT"), "true")
}

# https://datatracker.ietf.org/doc/html/rfc7636#appendix-A
base64_url_encode <- function(x) {
  x <- openssl::base64_encode(x)
  x <- gsub("=+$", "", x)
  x <- gsub("+", "-", x, fixed = TRUE)
  x <- gsub("/", "_", x, fixed = TRUE)
  x
}

base64_url_decode <- function(x) {
  mod4 <- nchar(x) %% 4
  if (mod4 > 0) {
    x <- paste0(x, strrep("=", 4 - mod4))
  }

  x <- gsub("_", "/", x, fixed = TRUE)
  x <- gsub("-", "+", x, fixed = TRUE)
  # x <- gsub("=+$", "", x)
  openssl::base64_decode(x)
}

base64_url_rand <- function(bytes = 32) {
  base64_url_encode(openssl::rand_bytes(bytes))
}

local_time <- function(x, tz = "UTC") {
  out <- as.POSIXct(x, tz = tz)
  attr(out, "tzone") <- NULL
  out
}

http_date <- function(x = Sys.time()) {
  withr::local_locale(LC_TIME = "C")
  strftime(x, "%a, %d %b %Y %H:%M:%S", tz = "UTC", usetz = TRUE)
}

parse_http_date <- function(x) {
  check_string(x)

  withr::local_locale(LC_TIME = "C")

  # https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.1
  out <- as.POSIXct(strptime(x, "%a, %d %b %Y %H:%M:%S", tz = "UTC"))
  attr(out, "tzone") <- NULL
  out
}

touch <- function(path, time = Sys.time()) {
  if (!file.exists(path)) {
    file.create(path)
  }
  Sys.setFileTime(path, time)
}

local_write_lines <- function(..., .env = caller_env()) {
  path <- withr::local_tempfile(.local_envir = .env)
  writeLines(c(...), path)
  path
}

check_function2 <- function(
  x,
  ...,
  args = NULL,
  allow_null = FALSE,
  arg = caller_arg(x),
  call = caller_env()
) {
  check_function(
    x = x,
    allow_null = allow_null,
    arg = arg,
    call = call
  )

  if (!is.null(x)) {
    .check_function_args(
      f = x,
      expected_args = args,
      arg = arg,
      call = call
    )
  }
}

# Basically copied from rlang. Can be removed when https://github.com/r-lib/rlang/pull/1652
# is merged
.check_function_args <- function(f, expected_args, arg, call) {
  if (is_null(expected_args)) {
    return(invisible(NULL))
  }

  actual_args <- fn_fmls_names(f) %||% character()
  missing_args <- setdiff(expected_args, actual_args)
  if (is_empty(missing_args)) {
    return(invisible(NULL))
  }

  n_expected_args <- length(expected_args)
  n_actual_args <- length(actual_args)

  if (n_actual_args == 0) {
    arg_info <- "instead it has no arguments"
  } else {
    arg_info <- paste0("it currently has {.arg {actual_args}}")
  }

  cli::cli_abort(
    paste0(
      "{.arg {arg}} must have the {cli::qty(n_expected_args)}argument{?s} {.arg {expected_args}}; ",
      arg_info,
      "."
    ),
    call = call,
    arg = arg
  )
}

# This is inspired by the C interface of `cli_progress_bar()` which has just
# 2 arguments: `total` and `config`
create_progress_bar <- function(
  progress,
  total,
  name = "iterating",
  format = NULL,
  envir = caller_env(),
  frame = caller_env()
) {
  if (is_false(progress)) {
    return(list(
      update = function(...) {},
      done = function() {}
    ))
  }

  if (is.null(progress) || isTRUE(progress)) {
    args <- list()
    args$name <- name
    args$format <- format
  } else if (is_scalar_character(progress)) {
    args <- list(name = progress)
  } else if (is.list(progress)) {
    args <- progress
    args$name <- args$name %||% name
    args$format <- args$format %||% format
  } else {
    stop_input_type(
      progress,
      what = c("a bool", "a string", "a list"),
      call = frame
    )
  }

  args$total <- total
  args$.envir <- envir
  args$.auto_close <- FALSE

  id <- exec(cli::cli_progress_bar, !!!args)
  withr::defer(cli::cli_progress_done(id = id), envir = frame)

  list(
    update = function(...) {
      cli::cli_progress_update(..., id = id, .envir = envir)
    },
    done = function() cli::cli_progress_done(id = id)
  )
}

imap <- function(.x, .f, ...) {
  map2(.x, names(.x), .f, ...)
}

# Slices the vector using the only sane semantics: start inclusive, end
# exclusive.
#
# * Allows start == end, which means return no elements.
# * Allows start == length(vector) + 1, which means return no elements.
# * Allows zero-length vectors.
#
# Otherwise, slice() is quite strict about what it allows start/end to be: no
# negatives, no reversed order.
slice <- function(vector, start = 1, end = length(vector) + 1) {
  stopifnot(start > 0)
  stopifnot(start <= length(vector) + 1)
  stopifnot(end > 0)
  stopifnot(end <= length(vector) + 1)
  stopifnot(end >= start)

  if (start == end) {
    vector[FALSE] # Return an empty vector of the same type
  } else {
    vector[start:(end - 1)]
  }
}

is_named_list <- function(x) {
  is_list(x) && (is_named(x) || length(x) == 0)
}

pretty_json <- function(x) {
  tryCatch(
    gsub("\n$", "", jsonlite::prettify(x, indent = 2)),
    error = function(e) x
  )
}

log_stream <- function(..., prefix = "<< ") {
  out <- gsub("\n", paste0("\n", prefix), paste0(prefix, ..., collapse = ""))
  cli::cat_line(out)
}

paste_c <- function(..., collapse = "") {
  paste0(c(...), collapse = collapse)
}

# Give user the get-out-of-jail-free card if interrupt-capturing function
# is wrapped inside a loop
check_repeated_interrupt <- function() {
  if (as.double(Sys.time()) - the$last_interrupt < 1) {
    cli::cli_alert_warning("Interrupting")
    interrupt()
  }
  the$last_interrupt <- as.double(Sys.time())
}