line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Pithub::Base; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PLU'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Github v3 base class for all Pithub modules |
4
|
|
|
|
|
|
|
|
5
|
24
|
|
|
24
|
|
11961
|
use Moo; |
|
24
|
|
|
|
|
43
|
|
|
24
|
|
|
|
|
107
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01040'; |
8
|
|
|
|
|
|
|
|
9
|
24
|
|
|
24
|
|
6340
|
use Carp qw( croak ); |
|
24
|
|
|
|
|
65
|
|
|
24
|
|
|
|
|
1069
|
|
10
|
24
|
|
|
24
|
|
6002
|
use HTTP::Headers (); |
|
24
|
|
|
|
|
46241
|
|
|
24
|
|
|
|
|
532
|
|
11
|
24
|
|
|
24
|
|
9703
|
use HTTP::Request (); |
|
24
|
|
|
|
|
164533
|
|
|
24
|
|
|
|
|
571
|
|
12
|
24
|
|
|
24
|
|
5498
|
use JSON::MaybeXS qw( JSON ); |
|
24
|
|
|
|
|
64983
|
|
|
24
|
|
|
|
|
1189
|
|
13
|
24
|
|
|
24
|
|
14336
|
use LWP::UserAgent (); |
|
24
|
|
|
|
|
517249
|
|
|
24
|
|
|
|
|
576
|
|
14
|
24
|
|
|
24
|
|
10961
|
use Pithub::Result (); |
|
24
|
|
|
|
|
85
|
|
|
24
|
|
|
|
|
688
|
|
15
|
24
|
|
|
24
|
|
180
|
use URI (); |
|
24
|
|
|
|
|
40
|
|
|
24
|
|
|
|
|
65477
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
with 'Pithub::Result::SharedCache'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has 'auto_pagination' => ( |
21
|
|
|
|
|
|
|
default => sub { 0 }, |
22
|
|
|
|
|
|
|
is => 'rw', |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
has 'api_uri' => ( |
27
|
|
|
|
|
|
|
default => sub { URI->new('https://api.github.com') }, |
28
|
|
|
|
|
|
|
is => 'rw', |
29
|
|
|
|
|
|
|
trigger => sub { |
30
|
|
|
|
|
|
|
my ( $self, $uri ) = @_; |
31
|
|
|
|
|
|
|
$self->{api_uri} = URI->new("$uri"); |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has 'jsonp_callback' => ( |
37
|
|
|
|
|
|
|
clearer => 'clear_jsonp_callback', |
38
|
|
|
|
|
|
|
is => 'rw', |
39
|
|
|
|
|
|
|
predicate => 'has_jsonp_callback', |
40
|
|
|
|
|
|
|
required => 0, |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
has 'per_page' => ( |
45
|
|
|
|
|
|
|
clearer => 'clear_per_page', |
46
|
|
|
|
|
|
|
is => 'rw', |
47
|
|
|
|
|
|
|
predicate => 'has_per_page', |
48
|
|
|
|
|
|
|
default => 100, |
49
|
|
|
|
|
|
|
required => 0, |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has 'prepare_request' => ( |
54
|
|
|
|
|
|
|
clearer => 'clear_prepare_request', |
55
|
|
|
|
|
|
|
is => 'rw', |
56
|
|
|
|
|
|
|
predicate => 'has_prepare_request', |
57
|
|
|
|
|
|
|
required => 0, |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
has 'repo' => ( |
62
|
|
|
|
|
|
|
clearer => 'clear_repo', |
63
|
|
|
|
|
|
|
is => 'rw', |
64
|
|
|
|
|
|
|
predicate => 'has_repo', |
65
|
|
|
|
|
|
|
required => 0, |
66
|
|
|
|
|
|
|
); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has 'token' => ( |
70
|
|
|
|
|
|
|
clearer => 'clear_token', |
71
|
|
|
|
|
|
|
is => 'rw', |
72
|
|
|
|
|
|
|
predicate => '_has_token', |
73
|
|
|
|
|
|
|
required => 0, |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
has 'ua' => ( |
78
|
|
|
|
|
|
|
builder => '_build_ua', |
79
|
|
|
|
|
|
|
is => 'ro', |
80
|
|
|
|
|
|
|
lazy => 1, |
81
|
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
has 'user' => ( |
85
|
|
|
|
|
|
|
clearer => 'clear_user', |
86
|
|
|
|
|
|
|
is => 'rw', |
87
|
|
|
|
|
|
|
predicate => 'has_user', |
88
|
|
|
|
|
|
|
required => 0, |
89
|
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
has 'utf8' => ( |
93
|
|
|
|
|
|
|
is => 'ro', |
94
|
|
|
|
|
|
|
default => 1, |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
has '_json' => ( |
98
|
|
|
|
|
|
|
builder => '_build__json', |
99
|
|
|
|
|
|
|
is => 'ro', |
100
|
|
|
|
|
|
|
lazy => 1, |
101
|
|
|
|
|
|
|
); |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
my @TOKEN_REQUIRED = ( |
104
|
|
|
|
|
|
|
'DELETE /user/emails', |
105
|
|
|
|
|
|
|
'GET /user', |
106
|
|
|
|
|
|
|
'GET /user/emails', |
107
|
|
|
|
|
|
|
'GET /user/followers', |
108
|
|
|
|
|
|
|
'GET /user/following', |
109
|
|
|
|
|
|
|
'GET /user/keys', |
110
|
|
|
|
|
|
|
'GET /user/repos', |
111
|
|
|
|
|
|
|
'PATCH /user', |
112
|
|
|
|
|
|
|
'POST /user/emails', |
113
|
|
|
|
|
|
|
'POST /user/keys', |
114
|
|
|
|
|
|
|
'POST /user/repos', |
115
|
|
|
|
|
|
|
); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my @TOKEN_REQUIRED_REGEXP = ( |
118
|
|
|
|
|
|
|
qr{^DELETE }, |
119
|
|
|
|
|
|
|
qr{^GET /gists/starred$}, |
120
|
|
|
|
|
|
|
qr{^GET /gists/[^/]+/star$}, |
121
|
|
|
|
|
|
|
qr{^GET /issues$}, |
122
|
|
|
|
|
|
|
qr{^GET /orgs/[^/]+/members/.*$}, |
123
|
|
|
|
|
|
|
qr{^GET /orgs/[^/]+/teams$}, |
124
|
|
|
|
|
|
|
qr{^GET /repos/[^/]+/[^/]+/collaborators$}, |
125
|
|
|
|
|
|
|
qr{^GET /repos/[^/]+/[^/]+/collaborators/.*$}, |
126
|
|
|
|
|
|
|
qr{^GET /repos/[^/]+/[^/]+/hooks$}, |
127
|
|
|
|
|
|
|
qr{^GET /repos/[^/]+/[^/]+/hooks/.*$}, |
128
|
|
|
|
|
|
|
qr{^GET /repos/[^/]+/[^/]+/keys$}, |
129
|
|
|
|
|
|
|
qr{^GET /repos/[^/]+/[^/]+/keys/.*$}, |
130
|
|
|
|
|
|
|
qr{^GET /teams/.*$}, |
131
|
|
|
|
|
|
|
qr{^GET /teams/[^/]+/members$}, |
132
|
|
|
|
|
|
|
qr{^GET /teams/[^/]+/members/.*$}, |
133
|
|
|
|
|
|
|
qr{^GET /teams/[^/]+/repos$}, |
134
|
|
|
|
|
|
|
qr{^GET /teams/[^/]+/repos/.*$}, |
135
|
|
|
|
|
|
|
qr{^GET /user/following/.*$}, |
136
|
|
|
|
|
|
|
qr{^GET /user/keys/.*$}, |
137
|
|
|
|
|
|
|
qr{^GET /user/orgs$}, |
138
|
|
|
|
|
|
|
qr{^GET /user/starred/[^/]+/.*$}, |
139
|
|
|
|
|
|
|
qr{^GET /user/watched$}, |
140
|
|
|
|
|
|
|
qr{^GET /user/watched/[^/]+/.*$}, |
141
|
|
|
|
|
|
|
qr{^GET /users/[^/]+/events/orgs/.*$}, |
142
|
|
|
|
|
|
|
qr{^PATCH /gists/.*$}, |
143
|
|
|
|
|
|
|
qr{^PATCH /gists/[^/]+/comments/.*$}, |
144
|
|
|
|
|
|
|
qr{^PATCH /orgs/.*$}, |
145
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/.*$}, |
146
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/comments/.*$}, |
147
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/git/refs/.*$}, |
148
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/hooks/.*$}, |
149
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/issues/.*$}, |
150
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/issues/comments/.*$}, |
151
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/keys/.*$}, |
152
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/labels/.*$}, |
153
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/milestones/.*$}, |
154
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/pulls/.*$}, |
155
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/releases/.*$}, |
156
|
|
|
|
|
|
|
qr{^PATCH /repos/[^/]+/[^/]+/pulls/comments/.*$}, |
157
|
|
|
|
|
|
|
qr{^PATCH /teams/.*$}, |
158
|
|
|
|
|
|
|
qr{^PATCH /user/keys/.*$}, |
159
|
|
|
|
|
|
|
qr{^PATCH /user/repos/.*$}, |
160
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/releases/[^/]+/assets.*$}, |
161
|
|
|
|
|
|
|
qr{^POST /gists/[^/]+/comments$}, |
162
|
|
|
|
|
|
|
qr{^POST /orgs/[^/]+/repos$}, |
163
|
|
|
|
|
|
|
qr{^POST /orgs/[^/]+/teams$}, |
164
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/commits/[^/]+/comments$}, |
165
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/downloads$}, |
166
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/forks}, |
167
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/git/blobs$}, |
168
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/git/commits$}, |
169
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/git/refs}, |
170
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/git/tags$}, |
171
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/git/trees$}, |
172
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/hooks$}, |
173
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/hooks/[^/]+/test$}, |
174
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/issues$}, |
175
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/issues/[^/]+/comments}, |
176
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/issues/[^/]+/labels$}, |
177
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/keys$}, |
178
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/labels$}, |
179
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/milestones$}, |
180
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/pulls$}, |
181
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/releases$}, |
182
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/pulls/[^/]+/comments$}, |
183
|
|
|
|
|
|
|
qr{^POST /repos/[^/]+/[^/]+/pulls/[^/]+/requested_reviewers$}, |
184
|
|
|
|
|
|
|
qr{^PUT /gists/[^/]+/star$}, |
185
|
|
|
|
|
|
|
qr{^PUT /orgs/[^/]+/public_members/.*$}, |
186
|
|
|
|
|
|
|
qr{^PUT /repos/[^/]+/[^/]+/collaborators/.*$}, |
187
|
|
|
|
|
|
|
qr{^PUT /repos/[^/]+/[^/]+/issues/[^/]+/labels$}, |
188
|
|
|
|
|
|
|
qr{^PUT /repos/[^/]+/[^/]+/pulls/[^/]+/merge$}, |
189
|
|
|
|
|
|
|
qr{^PUT /teams/[^/]+/members/.*$}, |
190
|
|
|
|
|
|
|
qr{^PUT /teams/[^/]+/memberships/.*$}, |
191
|
|
|
|
|
|
|
qr{^PUT /teams/[^/]+/repos/.*$}, |
192
|
|
|
|
|
|
|
qr{^PUT /user/following/.*$}, |
193
|
|
|
|
|
|
|
qr{^PUT /user/starred/[^/]+/.*$}, |
194
|
|
|
|
|
|
|
qr{^PUT /user/watched/[^/]+/.*$}, |
195
|
|
|
|
|
|
|
); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub request { |
199
|
686
|
|
|
686
|
1
|
14680
|
my ( $self, %args ) = @_; |
200
|
|
|
|
|
|
|
|
201
|
686
|
|
66
|
|
|
1929
|
my $method = delete $args{method} || croak 'Missing mandatory key in parameters: method'; |
202
|
685
|
|
66
|
|
|
1627
|
my $path = delete $args{path} || croak 'Missing mandatory key in parameters: path'; |
203
|
684
|
|
|
|
|
1111
|
my $data = delete $args{data}; |
204
|
684
|
|
|
|
|
1076
|
my $options = delete $args{options}; |
205
|
684
|
|
|
|
|
958
|
my $params = delete $args{params}; |
206
|
|
|
|
|
|
|
|
207
|
684
|
100
|
|
|
|
2684
|
croak "Invalid method: $method" unless grep $_ eq $method, qw(DELETE GET PATCH POST PUT); |
208
|
|
|
|
|
|
|
|
209
|
683
|
|
|
|
|
1751
|
my $uri = $self->_uri_for($path); |
210
|
|
|
|
|
|
|
|
211
|
683
|
100
|
|
|
|
1761
|
if (my $host = delete $args{host}) { |
212
|
3
|
|
|
|
|
30
|
$uri->host($host); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
683
|
100
|
|
|
|
1864
|
if (my $query = delete $args{query}) { |
216
|
6
|
|
|
|
|
27
|
$uri->query_form(%$query); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
683
|
|
|
|
|
2205
|
my $request = $self->_request_for( $method, $uri, $data ); |
220
|
|
|
|
|
|
|
|
221
|
683
|
100
|
|
|
|
1603
|
if (my $headers = delete $args{headers}) { |
222
|
3
|
|
|
|
|
13
|
foreach my $header (keys %$headers) { |
223
|
3
|
|
|
|
|
11
|
$request->header($header, $headers->{$header}); |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
683
|
100
|
100
|
|
|
1741
|
if ( $self->_token_required( $method, $path ) && !$self->has_token($request) ) { |
228
|
112
|
|
|
|
|
672
|
croak sprintf 'Access token required for: %s %s (%s)', $method, $path, $uri; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
571
|
100
|
|
|
|
1257
|
if ($options) { |
232
|
227
|
100
|
|
|
|
675
|
croak 'The key options must be a hashref' unless ref $options eq 'HASH'; |
233
|
226
|
100
|
100
|
|
|
930
|
croak 'The key prepare_request in the options hashref must be a coderef' if $options->{prepare_request} && ref $options->{prepare_request} ne 'CODE'; |
234
|
|
|
|
|
|
|
|
235
|
225
|
100
|
|
|
|
472
|
if ( $options->{prepare_request} ) { |
236
|
224
|
|
|
|
|
505
|
$options->{prepare_request}->($request); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
569
|
100
|
|
|
|
11442
|
if ($params) { |
241
|
9
|
100
|
|
|
|
56
|
croak 'The key params must be a hashref' unless ref $params eq 'HASH'; |
242
|
8
|
|
|
|
|
25
|
my %query = ( $request->uri->query_form, %$params ); |
243
|
8
|
|
|
|
|
422
|
$request->uri->query_form(%query); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
568
|
|
|
|
|
2026
|
my $response = $self->_make_request($request); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
return Pithub::Result->new( |
249
|
|
|
|
|
|
|
auto_pagination => $self->auto_pagination, |
250
|
|
|
|
|
|
|
response => $response, |
251
|
|
|
|
|
|
|
utf8 => $self->utf8, |
252
|
21
|
|
|
21
|
|
471
|
_request => sub { $self->request(@_) }, |
253
|
568
|
|
|
|
|
12405
|
); |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
sub _make_request { |
258
|
568
|
|
|
568
|
|
1080
|
my($self, $request) = @_; |
259
|
|
|
|
|
|
|
|
260
|
568
|
|
|
|
|
1387
|
my $cache_key = $request->uri->as_string; |
261
|
568
|
100
|
|
|
|
7210
|
if( my $cached_response = $self->shared_cache->get($cache_key) ) { |
262
|
|
|
|
|
|
|
# Add the If-None-Match header from the cache's ETag |
263
|
|
|
|
|
|
|
# and make the request |
264
|
234
|
|
|
|
|
21738
|
$request->header( 'If-None-Match' => $cached_response->header('ETag') ); |
265
|
234
|
|
|
|
|
20127
|
my $response = $self->ua->request($request); |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
# Got 304 Not Modified, cache is still valid |
268
|
234
|
50
|
100
|
|
|
15011
|
return $cached_response if ($response->code || 0) == 304; |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
# The response changed, cache it and return it. |
271
|
234
|
|
|
|
|
3163
|
$self->shared_cache->set( $cache_key, $response ); |
272
|
234
|
|
|
|
|
100246
|
return $response; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
334
|
|
|
|
|
26583
|
my $response = $self->ua->request($request); |
276
|
334
|
|
|
|
|
21082
|
$self->shared_cache->set( $cache_key, $response ); |
277
|
334
|
|
|
|
|
163368
|
return $response; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
sub has_token { |
283
|
1264
|
|
|
1264
|
1
|
2317
|
my ($self, $request) = @_; |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
# If we have one specified in the object, return true |
286
|
1264
|
100
|
|
|
|
4275
|
return 1 if $self->_has_token; |
287
|
|
|
|
|
|
|
# If no request object here, we don't have a token |
288
|
419
|
100
|
|
|
|
1381
|
return 0 unless $request; |
289
|
|
|
|
|
|
|
|
290
|
113
|
100
|
|
|
|
301
|
return 1 if $request->header('Authorization'); |
291
|
112
|
|
|
|
|
4394
|
return 0; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub rate_limit { |
296
|
0
|
|
|
0
|
1
|
0
|
return shift->request( method => 'GET', path => '/rate_limit' ); |
297
|
|
|
|
|
|
|
} |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
sub _build__json { |
300
|
96
|
|
|
96
|
|
981
|
my ($self) = @_; |
301
|
96
|
|
|
|
|
340
|
return JSON->new->utf8($self->utf8); |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
sub _build_ua { |
305
|
27
|
|
|
27
|
|
764
|
my ($self) = @_; |
306
|
27
|
|
|
|
|
140
|
return LWP::UserAgent->new; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
sub _get_user_repo_args { |
310
|
476
|
|
|
476
|
|
835
|
my ( $self, $args ) = @_; |
311
|
476
|
100
|
|
|
|
1876
|
$args->{user} = $self->user unless defined $args->{user}; |
312
|
476
|
100
|
|
|
|
1386
|
$args->{repo} = $self->repo unless defined $args->{repo}; |
313
|
476
|
|
|
|
|
835
|
return $args; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) |
317
|
|
|
|
|
|
|
sub _create_instance { |
318
|
224
|
|
|
224
|
|
581
|
my ( $self, $class, @args ) = @_; |
319
|
|
|
|
|
|
|
|
320
|
224
|
|
|
|
|
3936
|
my %args = ( |
321
|
|
|
|
|
|
|
api_uri => $self->api_uri, |
322
|
|
|
|
|
|
|
auto_pagination => $self->auto_pagination, |
323
|
|
|
|
|
|
|
ua => $self->ua, |
324
|
|
|
|
|
|
|
utf8 => $self->utf8, |
325
|
|
|
|
|
|
|
@args, |
326
|
|
|
|
|
|
|
); |
327
|
|
|
|
|
|
|
|
328
|
224
|
|
|
|
|
15612
|
for my $attr (qw(repo token user per_page jsonp_callback prepare_request)) { |
329
|
|
|
|
|
|
|
# Allow overrides to set attributes to undef |
330
|
1344
|
100
|
|
|
|
2265
|
next if exists $args{$attr}; |
331
|
|
|
|
|
|
|
|
332
|
1341
|
|
|
|
|
1825
|
my $has_attr = "has_$attr"; |
333
|
1341
|
100
|
|
|
|
4299
|
$args{$attr} = $self->$attr if $self->$has_attr; |
334
|
|
|
|
|
|
|
} |
335
|
|
|
|
|
|
|
|
336
|
224
|
|
|
|
|
4658
|
return $class->new(%args); |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
## use critic |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
sub _request_for { |
341
|
683
|
|
|
683
|
|
1472
|
my ( $self, $method, $uri, $data ) = @_; |
342
|
|
|
|
|
|
|
|
343
|
683
|
|
|
|
|
2339
|
my $headers = HTTP::Headers->new; |
344
|
|
|
|
|
|
|
|
345
|
683
|
100
|
|
|
|
5371
|
if ( $self->has_token ) { |
346
|
451
|
|
|
|
|
2177
|
$headers->header( 'Authorization' => sprintf( 'token %s', $self->token ) ); |
347
|
|
|
|
|
|
|
} |
348
|
|
|
|
|
|
|
|
349
|
683
|
|
|
|
|
19941
|
my $request = HTTP::Request->new( $method, $uri, $headers ); |
350
|
|
|
|
|
|
|
|
351
|
683
|
100
|
|
|
|
64887
|
if ($data) { |
352
|
304
|
100
|
|
|
|
6378
|
$data = $self->_json->encode($data) if ref $data; |
353
|
304
|
|
|
|
|
4132
|
$request->content($data); |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
|
356
|
683
|
|
|
|
|
6683
|
$request->header( 'Content-Length' => length $request->content ); |
357
|
|
|
|
|
|
|
|
358
|
683
|
100
|
|
|
|
41800
|
if ( $self->has_prepare_request ) { |
359
|
204
|
|
|
|
|
690
|
$self->prepare_request->($request); |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
|
362
|
683
|
|
|
|
|
2203
|
return $request; |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
my %TOKEN_REQUIRED = map { ($_ => 1) } @TOKEN_REQUIRED; |
366
|
|
|
|
|
|
|
sub _token_required { |
367
|
683
|
|
|
683
|
|
1341
|
my ( $self, $method, $path ) = @_; |
368
|
|
|
|
|
|
|
|
369
|
683
|
|
|
|
|
1636
|
my $key = "${method} ${path}"; |
370
|
|
|
|
|
|
|
|
371
|
683
|
100
|
|
|
|
1788
|
return 1 if $TOKEN_REQUIRED{$key}; |
372
|
|
|
|
|
|
|
|
373
|
653
|
|
|
|
|
1252
|
foreach my $regexp (@TOKEN_REQUIRED_REGEXP) { |
374
|
34159
|
100
|
|
|
|
70963
|
return 1 if $key =~ /$regexp/; |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
|
377
|
326
|
|
|
|
|
1014
|
return 0; |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
sub _uri_for { |
381
|
683
|
|
|
683
|
|
1327
|
my ( $self, $path ) = @_; |
382
|
|
|
|
|
|
|
|
383
|
683
|
|
|
|
|
14001
|
my $uri = $self->api_uri->clone; |
384
|
683
|
|
|
|
|
9580
|
my $base_path = $uri->path; |
385
|
683
|
|
|
|
|
9118
|
$path =~ s/^$base_path//; |
386
|
683
|
|
|
|
|
1153
|
my @parts; |
387
|
683
|
|
|
|
|
3236
|
push @parts, split qr{/+}, $uri->path; |
388
|
683
|
|
|
|
|
10148
|
push @parts, split qr{/+}, $path; |
389
|
683
|
|
|
|
|
2037
|
$uri->path( join '/', grep { $_ } @parts ); |
|
3430
|
|
|
|
|
6339
|
|
390
|
|
|
|
|
|
|
|
391
|
683
|
50
|
|
|
|
23316
|
if ( $self->has_per_page ) { |
392
|
683
|
|
|
|
|
2260
|
my %query = ( $uri->query_form, per_page => $self->per_page ); |
393
|
683
|
|
|
|
|
12272
|
$uri->query_form(%query); |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
683
|
100
|
|
|
|
41288
|
if ( $self->has_jsonp_callback ) { |
397
|
203
|
|
|
|
|
464
|
my %query = ( $uri->query_form, callback => $self->jsonp_callback ); |
398
|
203
|
|
|
|
|
7446
|
$uri->query_form(%query); |
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
|
401
|
683
|
|
|
|
|
14157
|
return $uri; |
402
|
|
|
|
|
|
|
} |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) |
405
|
|
|
|
|
|
|
sub _validate_user_repo_args { |
406
|
471
|
|
|
471
|
|
973
|
my ( $self, $args ) = @_; |
407
|
471
|
|
|
|
|
1299
|
$args = $self->_get_user_repo_args($args); |
408
|
471
|
100
|
|
|
|
1379
|
croak 'Missing key in parameters: user' unless $args->{user}; |
409
|
450
|
100
|
|
|
|
1315
|
croak 'Missing key in parameters: repo' unless $args->{repo}; |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
## use critic |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
1; |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
__END__ |