Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
Add all the development
Browse files Browse the repository at this point in the history
Moves all the updates from devel to master. Due to the break in the API and the low usage I am now more confident to develop directly in master.
  • Loading branch information
llrs authored Dec 17, 2023
2 parents 8e89e7a + ba947e9 commit d7f5a41
Show file tree
Hide file tree
Showing 165 changed files with 414 additions and 2,828 deletions.
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", , "[email protected]", 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())
}
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())
}
}

Expand Down Expand Up @@ -83,7 +84,7 @@ resp <- function(x, ...) {
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())
}

if (has_name_(out, "meta")) {
Expand All @@ -103,7 +104,7 @@ resp <- function(x, ...) {
}

if (nrow(rest) > 1) {
abort("Please check", call = call)
abort("Please check", call = current_call())
}
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 @@ auth_is_bearer <- function(token = NULL) {
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
}

prepare_bearer <- function(x, y) {
Expand Down Expand Up @@ -37,7 +39,7 @@ auth_is_pkce <- function(token = NULL) {
# 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 @@ check_token_v2 <- function(token = NULL, mechanism = "bearer", call = caller_env
} 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"))
}

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())
}

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())
}
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 @@ check_token_v2 <- function(token = NULL, mechanism = "bearer", call = caller_env
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())
}
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)
} 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 @@ req_is_error <- function(resp) {
} else {
r <- resp
}
has_name_(r, "errors")
has_name_(r, "errors") || httr2::resp_is_error(resp)
}

req_errors <- function(resp) {
Expand All @@ -131,7 +126,7 @@ req_errors <- function(resp) {
# 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 @@ twitter_after <- function(resp) {
}


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)
httr2::req_throttle(req, throttle, realm = path)
}

Expand Down
Loading

0 comments on commit d7f5a41

Please sign in to comment.