blib/lib/GitLab/API/v4/RESTClient.pm

filter: Statementstmt 75.5% 102 / 135 Branchbran 50.0% 19 / 38 Conditioncond 31.8% 7 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 79.1% 19 / 24 Podpod 0.0% 0 / 3 totaltotal 62.8% 147 / 234 SCAR 40.9
CC
28
Cov
66%
request
102.7
croak
2.0
croakf
2.0
CRAP 59.8
« blib/lib/GitLab/API/v4.pm ↑ Summary blib/lib/GitLab/API/v4/Config.pm »
1 package GitLab::API::v4::RESTClient;
2 1 our $VERSION = '0.25';
3  
4 =encoding utf8
5  
6 =head1 NAME
7  
8 GitLab::API::v4::RESTClient - The HTTP client that does the heavy lifting.
9  
10 =head1 DESCRIPTION
11  
12 Currently this class uses L<HTTP::Tiny> and L<JSON> to do its job. This may
13 change, and the interface may change, so documentation is lacking in order
14 to not mislead people.
15  
16 If you do want to customize how this class works then take a look at the
17 source.
18  
19 =head1 ATTRIBUTES
20  
21 =head1 http_tiny_request
22  
23 my $req = $api->rest_client->http_tiny_request();
24  
25 The most recent request arrayref as passed to L<HTTP::Tiny>.
26  
27 If this is C<undef> then no request has been made.
28  
29 =head1 http_tiny_response
30  
31 my $res = $api->rest_client->http_tiny_response();
32  
33 The most recent response hashref as passed back from L<HTTP::Tiny>.
34  
35 If this is C<undef> and L</request> is defined then no response was received
36 and you will have encountered an error when making the request
37  
38 =cut
39  
40 1 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use HTTP::Tiny::Multipart;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use Types::Common::Numeric -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Types::Common::String -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 sub croak {
58 0 ▶ local $Carp::Internal{ 'GitLab::API::v4' } = 1;
Subroutine croak: not called CC=1 SCAR=6.9
Pod undocumented
59 0 local $Carp::Internal{ 'GitLab::API::v4::RESTClient' } = 1;
60  
61 0 return Carp::croak( @_ );
62 }
63  
64 sub croakf {
65 0 ▶ my $msg = shift;
Subroutine croakf: not called CC=1 SCAR=6.9
Pod undocumented
66 0 $msg = sprintf( $msg, @_ );
67 0 return croak( $msg );
68 }
69  
70 1 has _clean_base_url => (
71 is => 'lazy',
72 init_arg => undef,
73 builder => '_build_clean_base_url',
74 );
75 sub _build_clean_base_url {
76 2 ▶ my ($self) = @_;
Subroutine _build_clean_base_url: called CC=1 SCAR=0.0
77 2 my $url = $self->base_url();
78  
79 # Remove any leading slash so that request() does not build URLs
80 # with double slashes when joining the base_url with the path.
81 # If double slashes were allowed then extra unecessary redirects
82 # could happen.
83 2 $url =~ s{/+$}{};
84  
85 2 return URI->new( $url )->canonical();
86 }
87  
88 1 has base_url => (
89 is => 'ro',
90 isa => NonEmptySimpleStr,
91 required => 1,
92 );
93  
94 1 has retries => (
95 is => 'ro',
96 isa => PositiveOrZeroInt,
97 default => 0,
98 );
99  
100 1 has http_tiny => (
101 is => 'lazy',
102 isa => InstanceOf[ 'HTTP::Tiny' ],
103 );
104 sub _build_http_tiny {
105 0 ▶ return HTTP::Tiny->new();
Subroutine _build_http_tiny: not called CC=1 SCAR=6.9
106 }
107  
108 1 has json => (
109 is => 'lazy',
110 isa => HasMethods[ 'encode', 'decode' ],
111 );
112 sub _build_json {
113 2 ▶ return JSON->new->utf8->allow_nonref();
Subroutine _build_json: called CC=1 SCAR=0.0
114 }
115  
116 1 has http_tiny_request => (
117 is => 'ro',
118 writer => '_set_request',
119 clearer => '_clear_request',
120 init_arg => undef,
121 );
122  
123 1 has http_tiny_response => (
124 is => 'ro',
125 writer => '_set_response',
126 clearer => '_clear_response',
127 init_arg => undef,
128 );
129  
130 # The purpose of this method is for tests to have a place to inject themselves.
131 sub _http_tiny_request {
132 0 ▶ my ($self, $req_method, $req) = @_;
Subroutine _http_tiny_request: not called CC=1 SCAR=6.9
133  
134 0 return $self->http_tiny->$req_method( @$req );
135 }
136  
137 sub request {
138 11 ▶ my ($self, $verb, $raw_path, $path_vars, $options) = @_;
Subroutine request: called CC=28 SCAR=46.3
Pod undocumented
139  
140 11 $self->_clear_request();
141 11 $self->_clear_response();
142  
143 11 ▶ $options = { %{ $options || {} } };
Branch: 1 branch
BranchTrueFalseTotal
$options or {} 0 11 11
144 11 my $query = delete $options->{query};
145 11 my $content = delete $options->{content};
146 11 ▶ my $headers = $options->{headers} = { %{ $options->{headers} || {} } };
Branch: 1 branch
BranchTrueFalseTotal
$options->{"headers"} or {} 0 11 11
147  
148 # Convert foo/:bar/baz into foo/%s/baz.
149 11 my $path = $raw_path;
150 11 $path =~ s{:[^/]+}{%s}g;
151 # sprintf will throw if the number of %s doesn't match the size of @$path_vars.
152 # Might be nice to catch that and provide a better error message, but that should
153 # never happen as the API methods verify the argument size before we get here.
154 11 ▶ $path = sprintf($path, (map { uri_escape($_) } @$path_vars)) if @$path_vars;
Branch: 1 branch
BranchTrueFalseTotal
if @$path_vars 2 9 11
155  
156 11 $log->tracef( 'Making %s request against %s', $verb, $path );
157  
158 11 my $url = $self->_clean_base_url->clone();
159 11 $url->path( $url->path() . '/' . $path );
160 11 ▶ $url->query_form( $query ) if defined $query;
Branch: 1 branch
BranchTrueFalseTotal
if defined $query 0 11 11
161 11 $url = "$url"; # No more changes to the url from this point forward.
162  
163 11 my $req_method = 'request';
164 11 my $req = [ $verb, $url, $options ];
165  
166 11 ▶ if ($verb eq 'POST' and ref($content) eq 'HASH' and $content->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq "POST" and ref $content eq "HASH" and $content->{"file"}) 0 11 11
Condition66%4/6
$verb eq "POST" and ref $content eq "HASH" and $content->{"file"}
!ll&&!rl&&r
830
$verb eq "POST" and ref $content eq "HASH"
!ll&&!rl&&r
803
MC/DC0%0/3
$verb eq "POST" and ref $content eq "HASH" and $content->{"file"}
$verb eq "POST" ref $content eq "HASH" $content->{"file"}
Truth table50%2/4
A and B and C
A= $verb eq "POST"B= ref $content eq "HASH"C= $content->{"file"}
ABCresult
0XX0
10X0
1100
1111
167 0 $content = { %$content };
168 0 my $file = path( delete $content->{file} );
169  
170 0 ▶ unless (-f $file and -r $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file and -r $file) 0 0 0
Condition0%0/3
-f $file and -r $file
!ll&&!rl&&r
000
MC/DC0%0/2
-f $file and -r $file
-f $file -r $file
Truth table0%0/3
A and B
A= -f $fileB= -r $file
ABresult
0X0
100
111
171 0 local $Carp::Internal{ 'GitLab::API::v4' } = 1;
172 0 local $Carp::Internal{ 'GitLab::API::v4::RESTClient' } = 1;
173 0 croak "File $file is not readable";
174 }
175  
176 # Might as well mask the filename, but leave the extension.
177 0 my $filename = $file->basename(); # foo/bar.txt => bar.txt
178 0 $filename =~ s{^.*?(\.[^.]+|)}{upload$1}; # bar.txt => upload.txt
179  
180 0 my $data = {
181 file => {
182 filename => $filename,
183 content => $file->slurp(),
184 },
185 };
186  
187 0 $req->[0] = $req->[1]; # Replace method with url.
188 0 $req->[1] = $data; # Put data where url was.
189 # So, req went from [$verb,$url,$options] to [$url,$data,$options],
190 # per the post_multipart interface.
191  
192 0 $req_method = 'post_multipart';
193 0 ▶ $content = undef if ! %$content;
Branch: 1 branch
BranchTrueFalseTotal
unless %$content 0 0 0
194 }
195  
196 11 ▶ if (ref $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content) 4 7 11
197 4 $content = $self->json->encode( $content );
198 4 $headers->{'content-type'} = 'application/json';
199 4 $headers->{'content-length'} = length( $content );
200 }
201  
202 11 ▶ $options->{content} = $content if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 4 7 11
203  
204 11 $self->_set_request( $req );
205  
206 11 my $res;
207 11 my $tries_left = $self->retries();
208 11 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$tries_left > 0 and do { do { $res = $self->_http_tiny_request($req_method, $req); if ($res->{"status"} =~ /^5/) { --$tries_left; $GitLab::API::v4::RESTClient::log->warn("Request failed; retrying...") if $tries_left > 0; } else { $tries_left = 0; } } } 0 11 11
209 11 $res = $self->_http_tiny_request( $req_method, $req );
210 11 ▶ if ($res->{status} =~ m{^5}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'status'} =~ /^5/) { } 0 11 11
211 0 $tries_left--;
212 0 ▶ $log->warn('Request failed; retrying...') if $tries_left > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tries_left > 0 0 0 0
213 }
214 else {
215 11 $tries_left = 0
216 }
217 } while $tries_left > 0;
218  
219 11 $self->_set_response( $res );
220  
221 11 ▶ if ($res->{status} eq '404' and $verb eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} eq 404 and $verb eq "GET") 0 11 11
Condition33%1/3
$res->{"status"} eq 404 and $verb eq "GET"
!ll&&!rl&&r
1100
MC/DC0%0/2
$res->{"status"} eq 404 and $verb eq "GET"
$res->{"status"} eq 404 $verb eq "GET"
Truth table33%1/3
A and B
A= $res->{"status"} eq 404B= $verb eq "GET"
ABresult
0X0
100
111
222 0 return undef;
223 }
224  
225 # Special case for:
226 # https://github.com/bluefeet/GitLab-API-v4/issues/35#issuecomment-515533017
227 11 ▶ if ($res->{status} eq '403' and $verb eq 'GET' and $raw_path eq 'projects/:project_id/releases/:tag_name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"status"} eq 403 and $verb eq "GET" and $raw_path eq "projects/:project_id/releases/:tag_name") 0 11 11
Condition33%2/6
$res->{"status"} eq 403 and $verb eq "GET" and $raw_path eq "projects/:project_id/releases/:tag_name"
!ll&&!rl&&r
1100
$res->{"status"} eq 403 and $verb eq "GET"
!ll&&!rl&&r
1100
MC/DC0%0/3
$res->{"status"} eq 403 and $verb eq "GET" and $raw_path eq "projects/:project_id/releases/:tag_name"
$res->{"status"} eq 403 $verb eq "GET" $raw_path eq "projects/:project_id/releases/:tag_name"
Truth table25%1/4
A and B and C
A= $res->{"status"} eq 403B= $verb eq "GET"C= $raw_path eq "projects/:project_id/releases/:tag_name"
ABCresult
0XX0
10X0
1100
1111
228 0 return undef;
229 }
230  
231 11 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"success"}) 11 0 11
232 11 ▶ return undef if $res->{status} eq '204';
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} eq 204 5 6 11
233  
234 6 my $decode = $options->{decode};
235 6 ▶ $decode = 1 if !defined $decode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decode 6 0 6
236 6 ▶ return $res->{content} if !$decode;
Branch: 1 branch
BranchTrueFalseTotal
unless $decode 0 6 6
237  
238 return try{
239 6 ▶ $self->json->decode( $res->{content} );
Subroutine __ANON__: called CC=1 SCAR=0.0
240 }
241 catch {
242 croakf(
243 'Error decoding JSON (%s %s %s): ',
244 0 ▶ $verb, $url, $res->{status}, $_,
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 );
246 6 };
247 }
248  
249 0 ▶ my $glimpse = $res->{content} || '';
Condition0%0/2
$res->{'content'} || ''
l!l
00
MC/DC0%0/1
$res->{'content'} || ''
$res->{'content'}
Truth table0%0/2
A
A= $res->{'content'}
Aresult
11
00
250 0 $glimpse =~ s{\s+}{ }g;
251 0 ▶ if ( length($glimpse) > 50 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $glimpse > 50) 0 0 0
252 0 $glimpse = substr( $glimpse, 0, 50 );
253 0 $glimpse .= '...';
254 }
255  
256 croakf(
257 'Error %sing %s (HTTP %s): %s %s',
258 $verb, $url,
259 0 ▶ $res->{status}, ($res->{reason} || 'Unknown'),
Condition0%0/2
$res->{'reason'} || 'Unknown'
l!l
00
MC/DC0%0/1
$res->{'reason'} || 'Unknown'
$res->{'reason'}
Truth table0%0/2
A
A= $res->{'reason'}
Aresult
11
00
260 $glimpse,
261 );
262 }
263  
264 1 1;
265 __END__
« blib/lib/GitLab/API/v4.pm ↑ Summary blib/lib/GitLab/API/v4/Config.pm »