@@ -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
9691upload_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
0 commit comments