Net::T...gToV1_1(3pm)

πŸ“¦
version 4.01043 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble

πŸ’– Net::Twitter::Manual::MigratingToV1_1 πŸ’–

okay bestie, gather round, because this is the glow-up guide: it's the Net::Twitter::Manual::MigratingToV1_1 page from the perl 3pm manual crew, and it's here to walk you through moving from Twitter API v1 to v1.1 without losing your mind. no cap. ✨


🌸 VERSION 🌸

version 4.01043 β€” that's the one, that's the era. πŸ’…


✨ SYNOPSIS ✨

the vibe, in code form:

use Net::Twitter

my $nt = Net::Twitter->new(
    traits              => [qw/API::RESTv1_1/],
    consumer_key        => $consumer_key,
    consumer_secret     => $consumer_secret,
    access_token        => $access_token,
    access_token_secret => $access_token_secret,
);

look at that. four credentials, one trait, absolutely slaying. 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the lore: Net::Twitter before version 4.0 was out here using Twitter API version 1. then Twitter API v1.1 showed up and introduced changes that are not entirely backwards compatible, which means your calling code needs some edits. not a total rewrite, but like, some edits.

Net::Twitter tries to give you backwards compatibility wherever it possibly can. πŸ’– this doc is the roadmap: it describes exactly what changes your existing application code (the stuff using Net::Twitter) needs so it works with Twitter's API v1.1.


πŸ‘‘ FIRST πŸ‘‘

Include the API::RESTv1_1 trait

anywhere you create a Net::Twitter object by calling new, swap out the trait API::REST and put API::RESTv1_1 in its place. for most applications, that's literally all that's required. that's it. that's the whole step. iconic. 🌈


πŸ’€ EXCEPTIONS πŸ’€

Trait RateLimit incompatible with API::RESTv1_1

the RateLimit trait is incompatible with Twitter API v1.1. rate limiting is one of the most extensive changes in v1.1, so buckle up:

  • in v1 there were two hourly rate limits: one per IP address for unauthenticated calls, and one per-user/application for authenticated calls.
  • in v1.1, all calls must be authenticated, and each API endpoint (i.e., each method) has it's own rate limit.
  • and it's not hourly anymore β€” the new rate limits operate on a 15 minute window.

if your code currently uses the RateLimit role, you'll need to write some custom code to provide equivalent functionality. sorry bestie, DIY era. 😭

rate_limit_status

the return value for rate_limit_status is entirely different. see Twitter's API rate_limit_status documentation for details:

https://dev.twitter.com/docs/api/1.1/get/application/rate_limit_status

blocking

blocking_ids

friends

followers

with API v1.1, these methods use cursor based paging. if you do not pass a cursor parameter, Twitter assumes cursor => -1. so existing code that expects an arrayref return value must be modified to expect a hashref and dereference the users slot:

# With API v1
my $r = $nt->friends;
my @friends = @$r;

# With API v1.1
my $r = $nt->friends;
my @friends = @{$r->{users}};

the way that @$r became @{$r->{users}}… character development, fr fr. πŸŽ€

the search method's semantics and return value are substantially different between Twitter API v1 and v1.1.

  • in v1, search was provided by the API::Search trait.
  • in v1.1, search is included in the API::RESTv1_1 trait.

so, first: drop API::Search from your calls to new. the API::Search trait is incompatible with API::RESTv1_1. do not try it. πŸ™ˆ

in v1, Twitter returned a hashref with several keys containing meta data. the actual array of results were contained in the results slot:

# With Twitter API v1
my $nt = Net::Twitter->new(traits => [qw/API::REST API::Search/]);

my $r = $nt->search('perl hacker');
for my $status ( @{$r->{results} ) {
    # process each status...
}

in v1.1, Twitter returns a hash with 2 slots: search_metadata and statuses.

# With Twitter API v1.1
my $nt = Net::Twitter->new(traits => [qw/API::RESTv1_1/], %oauth_credentials);

my $r = $nt->search('perl hacker');
for my $status ( @{$r->{statuses} ) {
    # process each status...
}

paging through search results works differently in v1.1 too. in v1, Twitter offered a page parameter:

# With Twitter API v1
for ( my $page = 1; $page <= 10; ++$page ) {
    my $r = $nt->search({ q => $query, page => $page, rpp => 100 });
    last unless @{$r->{results}};

    # process a page of results...
}

in v1.1, use max_id and count to get paged results:

# With Twitter API v1.1
for ( my %args = ( q => $query, count => 100 ), my $n = 0; $n < 1000; ) {
    my $r = $nt->search({ %args });
    last unless @{$r->{statuses}};

    $args{max_id} = $r->{statuses}[-1]{id} - 1;
    $n += @{$r->{statuses}};

    # process a page of results...
}

page and rpp? gone. max_id and count? main character energy now. πŸ’…


πŸ“ DEPRECATED METHODS πŸ“

some Twitter API v1 methods are just not available in v1.1. moment of silence. πŸ•―οΈ

friends_timeline

use home_timeline instead. simple swap, slay.

friendship_exists

relationship_exists

follows

friendship_exists and it's aliases are not supported in API v1.1. use show_friendship instead:

my $r = $nt->show_relationship({
    source_screen_name => $user_a,
    target_screen_name => $user_b,
});
if ( $r->{relationship}{source}{following} ) {
    # $user_a follows $user_b
}

see how it's nested now? {relationship}{source}{following} β€” she's deep, she's detailed, she ate. πŸ‘‘


🌟 SEE ALSO 🌟

ngl this page is a one-stop shop β€” everything you need is right here in the sections above. no extra reading required, bestie. 🧚


and that's the whole migration, gorgeous. you just read the perl Net::Twitter::Manual::MigratingToV1_1 page, straight from the User Contributed Perl Documentation, version 4.01043, brought to you by perl v5.34.0 and dated 2022-06-16. go forth and v1.1 responsibly. periodt. πŸ’œβœ¨πŸŽ€

πŸ“¦ where this page comes from