v4/RESTClient.pm

filter: Statementstmt 76.1% 102 / 134 Branchbran 50.0% 17 / 34 Conditioncond 34.6% 9 / 26 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 79.1% 19 / 24 Podpod 0.0% 0 / 3 totaltotal 62.5% 147 / 235 CC 28 SCAR 40.2
CC
28
Cov
66%
request
100.6
croak
2.0
croakf
2.0
CRAP 55.8
« v4.pm ↑ Summary v4/Paginator.pm »
1 package GitLab::API::v4::RESTClient;
2 1 our $VERSION = '0.27';
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::MaybeXS> to do its job.
13 This may change, and the interface may change, so documentation is lacking in
14 order 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::MaybeXS;
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 0 ▶sub croak {
Subroutine croak: not called CC=1 SCAR=6.9
Pod undocumented
58 0 local $Carp::Internal{ 'GitLab::API::v4' } = 1;
59 0 local $Carp::Internal{ 'GitLab::API::v4::RESTClient' } = 1;
60  
61 0 return Carp::croak( @_ );
62 }
63  
64 0 ▶sub croakf {
Subroutine croakf: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $msg = shift;
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 2 ▶sub _build_clean_base_url {
Subroutine _build_clean_base_url: called CC=1 SCAR=0.0
76 2 my ($self) = @_;
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 0 ▶sub _build_http_tiny {
Subroutine _build_http_tiny: not called CC=1 SCAR=6.9
105 0 return HTTP::Tiny->new( verify_SSL => 1 );
106 }
107  
108 1 has json => (
109 is => 'lazy',
110 isa => HasMethods[ 'encode', 'decode' ],
111 );
112 2 ▶sub _build_json {
Subroutine _build_json: called CC=1 SCAR=0.0
113 2 return JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1);
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 0 ▶sub _http_tiny_request {
Subroutine _http_tiny_request: not called CC=1 SCAR=6.9
132 0 my ($self, $req_method, $req) = @_;
133  
134 0 return $self->http_tiny->$req_method( @$req );
135 }
136  
137 11 ▶sub request {
Subroutine request: called CC=28 SCAR=46.1
Pod undocumented
138 11 my ($self, $verb, $raw_path, $path_vars, $options) = @_;
139  
140 11 $self->_clear_request();
141 11 $self->_clear_response();
142  
143 11 ▶ $options = { %{ $options || {} } };
Condition50%1/2
$options || {}
l!l
110
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
144 11 my $query = delete $options->{query};
145 11 my $content = delete $options->{content};
146 11 ▶ my $headers = $options->{headers} = { %{ $options->{headers} || {} } };
Condition50%1/2
$options->{'headers'} || {}
l!l
110
MC/DC0%0/1
$options->{'headers'} || {}
$options->{'headers'}
Truth table50%1/2
A
A= $options->{'headers'}
Aresult
11
00
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  
179 0 my $data = {
180 file => {
181 filename => $filename,
182 content => $file->slurp(),
183 },
184 };
185  
186 0 $req->[0] = $req->[1]; # Replace method with url.
187 0 $req->[1] = $data; # Put data where url was.
188 # So, req went from [$verb,$url,$options] to [$url,$data,$options],
189 # per the post_multipart interface.
190  
191 0 $req_method = 'post_multipart';
192 0 ▶ $content = undef if ! %$content;
Branch: 1 branch
BranchTrueFalseTotal
unless %$content 0 0 0
193 }
194  
195 11 ▶ if (ref $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content) 4 7 11
196 4 $content = $self->json->encode( $content );
197 4 $headers->{'content-type'} = 'application/json';
198 4 $headers->{'content-length'} = length( $content );
199 }
200  
201 11 ▶ $options->{content} = $content if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 4 7 11
202  
203 11 $self->_set_request( $req );
204  
205 11 my $res;
206 11 my $tries_left = $self->retries();
207 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
208 11 $res = $self->_http_tiny_request( $req_method, $req );
209 11 ▶ if ($res->{status} =~ m{^5}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{'status'} =~ /^5/) { } 0 11 11
210 0 $tries_left--;
211 0 ▶ $log->warn('Request failed; retrying...') if $tries_left > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $tries_left > 0 0 0 0
212 }
213 else {
214 11 $tries_left = 0
215 }
216 } while $tries_left > 0;
217  
218 11 $self->_set_response( $res );
219  
220 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
221 0 return undef;
222 }
223  
224 # Special case for:
225 # https://github.com/bluefeet/GitLab-API-v4/issues/35#issuecomment-515533017
226 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
227 0 return undef;
228 }
229  
230 11 ▶ if ($res->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"success"}) 11 0 11
231 11 ▶ return undef if $res->{status} eq '204';
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"status"} eq 204 5 6 11
232  
233 6 my $decode = $options->{decode};
234 6 ▶ $decode = 1 if !defined $decode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decode 6 0 6
235 6 ▶ return $res->{content} if !$decode;
Branch: 1 branch
BranchTrueFalseTotal
unless $decode 0 6 6
236  
237 6 ▶ return try{
Subroutine __ANON__: called CC=1 SCAR=0.0
238 6 $self->json->decode( $res->{content} );
239 }
240 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 croakf(
242 'Error decoding JSON (%s %s %s): ',
243 0 $verb, $url, $res->{status}, $_,
244 );
245 6 };
246 }
247  
248 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
249 0 $glimpse =~ s{\s+}{ }g;
250 0 ▶ if ( length($glimpse) > 50 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $glimpse > 50) 0 0 0
251 0 $glimpse = substr( $glimpse, 0, 50 );
252 0 $glimpse .= '...';
253 }
254  
255 croakf(
256 'Error %sing %s (HTTP %s): %s %s',
257 $verb, $url,
258 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
259 $glimpse,
260 );
261 }
262  
263 1 1;
264 __END__
« v4.pm ↑ Summary v4/Paginator.pm »