Skip to content

Commit

Permalink
Merge branch 'master' of github.com:fayland/perl-net-github
Browse files Browse the repository at this point in the history
  • Loading branch information
fayland committed Jun 7, 2015
2 parents 4398139 + 3ae76d8 commit 364a92f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Net/GitHub/V3.pm
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Or:
=head1 DESCRIPTION
L<http://develop.github.com/>
L<http://developer.github.com/v3/>
=head2 ATTRIBUTES
Expand Down
33 changes: 29 additions & 4 deletions lib/Net/GitHub/V3/Query.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use LWP::UserAgent;
use HTTP::Request;
use Carp qw/croak/;
use URI::Escape;
use Types::Standard qw(Str Bool InstanceOf Object);
use Types::Standard qw(Int Str Bool InstanceOf Object);

use Moo::Role;

Expand Down Expand Up @@ -40,6 +40,11 @@ has 'per_page' => ( is => 'rw', isa => Str, default => 100 );
# Error handle
has 'RaiseError' => ( is => 'rw', isa => Bool, default => 1 );

# Rate limits
has 'rate_limit' => ( is => 'rw', isa => Int, default => 0 );
has 'rate_limit_remaining' => ( is => 'rw', isa => Int, default => 0 );
has 'rate_limit_reset' => ( is => 'rw', isa => Str, default => 0 );

# optional
has 'u' => (is => 'rw', isa => Str);
has 'repo' => (is => 'rw', isa => Str);
Expand Down Expand Up @@ -151,12 +156,17 @@ sub query {

my $res = $ua->request($req);

# get the rate limit information from the http response headers
$self->rate_limit( $res->header('x-ratelimit-limit') );
$self->rate_limit_remaining( $res->header('x-ratelimit-remaining') );
$self->rate_limit_reset( $res->header('x-ratelimit-reset') );

# Slow down if we're approaching the rate limit
# By the way GitHub mistakes days for minutes in their documentation --
# the rate limit is per minute, not per day.
if ( $self->api_throttle ) {
sleep 2 if (($res->header('x-ratelimit-remaining') || 0)
< ($res->header('x-ratelimit-limit') || 60) / 2);
sleep 2 if (($self->rate_limit_remaining || 0)
< ($self->rate_limit || 60) / 2);
}

print STDERR "<<< " . $res->decoded_content . "\n" if $ENV{NG_DEBUG} and $ENV{NG_DEBUG} > 1;
Expand Down Expand Up @@ -315,7 +325,7 @@ set Authentication and call API
=item access_token
either set access_token from OAuth or login:pass for Basic Authentication
Either set access_token from OAuth or login:pass for Basic Authentication
L<http://developer.github.com/>
Expand All @@ -325,6 +335,21 @@ L<http://developer.github.com/>
=item api_throttle
API throttling is enabled by default, set api_throttle to 0 to disable it.
=item rate_limit
The maximum number of queries allowed per hour. 60 for anonymous users and
5,000 for authenticated users.
=item rate_limit_remaining
The number of requests remaining in the current rate limit window.
=item rate_limit_reset
The time the current rate limit resets in UTC epoch seconds.
=item RaiseError
=back
Expand Down

0 comments on commit 364a92f

Please sign in to comment.