REST/APICaller.pm

filter: Statementstmt 37.5% 21 / 56 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 33.3% 5 / 15 Podpod 0.0% 0 / 4 totaltotal 26.5% 26 / 98 CC 11 SCAR 19.0
CC
11
Cov
28%
post
12.0
put
12.0
__ANON__
12.0
CRAP 6.7
« REST/Config.pm ↑ Summary REST.pm »
1 package Opsview::REST::APICaller;
2 {
3 9 $Opsview::REST::APICaller::VERSION = '0.013';
4 }
5  
6 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use HTTP::Tiny 0.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has ua => (
15 is => 'ro',
16 0 ▶ default => sub { HTTP::Tiny->new(
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
__PACKAGE__->VERSION || '0.001_DEV'
l!l
00
MC/DC0%0/1
__PACKAGE__->VERSION || '0.001_DEV'
__PACKAGE__->VERSION
Truth table0%0/2
A
A= __PACKAGE__->VERSION
Aresult
11
00
17 agent => 'Opsview::REST/' . (__PACKAGE__->VERSION || '0.001_DEV'),
18 ); },
19 9 );
20  
21 has headers => (
22 is => 'rw',
23 0 ▶ default => sub { {
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 'Accept' => 'application/json',
25 'Content-type' => 'application/json',
26 }; },
27 9 );
28  
29 has json => (
30 is => 'ro',
31 0 ▶ default => sub { JSON->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 9 );
33  
34 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
35 0 my $self = shift;
36 0 my $r = $self->ua->get($self->base_url . shift, {
37 headers => $self->headers,
38 });
39 0 ▶ croak $self->_errmsg($r) unless $r->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"success"} 0 0 0
40  
41 0 return $self->json->decode($r->{content});
42 }
43  
44 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my $self = shift;
46 0 my $r = $self->ua->delete($self->base_url . shift, {
47 headers => $self->headers,
48 });
49 0 ▶ croak $self->_errmsg($r) unless $r->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"success"} 0 0 0
50  
51 0 return $self->json->decode($r->{content});
52 }
53  
54 0 ▶sub post {
Subroutine post: not called CC=3 SCAR=24.8
Pod undocumented
55 0 my ($self, $method, $data) = @_;
56  
57 0 my $stuff = { headers => $self->headers };
58 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 0 0 0
59 0 $stuff->{content} = $self->json->encode($data);
60 } else {
61 0 $stuff->{headers}->{'Content-Length'} = '0';
62 }
63  
64 0 my $r = $self->ua->post($self->base_url . $method, $stuff);
65 0 ▶ croak $self->_errmsg($r) unless $r->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"success"} 0 0 0
66  
67 0 return $self->json->decode($r->{content});
68 }
69  
70 0 ▶sub put {
Subroutine put: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my ($self, $method, $data) = @_;
72  
73 0 my $stuff = { headers => $self->headers };
74 0 ▶ $stuff->{content} = $self->json->encode($data) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 0 0 0
75  
76 0 my $r = $self->ua->put($self->base_url . $method, $stuff);
77 0 ▶ croak $self->_errmsg($r) unless $r->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"success"} 0 0 0
78  
79 0 return $self->json->decode($r->{content});
80 }
81  
82 0 ▶sub _errmsg {
Subroutine _errmsg: not called CC=2 SCAR=17.9
83 0 my ($self, $r) = @_;
84 my $exc = {
85 status => $r->{status},
86 reason => $r->{reason},
87 0 };
88  
89 0 ▶ if ($r->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"content"}) 0 0 0
90 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
91 0 my $resp;
92 0 $resp = $self->json->decode($r->{content});
93 0 ▶ $exc->{message} = $resp->{message} || undef;
Condition0%0/2
$resp->{'message'} || undef
l!l
00
MC/DC0%0/1
$resp->{'message'} || undef
$resp->{'message'}
Truth table0%0/2
A
A= $resp->{'message'}
Aresult
11
00
94 0 ▶ $exc->{detail} = $resp->{detail} || undef;
Condition0%0/2
$resp->{'detail'} || undef
l!l
00
MC/DC0%0/1
$resp->{'detail'} || undef
$resp->{'detail'}
Truth table0%0/2
A
A= $resp->{'detail'}
Aresult
11
00
95 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 $exc->{message} = $r->{content};
97 0 };
98 }
99  
100 0 return Opsview::REST::Exception->new($exc);
101 }
102  
103 9 1;
104 __END__
« REST/Config.pm ↑ Summary REST.pm »