Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devel #786

Merged
merged 22 commits into from
Dec 17, 2023
Merged

Devel #786

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: rtweet
Title: Collecting Twitter Data
Version: 1.2.1.9000
Version: 1.2.1.9001
Authors@R: c(
person("Michael W.", "Kearney", , "kearneymw@missouri.edu", role = "aut",
comment = c(ORCID = "0000-0002-0730-4694")),
Expand Down Expand Up @@ -29,13 +29,14 @@ Imports:
bit64 (>= 4.0.5),
curl (>= 4.3.2),
httr (>= 1.3.0),
httr2 (>= 0.2.2),
httr2 (>= 0.2.3.9000),
jsonlite (>= 0.9.22),
lifecycle (>= 1.0.0),
methods,
progress (>= 1.2.2),
rlang (>= 0.4.10),
tibble (>= 1.3.4),
tools,
utils,
withr (>= 2.5.0)
Suggests:
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export(get_tokens)
export(get_trends)
export(ids)
export(invalidate_bearer)
export(lat_lng)
export(links)
export(list_expansions)
export(list_fields)
Expand Down Expand Up @@ -108,7 +107,6 @@ export(post_destroy)
export(post_favorite)
export(post_follow)
export(post_friendship)
export(post_list)
export(post_message)
export(post_mute)
export(post_tweet)
Expand All @@ -120,6 +118,7 @@ export(rate_limit_wait)
export(read_twitter_csv)
export(round_time)
export(rtweet_app)
export(rtweet_bearer)
export(rtweet_bot)
export(rtweet_client)
export(rtweet_oauth2)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

* Fix problem with `auth_sitrep()` not correctly handling old tokens.

* Since httr2 > 0.2.3, rtweet refreshes OAuth 2.0 tokens automatically,
also if possible, replacing the file where they are saved.

# rtweet 1.2.1

* Fix `auth_sitrep()` to work well with OAuth2 tokens.
Expand Down
11 changes: 6 additions & 5 deletions R/api_v2_responses.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Handling responses ####
parsing <- function(x, expansions, fields, call = caller_env()) {
parsing <- function(x, expansions, fields) {
if (!is_logical(x)) {
abort("parse should be either TRUE or FALSE", call = call)
abort("parse should be either TRUE or FALSE", call = current_call())

Check warning on line 4 in R/api_v2_responses.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_responses.R#L4

Added line #L4 was not covered by tests
}
if (isTRUE(x) && (!is.null(expansions) || !is.null(fields))) {
abort(c("Not yet implemented!",
i = "Stay tuned for further updates or use `parse = FALSE`"))
i = "Stay tuned for further updates or use `parse = FALSE`"),
call = current_call())

Check warning on line 9 in R/api_v2_responses.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_responses.R#L8-L9

Added lines #L8 - L9 were not covered by tests
}
}

Expand Down Expand Up @@ -83,7 +84,7 @@
class(out) <- c("Twitter_resp", class(out))

if (has_name_(out, "errors")) {
abort(req_errors(out), call = NULL)
abort(req_errors(out), call = current_call())

Check warning on line 87 in R/api_v2_responses.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_responses.R#L87

Added line #L87 was not covered by tests
}

if (has_name_(out, "meta")) {
Expand All @@ -103,7 +104,7 @@
}

if (nrow(rest) > 1) {
abort("Please check", call = call)
abort("Please check", call = current_call())

Check warning on line 107 in R/api_v2_responses.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_responses.R#L107

Added line #L107 was not covered by tests
}
out$meta <- rest
}
Expand Down
35 changes: 15 additions & 20 deletions R/api_v2_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
if (is.null(token)) {
token <- auth_get()
}
inherits(token, "rtweet_bearer")
# if the bearer is created with rtweet_bearer the class is different
bearer_httr2 <- inherits(token, "httr2_token") && is.null(token$refresh_token)
inherits(token, "rtweet_bearer") || bearer_httr2

Check warning on line 9 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L8-L9

Added lines #L8 - L9 were not covered by tests
}

prepare_bearer <- function(x, y) {
Expand Down Expand Up @@ -37,7 +39,7 @@
# if (auth_has_default()) {
# tryCatch(check_token_v2())
# }
check_token_v2 <- function(token = NULL, mechanism = "bearer", call = caller_env()) {
check_token_v2 <- function(token = NULL, mechanism = "bearer") {

token <- token %||% auth_get()

Expand All @@ -49,29 +51,23 @@
} else if (length(mechanism) == 2) {
# To make it easier testing interactively
if (is_developing()) {
return(load_token("bearer_academic_dev", call = call))
return(load_token("bearer_academic_dev"))

Check warning on line 54 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L54

Added line #L54 was not covered by tests
}

abort(c(
"x" = "You must use a token accepted by the endpoints v2.",
"i" = "Check the `vignette('auth', package = 'rtweet')` about how to get them."),
call = call)
call = current_call())

Check warning on line 60 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L60

Added line #L60 was not covered by tests
}

if (mechanism == "bearer" && !auth_is_bearer(token)) {
# To make it easier testing interactively
if (is_developing()) {
return(load_token("bearer_academic_dev", call = call))
}

abort(c("x" = "A bearer `token` is needed for this endpoint.",
"i" = "Get one via `rtweet_app()`"),
call = call)
call = current_call())

Check warning on line 67 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L67

Added line #L67 was not covered by tests
}
if (mechanism == "pkce" && !auth_is_pkce(token)) {
# To make it easier testing interactively
if (is_developing()) {
return(load_token("renewed_token", call = call))
}

client <- client_get()
if (!is_client(client)) {
msg <- c(">" = "Check the vignette('auth', 'rtweet')",
Expand All @@ -82,20 +78,19 @@
abort(c("x" = "An OAuth 2.0 is needed for this endpoint.",
msg,
"i" = "Get the authorization via `rtweet_oauth2()`"),
call = call)
call = current_call())

Check warning on line 81 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L81

Added line #L81 was not covered by tests
}
token
}

# Provides the required method for the token type
req_auth <- function(req, token) {
if (auth_is_bearer(token)) {
token <- token$token
httr2::req_auth_bearer_token(req, token$token)

Check warning on line 89 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L89

Added line #L89 was not covered by tests
} else if (auth_is_pkce(token)) {
token <- auth_renew(token)
token <- token$access_token
}
httr2::req_auth_bearer_token(req, token)
}

req_is_error <- function(resp) {
Expand All @@ -104,7 +99,7 @@
} else {
r <- resp
}
has_name_(r, "errors")
has_name_(r, "errors") || httr2::resp_is_error(resp)

Check warning on line 102 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L102

Added line #L102 was not covered by tests
}

req_errors <- function(resp) {
Expand All @@ -131,7 +126,7 @@
# Prepare the requests ####
# General function to create the requests for Twitter API v2 with retry limits
# and error handling
req_v2 <- function(token = NULL, call = caller_env()) {
req_v2 <- function(token = NULL) {
req <- httr2::request("https://api.twitter.com/2")
req_agent <- httr2::req_user_agent(req, "rtweet (https://docs.ropensci.org/rtweet)")
req_authorized <- req_auth(req_agent, token)
Expand All @@ -155,9 +150,9 @@
}


endpoint_v2 <- function(token, path, throttle, call = caller_call()) {
endpoint_v2 <- function(token, path, throttle) {

req <- httr2::req_url_path_append(req_v2(token, call), path)
req <- httr2::req_url_path_append(req_v2(token), path)

Check warning on line 155 in R/api_v2_utils.R

View check run for this annotation

Codecov / codecov/patch

R/api_v2_utils.R#L155

Added line #L155 was not covered by tests
httr2::req_throttle(req, throttle, realm = path)
}

Expand Down
Loading
Loading