Skip to content

Commit d670dd8

Browse files
committed
add a fix for the first part
1 parent 6e33207 commit d670dd8

35 files changed

Lines changed: 605 additions & 554 deletions

.Rbuildignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ bitly_local_token.rds
1414
.circleci
1515
^tests/\.httr-oauth$
1616
.editorconfig
17-
.github
17+
.github
18+
r.secret

.gitignore

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
.Rproj.user
2-
.Rhistory
3-
.RData
4-
.httr-oauth
5-
*.html
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
.httr-oauth
5+
*.html
6+
r.secret

R/ApiKey.R

Lines changed: 60 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ processResponse <- function(resp) {
99
cat("you are not a premium account holder, or internet connection does not work properly")
1010
} else {
1111
text_response <- resp |> resp_body_string()
12-
json_response <- fromJSON(text_response)
12+
json_response <- jsonlite::fromJSON(text_response)
1313
message(sprintf("Code: %s - %s", json_response$message, json_response$description))
1414
cat("The requested URL has been this: ", resp$request$url, "\n")
1515
}
16+
return(json_response)
1617
}
1718

1819

@@ -33,36 +34,35 @@ processResponse <- function(resp) {
3334
doNoAuthRequest <- function(verb = "", url = NULL, queryParameters = NULL, patch_body = NULL, showURL = NULL) {
3435
req <- httr2::request(url)
3536
switch(verb,
36-
"PATCH" = {
37-
resp <- req |>
38-
req_url_query(!!!queryParameters) |>
39-
req_method("PATCH") |>
40-
req_body_json(list(patch_body)) |>
41-
req_headers(
42-
Accept = "application/json"
43-
) |>
44-
req_perform()
45-
},
46-
"GET" = {
47-
resp <- req |>
48-
req_url_query(!!!queryParameters) |>
49-
req_method("GET") |>
50-
req_perform()
51-
},
52-
"POST" = {
53-
resp <- req |>
54-
req_url_query(!!!queryParameters) |>
55-
req_method("POST") |>
56-
req_body_json(list(patch_body)) |>
57-
req_headers(
58-
Accept = "application/json"
59-
) |>
60-
req_perform()
61-
}
37+
"PATCH" = {
38+
resp <- req |>
39+
req_url_query(!!!queryParameters) |>
40+
req_method("PATCH") |>
41+
req_body_json(list(patch_body)) |>
42+
req_headers(
43+
Accept = "application/json"
44+
) |>
45+
req_perform()
46+
},
47+
"GET" = {
48+
resp <- req |>
49+
req_url_query(!!!queryParameters) |>
50+
req_method("GET") |>
51+
req_perform()
52+
},
53+
"POST" = {
54+
resp <- req |>
55+
req_method("POST") |>
56+
req_body_json(list(patch_body)) |>
57+
req_headers(
58+
Accept = "application/json"
59+
) |>
60+
req_perform()
61+
}
6262
)
63-
63+
6464
json_response <- processResponse(resp)
65-
65+
6666
return(json_response)
6767
}
6868

@@ -82,42 +82,36 @@ doNoAuthRequest <- function(verb = "", url = NULL, queryParameters = NULL, patch
8282
#'
8383
#' @noRd
8484
#' @keywords internal
85-
doBearerTokenRequest <- function(verb = "", url = NULL, access_token=NULL, queryParameters = NULL, patch_body = NULL, showURL = NULL) {
85+
doBearerTokenRequest <- function(verb = "", url = NULL, access_token = NULL, queryParameters = NULL, patch_body = NULL, showURL = NULL) {
8686
req <- httr2::request(url)
87-
switch(verb,
88-
"PATCH" = {
89-
resp <- req |>
90-
req_url_query(!!!queryParameters) |>
91-
req_method("PATCH") |>
92-
req_body_json(list(patch_body)) |>
93-
req_auth_bearer_token(access_token) |>
94-
req_headers(
95-
Accept = "application/json"
96-
) |>
97-
req_perform()
98-
},
99-
"GET" = {
100-
resp <- req |>
101-
req_url_query(!!!queryParameters) |>
102-
req_method("GET") |>
103-
req_auth_bearer_token(access_token) |>
104-
req_perform()
105-
},
106-
"POST" = {
107-
resp <- req |>
108-
req_url_query(!!!queryParameters) |>
109-
req_method("POST") |>
110-
req_body_json(list(patch_body)) |>
111-
req_auth_bearer_token(access_token) |>
112-
req_headers(
113-
Accept = "application/json",
114-
"Content-Type" = "application/json"
115-
) |>
116-
req_perform()
117-
}
118-
)
119-
120-
json_response <- processResponse(resp)
121-
122-
return(json_response)
87+
resp <- NULL
88+
if (verb == "PATCH") {
89+
resp <- req |>
90+
req_url_query(!!!queryParameters) |>
91+
req_method("PATCH") |>
92+
req_body_json(list(patch_body)) |>
93+
req_auth_bearer_token(access_token) |>
94+
req_headers(
95+
Accept = "application/json"
96+
) |>
97+
req_perform()
98+
} else if (verb == "GET") {
99+
resp <- req |>
100+
req_url_query(!!!queryParameters) |>
101+
req_method("GET") |>
102+
req_auth_bearer_token(access_token) |>
103+
req_perform()
104+
} else if (verb == "POST") {
105+
resp <- req |>
106+
req_method("POST") |>
107+
req_body_json(queryParameters) |>
108+
req_auth_bearer_token(access_token) |>
109+
req_verbose() |>
110+
req_headers(
111+
Accept = "application/json",
112+
"Content-Type" = "application/json"
113+
) |>
114+
req_perform()
115+
}
116+
return(processResponse(resp))
123117
}

R/addins.R

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#'
66
#' @export
77
shortenerAddin <- function() {
8-
98
ui <- miniUI::miniPage(
109
miniUI::gadgetTitleBar("Shorten/Expand URL"),
1110
miniUI::miniContentPanel(
@@ -25,7 +24,6 @@ shortenerAddin <- function() {
2524
)
2625

2726
server <- function(input, output, session) {
28-
2927
shortener_provider <- shiny::reactive({
3028
shiny::req(input$provider)
3129
input$provider
@@ -35,8 +33,10 @@ shortenerAddin <- function() {
3533
output$bitly_domain <- shiny::renderUI({
3634
if (shortener_provider() == "bit.ly") {
3735
shiny::tagList(
38-
shiny::textInput("domain", label = "Bit.ly domain", value = "bit.ly",
39-
width = "100%")
36+
shiny::textInput("domain",
37+
label = "Bit.ly domain", value = "bit.ly",
38+
width = "100%"
39+
)
4040
)
4141
} else {
4242
return(NULL)
@@ -45,7 +45,6 @@ shortenerAddin <- function() {
4545

4646
# Shorten links
4747
shiny::observeEvent(input$shorten, {
48-
4948
shiny::req(input$url)
5049

5150
shortener <- get_shortener(provider_name = input$provider)
@@ -57,24 +56,25 @@ shortenerAddin <- function() {
5756
res <- shortener(longUrl = input$url)
5857
}
5958

60-
tryCatch({
61-
parse_response(res) %>%
62-
copy_and_notify()
63-
}, error = function(e) {
64-
cli::cli_alert_danger(e)
65-
shiny::showNotification(
66-
ui = paste("Something went wrong:", e, sep = " "),
67-
duration = 3L,
68-
closeButton = TRUE,
69-
type = "message"
70-
)
71-
})
72-
59+
tryCatch(
60+
{
61+
parse_response(res) %>%
62+
copy_and_notify()
63+
},
64+
error = function(e) {
65+
cli::cli_alert_danger(e)
66+
shiny::showNotification(
67+
ui = paste("Something went wrong:", e, sep = " "),
68+
duration = 3L,
69+
closeButton = TRUE,
70+
type = "message"
71+
)
72+
}
73+
)
7374
})
7475

7576
# Expand links
7677
shiny::observeEvent(input$expand, {
77-
7878
shiny::req(input$url)
7979

8080
expander <- get_expander(provider_name = input$provider)
@@ -86,26 +86,27 @@ shortenerAddin <- function() {
8686
short_url <- input$url
8787
}
8888

89-
tryCatch({
90-
expander(shorturl = short_url) %>%
91-
parse_response(expand = TRUE) %>%
92-
copy_and_notify()
93-
}, error = function(e) {
94-
cli::cli_alert_danger(e)
95-
shiny::showNotification(
96-
ui = paste("Something went wrong:", e, sep = " "),
97-
duration = 3L,
98-
closeButton = TRUE,
99-
type = "warning"
100-
)
101-
})
102-
89+
tryCatch(
90+
{
91+
expander(shorturl = short_url) %>%
92+
parse_response(expand = TRUE) %>%
93+
copy_and_notify()
94+
},
95+
error = function(e) {
96+
cli::cli_alert_danger(e)
97+
shiny::showNotification(
98+
ui = paste("Something went wrong:", e, sep = " "),
99+
duration = 3L,
100+
closeButton = TRUE,
101+
type = "warning"
102+
)
103+
}
104+
)
103105
})
104106

105107
shiny::observeEvent(input$done, {
106108
invisible(shiny::stopApp())
107109
})
108-
109110
}
110111

111112
viewer <- shiny::dialogViewer(dialogName = "URL shortener", width = 600, height = 400)
@@ -118,14 +119,17 @@ shortenerAddin <- function() {
118119
clipShortenerAddin <- function() {
119120
long_url <- clipr::read_clip()
120121

121-
tryCatch({
122-
short_url <- bitly_shorten_link(long_url = long_url) %>%
123-
parse_response()
124-
clipr::write_clip(short_url)
125-
cli::cli_alert_success(paste(short_url, "copied to clipboard", sep = " "))
126-
}, error = function(err) {
127-
cli::cli_alert_danger(err)
128-
})
122+
tryCatch(
123+
{
124+
short_url <- bitly_shorten_link(long_url = long_url) %>%
125+
parse_response()
126+
clipr::write_clip(short_url)
127+
cli::cli_alert_success(paste(short_url, "copied to clipboard", sep = " "))
128+
},
129+
error = function(err) {
130+
cli::cli_alert_danger(err)
131+
}
132+
)
129133
}
130134

131135
#' Expand an URL from clipboard
@@ -135,15 +139,17 @@ clipExpanderAddin <- function() {
135139
bitly_id <- clipr::read_clip() %>%
136140
gsub(pattern = "https://", replacement = "")
137141

138-
tryCatch({
139-
long_url <- bitly_expand_link(bitlink_id = bitly_id) %>%
140-
parse_response(expand = TRUE)
141-
clipr::write_clip(long_url)
142-
cli::cli_alert_success(paste(long_url, "copied to clipboard", sep = " "))
143-
}, error = function(e) {
144-
cli::cli_alert_danger(e)
145-
})
146-
142+
tryCatch(
143+
{
144+
long_url <- bitly_expand_link(bitlink_id = bitly_id) %>%
145+
parse_response(expand = TRUE)
146+
clipr::write_clip(long_url)
147+
cli::cli_alert_success(paste(long_url, "copied to clipboard", sep = " "))
148+
},
149+
error = function(e) {
150+
cli::cli_alert_danger(e)
151+
}
152+
)
147153
}
148154

149155

@@ -154,10 +160,10 @@ clipExpanderAddin <- function() {
154160
#'
155161
#' @noRd
156162
get_shortener <- function(provider_name) {
157-
switch (provider_name,
158-
"bit.ly" = bitly_shorten_link_,
159-
"is.gd" = isgd_LinksShorten,
160-
"v.gd" = vgd_LinksShorten
163+
switch(provider_name,
164+
"bit.ly" = bitly_shorten_link_,
165+
"is.gd" = isgd_LinksShorten,
166+
"v.gd" = vgd_LinksShorten
161167
)
162168
}
163169

@@ -166,10 +172,10 @@ get_shortener <- function(provider_name) {
166172
#' @inheritParams get_shortener
167173
#' @noRd
168174
get_expander <- function(provider_name) {
169-
switch (provider_name,
170-
"bit.ly" = bitly_expand_link_,
171-
"is.gd" = isgd_LinksExpand,
172-
"v.gd" = vgd_LinksExpand
175+
switch(provider_name,
176+
"bit.ly" = bitly_expand_link_,
177+
"is.gd" = isgd_LinksExpand,
178+
"v.gd" = vgd_LinksExpand
173179
)
174180
}
175181

0 commit comments

Comments
 (0)