File Coverage

blib/lib/Twitter/API/Trait/RateLimiting.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Twitter::API::Trait::RateLimiting;
2             # ABSTRACT: Automatically sleep as needed to handle rate limiting
3             $Twitter::API::Trait::RateLimiting::VERSION = '1.0006';
4 1     1   548 use Moo::Role;
  1         2  
  1         6  
5 1     1   366 use HTTP::Status qw(HTTP_TOO_MANY_REQUESTS);
  1         2  
  1         62  
6 1     1   12 use namespace::clean;
  1         3  
  1         8  
7              
8             #pod =attr rate_limit_sleep_code
9             #pod
10             #pod A coderef, called to implement sleeping. It takes a single parameter -
11             #pod the number of seconds to sleep. The default implementation is:
12             #pod
13             #pod sub { sleep shift }
14             #pod
15             #pod =cut
16              
17             has rate_limit_sleep_code => (
18             is => 'rw',
19             default => sub {
20             sub { sleep shift };
21             },
22             );
23              
24             around send_request => sub {
25             my $orig = shift;
26             my $self = shift;
27              
28             my $res = $self->$orig(@_);
29              
30             while($res->code == HTTP_TOO_MANY_REQUESTS) {
31             my $sleep_time = $res->header('x-rate-limit-reset') - time;
32             $self->rate_limit_sleep_code->($sleep_time);
33              
34             $res = $self->$orig(@_);
35             }
36              
37             return $res;
38             };
39              
40             1;
41              
42             __END__
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             Twitter::API::Trait::RateLimiting - Automatically sleep as needed to handle rate limiting
51              
52             =head1 VERSION
53              
54             version 1.0006
55              
56             =head1 SYNOPSIS
57              
58             use Twitter::API;
59              
60             my $client = Twitter::API->new_with_options(
61             traits => [ qw/ApiMethods RateLimiting/ ],
62             %other_options,
63             );
64              
65             # Use $client as normal
66              
67             =head1 DESCRIPTION
68              
69             Twitter's API implements rate limiting in a 15-minute window, and
70             will serve up an HTTP 429 error if the rate limit is exceeded for
71             a window. Applying this trait will give L<Twitter::API> the ability
72             to automatically sleep as much as is needed and then retry a request
73             instead of simply throwing an exception.
74              
75             =head1 ATTRIBUTES
76              
77             =head2 rate_limit_sleep_code
78              
79             A coderef, called to implement sleeping. It takes a single parameter -
80             the number of seconds to sleep. The default implementation is:
81              
82             sub { sleep shift }
83              
84             =head1 SEE ALSO
85              
86             L<https://developer.twitter.com/en/docs/basics/rate-limiting>
87              
88             =head1 AUTHOR
89              
90             Marc Mims <marc@questright.com>
91              
92             =head1 COPYRIGHT AND LICENSE
93              
94             This software is copyright (c) 2015-2021 by Marc Mims.
95              
96             This is free software; you can redistribute it and/or modify it under
97             the same terms as the Perl 5 programming language system itself.
98              
99             =cut