Skip to content

Commit d8f20db

Browse files
authored
Improve printing for OAuth clients and tokens (#659)
Fixes #648
1 parent a6aac05 commit d8f20db

File tree

10 files changed

+53
-29
lines changed

10 files changed

+53
-29
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# httr2 (development version)
22

3+
* `oauth_client()` and `oauth_token()` gain refreshed print methods that use bulleted lists, like other httr2 objects. Additionally, print a `oauth_client()` with a custom `auth` function no longer errors (#648).
34
* `req_headers()` always redacts `Authorization` (#649).
45
* `req_headers_redacted()` supports dynamic dots (#647)
56
* `resp_stream_sse()` now automatically retrieves the next event if the current event contains no data. The data is now returned as a single string (#650).

R/headers.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,17 @@ list_redact <- function(x, names, case_sensitive = TRUE) {
5757
} else {
5858
i <- match(tolower(names), tolower(names(x)))
5959
}
60-
x[i] <- cli::col_grey("<REDACTED>")
60+
x[i] <- redacted()
6161
x
6262
}
6363

64+
redacted <- function() {
65+
cli::col_grey("<REDACTED>")
66+
}
67+
is_redacted <- function(x) {
68+
is.character(x) && length(x) == 1 && x == redacted()
69+
}
70+
6471

6572
#' @export
6673
`[.httr2_headers` <- function(x, i, ...) {

R/oauth-client.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ oauth_client <- function(
9393
print.httr2_oauth_client <- function(x, ...) {
9494
cli::cli_text(cli::style_bold("<", paste(class(x), collapse = "/"), ">"))
9595
redacted <- list_redact(compact(x), c("secret", "key"))
96-
cli::cli_dl(redacted)
96+
bullets(redacted)
9797
invisible(x)
9898
}
9999

R/oauth-token.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ print.httr2_token <- function(x, ...) {
6060
redacted$expires_at <- format(.POSIXct(x$expires_at))
6161
}
6262

63-
# https://github.com/r-lib/cli/issues/347
64-
is_empty <- map_lgl(redacted, ~ .x == "")
65-
redacted[is_empty] <- "''"
66-
67-
cli::cli_dl(compact(redacted))
63+
bullets(compact(redacted))
6864

6965
invisible(x)
7066
}

R/utils.R

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ bullets_with_header <- function(header, x) {
44
}
55

66
cli::cli_text("{.strong {header}}")
7+
bullets(x)
8+
}
79

10+
bullets <- function(x) {
811
as_simple <- function(x) {
912
if (is.atomic(x) && length(x) == 1) {
10-
if (is.character(x)) {
13+
if (is_redacted(x)) {
14+
x
15+
} else if (is.character(x)) {
1116
paste0('"', x, '"')
1217
} else {
1318
format(x)
1419
}
1520
} else {
16-
obj_type_friendly(x)
21+
paste0("<", class(x)[[1L]], ">")
1722
}
1823
}
1924
vals <- map_chr(x, as_simple)
25+
names <- format(names(x))
26+
names <- gsub(" ", "\u00a0", names, fixed = TRUE)
2027

2128
for (i in seq_along(x)) {
22-
cli::cli_li("{.field {names(x)[[i]]}}: {vals[[i]]}")
29+
cli::cli_li("{.field {names[[i]]}}: {vals[[i]]}")
2330
}
2431
}
2532

tests/testthat/_snaps/oauth-client.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,30 @@
4343
# client has useful print method
4444

4545
Code
46-
oauth_client("x", token_url = "http://example.com")
46+
oauth_client("x", url)
4747
Message
4848
<httr2_oauth_client>
49-
name: bf27508f7925b06bf28a10f3805351ab
50-
id: x
51-
token_url: http://example.com
52-
auth: oauth_client_req_auth_body
49+
* name : "bf27508f7925b06bf28a10f3805351ab"
50+
* id : "x"
51+
* token_url: "http://example.com"
52+
* auth : "oauth_client_req_auth_body"
5353
Code
54-
oauth_client("x", secret = "SECRET", token_url = "http://example.com")
54+
oauth_client("x", url, secret = "SECRET")
5555
Message
5656
<httr2_oauth_client>
57-
name: bf27508f7925b06bf28a10f3805351ab
58-
id: x
59-
secret: <REDACTED>
60-
token_url: http://example.com
61-
auth: oauth_client_req_auth_body
57+
* name : "bf27508f7925b06bf28a10f3805351ab"
58+
* id : "x"
59+
* secret : <REDACTED>
60+
* token_url: "http://example.com"
61+
* auth : "oauth_client_req_auth_body"
62+
Code
63+
oauth_client("x", url, auth = function(...) {
64+
xxx
65+
})
66+
Message
67+
<httr2_oauth_client>
68+
* name : "bf27508f7925b06bf28a10f3805351ab"
69+
* id : "x"
70+
* token_url: "http://example.com"
71+
* auth : <function>
6272

tests/testthat/_snaps/oauth-token.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
oauth_token(access_token = "secret", refresh_token = "secret", id_token = "secret")
55
Message
66
<httr2_token>
7-
token_type: bearer
8-
access_token: <REDACTED>
9-
refresh_token: <REDACTED>
10-
id_token: <REDACTED>
7+
* token_type : "bearer"
8+
* access_token : <REDACTED>
9+
* refresh_token: <REDACTED>
10+
* id_token : <REDACTED>
1111

tests/testthat/_snaps/req-headers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<httr2_request>
77
GET http://example.com
88
Headers:
9-
* a: "<REDACTED>"
9+
* a: <REDACTED>
1010
Body: empty
1111

1212
# checks input types

tests/testthat/_snaps/req.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<httr2_request>
2929
GET http://test
3030
Headers:
31-
* x: "{z}"
31+
* x : "{z}"
3232
* {z}: "x"
3333
Body: empty
3434

tests/testthat/test-oauth-client.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ test_that("checks auth types have needed args", {
1717
})
1818

1919
test_that("client has useful print method", {
20+
url <-"http://example.com"
21+
2022
expect_snapshot({
21-
oauth_client("x", token_url = "http://example.com")
22-
oauth_client("x", secret = "SECRET", token_url = "http://example.com")
23+
oauth_client("x", url)
24+
oauth_client("x", url, secret = "SECRET")
25+
oauth_client("x", url, auth = function(...) {xxx})
2326
})
2427
})
2528

0 commit comments

Comments
 (0)