| 1 | package Net::GitHub::V4; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | |||
| 5 | our $VERSION = '1.02'; | ||
| 6 | our $AUTHORITY = 'cpan:FAYLAND'; | ||
| 7 | |||
| 8 | use URI; | ||
| 9 | use JSON::MaybeXS; | ||
| 10 | use LWP::UserAgent; | ||
| 11 | use HTTP::Request; | ||
| 12 | use Carp qw/croak/; | ||
| 13 | use URI::Escape; | ||
| 14 | use Types::Standard qw(Int Str Bool InstanceOf Object); | ||
| 15 | use Cache::LRU; | ||
| 16 | |||
| 17 | # configurable args | ||
| 18 | |||
| 19 | # Authentication | ||
| 20 | has 'access_token' => ( is => 'rw', isa => Str, required => 1 ); | ||
| 21 | |||
| 22 | # return raw unparsed JSON | ||
| 23 | has 'raw_string' => (is => 'rw', isa => Bool, default => 0); | ||
| 24 | has 'raw_response' => (is => 'rw', isa => Bool, default => 0); | ||
| 25 | |||
| 26 | has 'api_url' => (is => 'ro', default => 'https://api.github.com/graphql'); | ||
| 27 | has 'api_throttle' => ( is => 'rw', isa => Bool, default => 1 ); | ||
| 28 | |||
| 29 | # Rate limits | ||
| 30 | has 'rate_limit' => ( is => 'rw', isa => Int, default => 0 ); | ||
| 31 | has 'rate_limit_remaining' => ( is => 'rw', isa => Int, default => 0 ); | ||
| 32 | has 'rate_limit_reset' => ( is => 'rw', isa => Str, default => 0 ); | ||
| 33 | |||
| 34 | has 'ua' => ( | ||
| 35 | isa => InstanceOf['LWP::UserAgent'], | ||
| 36 | is => 'ro', | ||
| 37 | lazy => 1, | ||
| 38 | default => sub { | ||
| 39 | LWP::UserAgent->new( | ||
| 40 | agent => "perl-net-github/$VERSION", | ||
| 41 | keep_alive => 4, | ||
| 42 | timeout => 60, | ||
| 43 | ); | ||
| 44 | }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | has 'json' => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => Object, # InstanceOf['JSON::MaybeXS'], | ||
| 50 | lazy => 1, | ||
| 51 | default => sub { | ||
| 52 | return JSON::MaybeXS->new( utf8 => 1 ); | ||
| 53 | } | ||
| 54 | ); | ||
| 55 | |||
| 56 | has 'cache' => ( | ||
| 57 | isa => InstanceOf['Cache::LRU'], | ||
| 58 | is => 'rw', | ||
| 59 | lazy => 1, | ||
| 60 | default => sub { | ||
| 61 | Cache::LRU->new( | ||
| 62 | size => 200 | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub query { | ||
| 68 | my ($self, $iql, $variables) = @_; | ||
| 69 | |||
| 70 | my $ua = $self->ua; | ||
| 71 | |||
| 72 | $ua->default_header('Authorization', "bearer " . $self->access_token); | ||
| 73 | |||
| 74 | my $data = { query => $iql }; | ||
| 75 | $data->{variables} = $self->json->encode($variables) if $variables; | ||
| 76 | my $json = $self->json->encode($data); | ||
| 77 | |||
| 78 | print STDERR ">>> POST {$self->api_url}\n" if $ENV{NG_DEBUG}; | ||
| 79 | print STDERR ">>> $json\n" if $ENV{NG_DEBUG} and $ENV{NG_DEBUG} > 1; | ||
| 80 | my $req = HTTP::Request->new( 'POST', $self->api_url ); | ||
| 81 | $req->accept_decodable; | ||
| 82 | $req->content($json); | ||
| 83 | $req->header( 'Content-Length' => length $req->content ); | ||
| 84 | |||
| 85 | my $res = $self->_make_request($req); | ||
| 86 | |||
| 87 | # get the rate limit information from the http response headers | ||
| 88 | $self->rate_limit( $res->header('x-ratelimit-limit') ); | ||
| 89 | $self->rate_limit_remaining( $res->header('x-ratelimit-remaining') ); | ||
| 90 | $self->rate_limit_reset( $res->header('x-ratelimit-reset') ); | ||
| 91 | |||
| 92 | # Slow down if we're approaching the rate limit | ||
| 93 | # By the way GitHub mistakes days for minutes in their documentation -- | ||
| 94 | # the rate limit is per minute, not per day. | ||
| 95 | if ( $self->api_throttle ) { | ||
| 96 | sleep 2 if (($self->rate_limit_remaining || 0) | ||
| 97 | < ($self->rate_limit || 60) / 2); | ||
| 98 | } | ||
| 99 | |||
| 100 | print STDERR "<<< " . $res->decoded_content . "\n" if $ENV{NG_DEBUG} and $ENV{NG_DEBUG} > 1; | ||
| 101 | return $res if $self->raw_response; | ||
| 102 | return $res->decoded_content if $self->raw_string; | ||
| 103 | |||
| 104 | if ($res->header('Content-Type') and $res->header('Content-Type') =~ 'application/json') { | ||
| 105 | my $json = $res->decoded_content; | ||
| 106 | $data = eval { $self->json->decode($json) }; | ||
| 107 | unless ($data) { | ||
| 108 | # We tolerate bad JSON for errors, | ||
| 109 | # otherwise we just rethrow the JSON parsing problem. | ||
| 110 | die unless $res->is_error; | ||
| 111 | $data = { message => $res->message }; | ||
| 112 | } | ||
| 113 | } else { | ||
| 114 | $data = { message => $res->message }; | ||
| 115 | } | ||
| 116 | |||
| 117 | ## be smarter | ||
| 118 | if (wantarray) { | ||
| 119 | return @$data if ref $data eq 'ARRAY'; | ||
| 120 | return %$data if ref $data eq 'HASH'; | ||
| 121 | } | ||
| 122 | |||
| 123 | return $data; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _make_request { | ||
| 127 | my($self, $req) = @_; | ||
| 128 | |||
| 129 | my $cached_res = $self->_get_shared_cache($req->uri); | ||
| 130 | |||
| 131 | if ($cached_res) { | ||
| 132 | $req->header("If-None-Match" => $cached_res->header("ETag")); | ||
| 133 | my $res = $self->ua->request($req); | ||
| 134 | |||
| 135 | if ($res->code == 304) { | ||
| 136 | return $cached_res; | ||
| 137 | } | ||
| 138 | |||
| 139 | $self->_set_shared_cache($req->uri, $res); | ||
| 140 | |||
| 141 | return $res; | ||
| 142 | } else { | ||
| 143 | my $res = $self->ua->request($req); | ||
| 144 | $self->_set_shared_cache( $req->uri, $res); | ||
| 145 | return $res; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _get_shared_cache { | ||
| 150 | my ($self, $uri) = @_; | ||
| 151 | return $self->cache->get($uri); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _set_shared_cache { | ||
| 155 | my($self, $uri, $response) = @_; | ||
| 156 | $self->cache->set($uri, $response); | ||
| 157 | } | ||
| 158 | |||
| 159 | no Moo; | ||
| 160 | |||
| 161 | 1; | ||
| 162 | __END__ |