Skip to content

Commit f3f0ee1

Browse files
committed
even more clean up - fixed all tests and added new endpoints
1 parent d670dd8 commit f3f0ee1

80 files changed

Lines changed: 419 additions & 344 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CONTRIBUTE.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
## How ?
22

33
- Fork the repository
4-
- Create and switch to a new branch `git checkout -b [name_of_your_new_branch]`
4+
- Create and switch to a new branch `git switch -c [name_of_your_new_branch]`
55
- Do the changes (i.e. edit files)
66
- Push remote branch to your github `git remote add -t [name_of_your_remote]` & `git push -u [name_of_your_remote] [name_of_your_new_branch]`
7-
- Create a new pull request (to my `master` branch)
7+
- Create a new pull request to my `master` branch
88

99
Want a direct push access? No problem, just let me know and become a :family:.
1010

1111
**Also**:
12-
- Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.
12+
13+
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). By participating in this project you agree to abide by its terms.

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Title: R Wrapper for the 'Bit.ly' and 'Is.gd'/'v.gd' URL Shortening
55
Description: Allows using two URL shortening services, which also provide
66
expanding and analytic functions. Specifically developed for 'Bit.ly' (which requires OAuth 2.0) and 'is.gd' (no API key).
77
Version: 2.0.0
8-
Date: 2025-04-10
8+
Date: 2025-04-13
99
Authors@R:
1010
c(
1111
person("John", "Malc", email = "cincenko@outlook.com", role = c("aut", "cre"), comment = "@dmpe"),
@@ -19,7 +19,7 @@ Imports: httr2 (>= 1.1.2), jsonlite (>= 2.0.0), stringr (>= 1.5.1),
1919
clipr (>= 0.8.0), miniUI (>= 0.1.1.1), cli (>= 3.6.4)
2020
Suggests: roxygen2 (>= 7.3.2), knitr (>= 1.50), testthat (>= 3.2.3),
2121
rmarkdown (>= 2.29), httpuv (>= 1.6.15), stringi (>= 1.8.7),
22-
covr (>= 3.6.4), lintr (>= 3.2.0)
22+
covr (>= 3.6.4), lintr (>= 3.2.0), dotenv (>= 1.0.3)
2323
VignetteBuilder: knitr
2424
License: Apache License 2.0
2525
URL: https://github.com/dmpe/urlshorteneR

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export(bitly_retrieve_group_click_metrics_by_cities)
2727
export(bitly_retrieve_group_click_metrics_by_countries)
2828
export(bitly_retrieve_group_click_metrics_by_devices)
2929
export(bitly_retrieve_group_click_metrics_by_ref_networks)
30+
export(bitly_retrieve_group_feature_usage)
3031
export(bitly_retrieve_group_pref)
3132
export(bitly_retrieve_group_shorten_counts)
3233
export(bitly_retrieve_groups)
@@ -51,8 +52,6 @@ export(bitly_update_group_pref)
5152
export(bitly_update_user)
5253
export(bitly_user_info)
5354
export(bitly_user_metrics_referring_domains)
54-
export(clipExpanderAddin)
55-
export(clipShortenerAddin)
5655
export(is_bitly_user_premium_holder)
5756
export(isgd_LinksExpand)
5857
export(isgd_LinksShorten)

NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ see: https://github.com/dmpe/urlshorteneR
33

44
###
55
April 2025 - Major rewrite of the package, using `httr2` as REST backend.
6-
- Add Bearer token authentication
6+
- Remove oAUTH authentication and replaced it with Bearer token authentication.
7+
- Removed RStudio Addins functionality to shorten and expand bit.ly links.
8+
- Add few additional endpoints.
79

810
February 2025 - Upgrade package dependancies
911

R/ApiKey.R

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# Bitly_api_version <- "v4"
22
# Isgd_api_version <- "v2019"
3-
.urlshorteneREnv <- new.env(parent = emptyenv())
43

54
#' @noRd
65
#' @keywords internal
76
processResponse <- function(resp) {
87
if (resp_is_error(resp) == TRUE) {
9-
cat("you are not a premium account holder, or internet connection does not work properly")
8+
cat("Either you are not a premium account holder and thus cannot execute the request, or internet connection does not work properly.")
9+
cat("The requested URL has been this: ", resp$request$url, "\n")
10+
message(sprintf("Code: %s - %s", json_response$message, json_response$description))
1011
} else {
1112
text_response <- resp |> resp_body_string()
1213
json_response <- jsonlite::fromJSON(text_response)
13-
message(sprintf("Code: %s - %s", json_response$message, json_response$description))
14-
cat("The requested URL has been this: ", resp$request$url, "\n")
1514
}
1615
return(json_response)
1716
}
@@ -34,16 +33,6 @@ processResponse <- function(resp) {
3433
doNoAuthRequest <- function(verb = "", url = NULL, queryParameters = NULL, patch_body = NULL, showURL = NULL) {
3534
req <- httr2::request(url)
3635
switch(verb,
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-
},
4736
"GET" = {
4837
resp <- req |>
4938
req_url_query(!!!queryParameters) |>
@@ -61,9 +50,7 @@ doNoAuthRequest <- function(verb = "", url = NULL, queryParameters = NULL, patch
6150
}
6251
)
6352

64-
json_response <- processResponse(resp)
65-
66-
return(json_response)
53+
return(processResponse(resp))
6754
}
6855

6956

@@ -85,33 +72,36 @@ doNoAuthRequest <- function(verb = "", url = NULL, queryParameters = NULL, patch
8572
doBearerTokenRequest <- function(verb = "", url = NULL, access_token = NULL, queryParameters = NULL, patch_body = NULL, showURL = NULL) {
8673
req <- httr2::request(url)
8774
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-
}
75+
switch(verb,
76+
"PATCH" = {
77+
resp <- req |>
78+
req_method("PATCH") |>
79+
req_body_json(patch_body) |>
80+
req_auth_bearer_token(access_token) |>
81+
req_headers(
82+
Accept = "application/json",
83+
"Content-Type" = "application/json"
84+
) |>
85+
req_perform()
86+
},
87+
"GET" = {
88+
resp <- req |>
89+
req_url_query(!!!queryParameters) |>
90+
req_method("GET") |>
91+
req_auth_bearer_token(access_token) |>
92+
req_perform()
93+
},
94+
"POST" = {
95+
resp <- req |>
96+
req_method("POST") |>
97+
req_body_json(queryParameters) |>
98+
req_auth_bearer_token(access_token) |>
99+
req_headers(
100+
Accept = "application/json",
101+
"Content-Type" = "application/json"
102+
) |>
103+
req_perform()
104+
}
105+
)
116106
return(processResponse(resp))
117107
}

R/addins.R

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ shortenerAddin <- function() {
1818
width = "100%"
1919
),
2020
shiny::uiOutput("bitly_domain"),
21+
shiny::uiOutput("bitly_token"),
2122
shiny::actionButton("shorten", label = "Shorten"),
2223
shiny::actionButton("expand", label = "Expand")
2324
)
@@ -43,6 +44,20 @@ shortenerAddin <- function() {
4344
}
4445
})
4546

47+
# bit.ly however also requires bearer tokens
48+
output$bitly_token <- shiny::renderUI({
49+
if (shortener_provider() == "bit.ly") {
50+
shiny::tagList(
51+
shiny::textInput("token",
52+
label = "Bearer Token", value = "",
53+
width = "100%"
54+
)
55+
)
56+
} else {
57+
return(NULL)
58+
}
59+
})
60+
4661
# Shorten links
4762
shiny::observeEvent(input$shorten, {
4863
shiny::req(input$url)
@@ -51,7 +66,7 @@ shortenerAddin <- function() {
5166

5267
# Conditionally pass custom bit.ly custom domain
5368
if (input$provider == "bit.ly") {
54-
res <- shortener(longUrl = input$url, domain = input$domain)
69+
res <- shortener(longUrl = input$url, domain = input$domain, accessToken = input$token)
5570
} else {
5671
res <- shortener(longUrl = input$url)
5772
}
@@ -88,7 +103,7 @@ shortenerAddin <- function() {
88103

89104
tryCatch(
90105
{
91-
expander(shorturl = short_url) %>%
106+
expander(shorturl = short_url, accessToken = input$token) %>%
92107
parse_response(expand = TRUE) %>%
93108
copy_and_notify()
94109
},
@@ -113,46 +128,6 @@ shortenerAddin <- function() {
113128
shiny::runGadget(ui, server, viewer = viewer)
114129
}
115130

116-
#' Shorten an URL from clipboard
117-
#'
118-
#' @export
119-
clipShortenerAddin <- function() {
120-
long_url <- clipr::read_clip()
121-
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-
)
133-
}
134-
135-
#' Expand an URL from clipboard
136-
#'
137-
#' @export
138-
clipExpanderAddin <- function() {
139-
bitly_id <- clipr::read_clip() %>%
140-
gsub(pattern = "https://", replacement = "")
141-
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-
)
153-
}
154-
155-
156131
#' Select shortener function
157132
#'
158133
#' @param provider_name (str) url shortener provider

R/bitly_bsd.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bitly_bsds <- function(access_token, showRequestURL = F) {
1717
if (length(df_bsds$bsds) == 0) {
1818
warning("There are no branded domains. First create some in Bitly.com")
1919
} else {
20-
df_bsds <- data.frame(df_bsds, stringsAsFactors = FALSE)
20+
df_bsds <- data.frame(df_bsds)
2121
return(df_bsds)
2222
}
2323
}

R/bitly_camp.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#' @param channel_guids - a list of strings
1111
#' @param description - description of campaign
1212
#' @param name - its name
13+
#' @param access_token - bearer token for authentication
1314
#'
1415
#' @inheritParams bitly_user_info
1516
#' @inheritParams bitly_retrieve_group
@@ -42,7 +43,7 @@ bitly_create_campaigns <- function(access_token, group_guid = NULL, channel_guid
4243
showURL = showRequestURL
4344
)
4445

45-
df_create_camps <- data.frame(df_create_camps, stringsAsFactors = FALSE)
46+
df_create_camps <- data.frame(df_create_camps)
4647
df_create_camps$created <- ymd_hms(df_create_camps$created, tz = "UTC")
4748
df_create_camps$modified <- ymd_hms(df_create_camps$modified, tz = "UTC")
4849

@@ -75,7 +76,7 @@ bitly_retrieve_campaigns <- function(access_token, group_guid = NULL, showReques
7576

7677
df_get_camps <- doBearerTokenRequest("GET", get_camp, access_token = access_token, queryParameters = query, showURL = showRequestURL)
7778

78-
df_get_camps <- data.frame(df_get_camps$campaigns, stringsAsFactors = FALSE)
79+
df_get_camps <- data.frame(df_get_camps$campaigns)
7980
df_get_camps$created <- ymd_hms(df_get_camps$created, tz = "UTC")
8081
df_get_camps$modified <- ymd_hms(df_get_camps$modified, tz = "UTC")
8182

@@ -125,7 +126,7 @@ bitly_create_channel <- function(access_token, group_guid = NULL, guid = NULL, n
125126
showURL = showRequestURL
126127
)
127128

128-
df_create_channel <- data.frame(df_create_channel, stringsAsFactors = FALSE)
129+
df_create_channel <- data.frame(df_create_channel)
129130
df_create_channel$created <- ymd_hms(df_create_channel$created, tz = "UTC")
130131
df_create_channel$modified <- ymd_hms(df_create_channel$modified, tz = "UTC")
131132

@@ -160,7 +161,7 @@ bitly_retrieve_channels <- function(access_token, group_guid = NULL, campaign_gu
160161

161162
df_get_channels <- doBearerTokenRequest("GET", get_channels, access_token = access_token, queryParameters = query, showURL = showRequestURL)
162163

163-
df_get_channels <- data.frame(df_get_channels$channels, stringsAsFactors = FALSE)
164+
df_get_channels <- data.frame(df_get_channels$channels)
164165
df_get_channels$created <- ymd_hms(df_get_channels$created, tz = "UTC")
165166
df_get_channels$modified <- ymd_hms(df_get_channels$modified, tz = "UTC")
166167

@@ -194,7 +195,7 @@ bitly_retrieve_campaign <- function(access_token, campaign_guid = NULL, showRequ
194195

195196
df_get_camp <- doBearerTokenRequest("GET", get_camp, access_token = access_token, queryParameters = query, showURL = showRequestURL)
196197

197-
df_get_camp <- data.frame(df_get_camp, stringsAsFactors = FALSE)
198+
df_get_camp <- data.frame(df_get_camp)
198199
df_get_camp$created <- ymd_hms(df_get_camp$created, tz = "UTC")
199200
df_get_camp$modified <- ymd_hms(df_get_camp$modified, tz = "UTC")
200201

@@ -229,7 +230,7 @@ bitly_retrieve_channel <- function(access_token, channel_guid = NULL, showReques
229230

230231
df_get_channel <- doBearerTokenRequest("GET", get_channel, access_token = access_token, queryParameters = query, showURL = showRequestURL)
231232

232-
df_get_channel <- data.frame(df_get_channel, stringsAsFactors = FALSE)
233+
df_get_channel <- data.frame(df_get_channel)
233234
df_get_channel$created <- ymd_hms(df_get_channel$created, tz = "UTC")
234235
df_get_channel$modified <- ymd_hms(df_get_channel$modified, tz = "UTC")
235236

@@ -277,7 +278,7 @@ bitly_update_campaign <- function(access_token, campaign_guid = NULL, group_guid
277278
showURL = showRequestURL
278279
)
279280

280-
df_update_camps <- data.frame(df_update_camps, stringsAsFactors = FALSE)
281+
df_update_camps <- data.frame(df_update_camps)
281282
df_update_camps$created <- ymd_hms(df_update_camps$created, tz = "UTC")
282283
df_update_camps$modified <- ymd_hms(df_update_camps$modified, tz = "UTC")
283284

@@ -322,7 +323,7 @@ bitly_update_channel <- function(access_token, channel_guid = NULL, group_guid =
322323
showURL = showRequestURL
323324
)
324325

325-
df_update_camp <- data.frame(df_update_camp, stringsAsFactors = FALSE)
326+
df_update_camp <- data.frame(df_update_camp)
326327
df_update_camp$created <- ymd_hms(df_update_camp$created, tz = "UTC")
327328
df_update_camp$modified <- ymd_hms(df_update_camp$modified, tz = "UTC")
328329

R/bitly_custom_links.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ bitly_retrieve_destination_metrics <- function(access_token, custom_bitlink = NU
6868
query <- list(access_token = access_token, custom_bitlink = custom_bitlink)
6969

7070
df_cust_metrics <- doBearerTokenRequest("GET", url = metrics_url, queryParameters = query, showURL = showRequestURL)
71-
df_cust_metrics <- data.frame(df_cust_metrics, stringsAsFactors = FALSE)
71+
df_cust_metrics <- data.frame(df_cust_metrics)
7272

7373
return(df_cust_metrics)
7474
}

0 commit comments

Comments
 (0)