Skip to content

Commit 7569376

Browse files
committed
use make_request more
1 parent 80344c5 commit 7569376

File tree

5 files changed

+97
-84
lines changed

5 files changed

+97
-84
lines changed

R/auth.R

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ process_created_token <- function(
9090
#' @return an rtoot client object
9191
#' @references https://docs.joinmastodon.org/client/token/#creating-our-application
9292
get_client <- function(instance = "mastodon.social") {
93-
url <- prepare_url(instance)
94-
auth1 <- httr2::request(url_build(url)) |>
95-
httr2::req_url_path("/api/v1/apps") |>
93+
auth1 <- make_request(instance, "/api/v1/apps") |>
9694
httr2::req_body_form(
9795
client_name = "rtoot package",
9896
redirect_uris = "urn:ietf:wg:oauth:2.0:oob",
@@ -121,8 +119,7 @@ create_token <- function(client, type = "public", browser = TRUE) {
121119
}
122120
url <- prepare_url(client$instance)
123121
if (type == "public") {
124-
auth2 <- httr2::request(url_build(url)) |>
125-
httr2::req_url_path("oauth/token") |>
122+
auth2 <- make_request(client$instance, "oauth/token") |>
126123
httr2::req_body_form(
127124
client_id = client$client_id,
128125
client_secret = client$client_secret,
@@ -157,15 +154,14 @@ create_token <- function(client, type = "public", browser = TRUE) {
157154
check_rstudio = TRUE,
158155
default = ""
159156
)
160-
auth2 <- httr2::request(url_build(url)) |>
161-
httr2::req_url_path("oauth/token") |>
157+
auth2 <- make_request(client$instance, "oauth/token") |>
162158
httr2::req_body_form(
163159
client_id = client$client_id,
164160
client_secret = client$client_secret,
165161
redirect_uri = "urn:ietf:wg:oauth:2.0:oob",
166162
grant_type = "authorization_code",
167163
code = auth_code,
168-
scope = "read write follow"
164+
scopes = "read write follow"
169165
) |>
170166
httr2::req_perform()
171167
}
@@ -195,16 +191,18 @@ verify_credentials <- function(token, verbose = TRUE) {
195191
type <- token$type
196192
url <- prepare_url(token$instance)
197193
if (type == "user") {
198-
acc <- httr2::request(url_build(url)) |>
199-
httr2::req_url_path("api/v1/accounts/verify_credentials") |>
200-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
201-
httr2::req_error(is_error = function(resp) FALSE) |>
194+
acc <- make_request(
195+
token$instance,
196+
"api/v1/accounts/verify_credentials",
197+
token$bearer
198+
) |>
202199
httr2::req_perform()
203200
} else if (type == "public") {
204-
acc <- httr2::request(url_build(url)) |>
205-
httr2::req_url_path("api/v1/apps/verify_credentials") |>
206-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
207-
httr2::req_error(is_error = function(resp) FALSE) |>
201+
acc <- make_request(
202+
token$instance,
203+
"api/v1/apps/verify_credentials",
204+
token$bearer
205+
) |>
208206
httr2::req_perform()
209207
} else {
210208
cli::cli_abort("unknown token type")

R/instances.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ get_fedi_instances <- function(token = NA, n = 20, ...) {
2121
request_results <- httr2::request(
2222
"https://instances.social/api/1.0/instances/list"
2323
) |>
24-
httr2::req_headers(Authorization = paste("Bearer", token)) |>
24+
httr2::req_headers(Authorization = format_bearer(token)) |>
2525
httr2::req_url_query(!!!params) |>
2626
httr2::req_error(is_error = function(resp) FALSE) |>
2727
httr2::req_perform()
28-
status_code <- httr2::resp_status(request_results)
29-
if (!status_code %in% c(200)) {
30-
cli::cli_abort(paste("something went wrong. Status code:", status_code))
31-
}
28+
check_status_code(
29+
request_results,
30+
200,
31+
"Failed to fetch instances. Status code: {httr2::resp_status(request_results)}"
32+
)
3233
tbl <- dplyr::bind_rows(httr2::resp_body_json(request_results)$instances)
3334
tbl[["info"]] <- NULL
3435
tbl <- dplyr::distinct(tbl)

R/lists.R

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ post_list_create <- function(
4141
path <- "/api/v1/lists/"
4242
params <- list(title = title, replies_policy = replies_policy)
4343

44-
url <- prepare_url(token$instance)
45-
r <- httr2::request(url_build(url)) |>
46-
httr2::req_url_path(path) |>
47-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
44+
r <- make_request(token$instance, path, token$bearer) |>
4845
httr2::req_body_form(!!!params) |>
49-
httr2::req_error(is_error = function(resp) FALSE) |>
5046
httr2::req_perform()
51-
if (httr2::resp_status(r) == 200L) {
52-
sayif(verbose, paste0("successfully created the list: ", title))
53-
}
47+
check_status_code(
48+
r,
49+
200,
50+
"Failed to create list. Status code: {httr2::resp_status(r)}"
51+
)
52+
sayif(verbose, paste0("successfully created the list: ", title))
5453
invisible(r)
5554
}
5655

@@ -106,15 +105,14 @@ post_list_accounts <- function(id, account_ids, token = NULL, verbose = TRUE) {
106105
names(ids_lst) <- rep("account_ids[]", length(ids_lst))
107106
params <- ids_lst
108107

109-
url <- prepare_url(token$instance)
110-
r <- httr2::request(url_build(url)) |>
111-
httr2::req_url_path(path) |>
112-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
108+
r <- make_request(token$instance, path, token$bearer) |>
113109
httr2::req_body_form(!!!params) |>
114-
httr2::req_error(is_error = function(resp) FALSE) |>
115110
httr2::req_perform()
116-
if (httr2::resp_status(r) == 200L) {
117-
sayif(verbose, paste0("successfully added accounts to list: ", id))
118-
}
111+
check_status_code(
112+
r,
113+
200,
114+
"Failed to add accounts to list. Status code: {httr2::resp_status(r)}"
115+
)
116+
sayif(verbose, paste0("successfully added accounts to list: ", id))
119117
invisible(r)
120118
}

R/post.R

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,43 +76,35 @@ post_toot <- function(
7676
if (!is.null(language)) {
7777
params[["language"]] <- language
7878
}
79-
url <- prepare_url(token$instance)
80-
r <- httr2::request(url_build(url)) |>
81-
httr2::req_url_path("api/v1/statuses") |>
82-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
79+
r <- make_request(token$instance, "api/v1/statuses", token$bearer) |>
8380
httr2::req_body_form(!!!params) |>
84-
httr2::req_error(is_error = function(resp) FALSE) |>
8581
httr2::req_perform()
86-
if (httr2::resp_status(r) == 200L) {
87-
sayif(verbose, "Your toot has been posted!")
88-
} else {
89-
cli::cli_abort(
90-
"Failed to post toot. Status code: {httr2::resp_status(r)}"
91-
)
92-
}
82+
check_status_code(
83+
r,
84+
200,
85+
"Failed to post toot. Status code: {httr2::resp_status(r)}"
86+
)
87+
sayif(verbose, "Your toot has been posted!")
9388
invisible(r)
9489
}
9590

9691
upload_media_to_mastodon <- function(media, alt_text, token) {
97-
url <- prepare_url(token$instance)
98-
r <- httr2::request(url_build(url)) |>
99-
httr2::req_url_path("api/v1/media") |>
100-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
92+
r <- make_request(token$instance, "api/v1/media", token$bearer) |>
10193
httr2::req_body_multipart(
10294
file = curl::form_file(media),
10395
description = alt_text
10496
) |>
105-
httr2::req_error(is_error = function(resp) FALSE) |>
10697
httr2::req_perform()
107-
if (httr2::resp_status(r) != 200) {
108-
cli::cli_abort(paste0(
98+
check_status_code(
99+
r,
100+
200,
101+
paste0(
109102
"Error while uploading: ",
110103
media,
111-
". Status Code:",
104+
". Status Code: ",
112105
httr2::resp_status(r)
113-
))
114-
}
115-
106+
)
107+
)
116108
httr2::resp_body_json(r)$id
117109
}
118110

@@ -183,16 +175,15 @@ post_user <- function(
183175
params <- list()
184176
}
185177

186-
url <- prepare_url(token$instance)
187-
r <- httr2::request(url_build(url)) |>
188-
httr2::req_url_path(path) |>
189-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
178+
r <- make_request(token$instance, path, token$bearer) |>
190179
httr2::req_body_form(!!!params) |>
191-
httr2::req_error(is_error = function(resp) FALSE) |>
192180
httr2::req_perform()
193-
if (httr2::resp_status(r) == 200L) {
194-
sayif(verbose, "successfully performed action on user")
195-
}
181+
check_status_code(
182+
r,
183+
200,
184+
"Failed to perform action on user. Status code: {httr2::resp_status(r)}"
185+
)
186+
sayif(verbose, "successfully performed action on user")
196187
invisible(r)
197188
}
198189

@@ -232,16 +223,15 @@ post_status <- function(
232223
path <- paste0("/api/v1/statuses/", id, "/", action)
233224
params <- list()
234225

235-
url <- prepare_url(token$instance)
236-
r <- httr2::request(url_build(url)) |>
237-
httr2::req_url_path(path) |>
238-
httr2::req_headers(Authorization = paste0("Bearer ", token$bearer)) |>
226+
r <- make_request(token$instance, path, token$bearer) |>
239227
httr2::req_body_form(!!!params) |>
240-
httr2::req_error(is_error = function(resp) FALSE) |>
241228
httr2::req_perform()
242-
if (httr2::resp_status(r) == 200L) {
243-
sayif(verbose, "successfully performed action on status")
244-
}
229+
check_status_code(
230+
r,
231+
200,
232+
"Failed to perform action on status. Status code: {httr2::resp_status(r)}"
233+
)
234+
sayif(verbose, "successfully performed action on status")
245235
invisible(r)
246236
}
247237

R/utils.R

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,8 @@ make_get_request <- function(
8787
if (count >= max_error) {
8888
cli::cli_abort("Too many errors.")
8989
}
90-
req <- httr2::request(url_build(url)) |>
91-
httr2::req_url_path(path) |>
92-
httr2::req_url_query(!!!params) |>
93-
httr2::req_error(is_error = function(resp) FALSE)
94-
if (!is.null(bearer)) {
95-
req <- req |>
96-
httr2::req_headers(Authorization = paste("Bearer", bearer))
97-
}
90+
req <- make_request(url$hostname, path, bearer) |>
91+
httr2::req_url_query(!!!params)
9892
request_results <- httr2::req_perform(req)
9993

10094
status_code <- httr2::resp_status(request_results)
@@ -146,6 +140,38 @@ url_build <- function(url) {
146140
base
147141
}
148142

143+
# helper to format bearer token header
144+
format_bearer <- function(token) {
145+
paste0("Bearer ", token)
146+
}
147+
148+
# helper to create base authenticated request
149+
make_request <- function(instance, path, bearer = NULL) {
150+
url <- prepare_url(instance)
151+
req <- httr2::request(url_build(url)) |>
152+
httr2::req_url_path(path) |>
153+
httr2::req_error(is_error = function(resp) FALSE)
154+
155+
if (!is.null(bearer)) {
156+
req <- req |>
157+
httr2::req_headers(Authorization = format_bearer(bearer))
158+
}
159+
160+
req
161+
}
162+
163+
# helper to check response status and abort on error
164+
check_status_code <- function(resp, expected = 200, error_msg = NULL) {
165+
status <- httr2::resp_status(resp)
166+
if (status != expected) {
167+
if (is.null(error_msg)) {
168+
error_msg <- paste("Request failed with status code:", status)
169+
}
170+
cli::cli_abort(error_msg, call. = FALSE)
171+
}
172+
invisible(status)
173+
}
174+
149175
# process the header of a get request
150176
parse_header <- function(header) {
151177
df <- tibble::tibble(

0 commit comments

Comments
 (0)