| 1 | package GitLab::API::v3::RESTClient; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | GitLab::API::v3::RESTClient - GitLab API v3 REST client. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head2 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | This module provides the actual REST communication with the GitLab | ||||||||||||||||||||||||||||
| 10 | server and is powered by L<Role::REST::Client>. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | The various HTTP verb methods are wrapped so that they throw an | ||||||||||||||||||||||||||||
| 13 | exception if an unexpected response is received, except for GET | ||||||||||||||||||||||||||||
| 14 | requests that respond with a 404 code; these return C<undef> | ||||||||||||||||||||||||||||
| 15 | instead. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | If the request was successful then the response data is returned | ||||||||||||||||||||||||||||
| 18 | rather than the response object itself. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use Carp qw( confess ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Data::Dumper qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Role::REST::Client::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Types::Standard qw( Int ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | has retries => ( | |||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | isa => Int, | ||||||||||||||||||||||||||||
| 36 | default => 0, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | with 'Role::REST::Client'; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | foreach my $method (qw( post get head put delete options )) { | |||||||||||||||||||||||||||
| 42 | around $method => sub{ | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 44 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | 0 | my $path = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | my $res; | |||||||||||||||||||||||||||
| 48 | 0 | my $retry = $self->retries; | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | $log->infof( 'Making %s request against %s', uc($method), $path ); | |||||||||||||||||||||||||||
| 51 | 0 | $res = $self->$orig( "/$path", @_ ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($res->code =~ /^5/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $log->warn('Request failed. Retrying...') if $retry; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||
| 57 | 0 | $retry = 0; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } while --$retry >= 0; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | return undef if $res->code() eq '404' and $method eq 'get'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->code eq 404 and $method eq "get"
MC/DC0%0/2
$res->code eq 404 and $method eq "get"
$res->code eq 404
$method eq "get"
Truth table0%0/3
A and B
A= $res->code eq 404B= $method eq "get"
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($res->failed()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | local $Carp::Internal{ 'GitLab::API::v3::RESTClient' } = 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | confess sprintf( | |||||||||||||||||||||||||||
| 67 | 'Error %sing %s from %s (HTTP %s): %s %s', | ||||||||||||||||||||||||||||
| 68 | uc($method), $path, $self->server(), $res->code(), $res->error(), | ||||||||||||||||||||||||||||
| 69 | _dump_one_line( $res->data() ), | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return $res->data(); | |||||||||||||||||||||||||||
| 74 | 6 | }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Stolen and modified from Log::Any::Adapter::Core. | ||||||||||||||||||||||||||||
| 78 | sub _dump_one_line { | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my ($value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_dump_one_line: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return '<undef>' if !defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | ->Terse(1)->Dump() if ref($value); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | $value =~ s{\s+}{ }g; | |||||||||||||||||||||||||||
| 87 | 0 | return $value; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Aran Clary Deltac <bluefeetE<64>gmail.com> | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 100 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||