v1/Role/REST/Client.pm

filter: Statementstmt 60.4% 26 / 43 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 63.6% 7 / 11 Podpod n/a totaltotal 35.1% 33 / 94 SCAR 40.8
CC
13
Cov
35%
__ANON__
132.0
__ANON__
6.0
__ANON__
6.0
CRAP 59.1
« v1.pm ↑ Summary v1/Role/FixAccessruleLiterals.pm »
1 package Net::Cisco::FMC::v1::Role::REST::Client;
2 3 $Net::Cisco::FMC::v1::Role::REST::Client::VERSION = '0.009001';
3 # ABSTRACT: Cisco Firepower Management Center (FMC) REST client
4  
5 3 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Time::HiRes qw( sleep );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Sub::Retry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Moo::Role; # last for cleanup
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 with 'Role::REST::Client';
15 3 with 'Role::REST::Client::Auth::Basic';
16  
17 # has be be after with 'Role::REST::Client::Auth::Basic'; to be called before
18 # its method modifier
19 before '_call' => sub {
20 # disable http basic auth by default because only the generatetoken call
21 # needs it
22 $_[4]->{authentication} = 'none'
23 0 ▶ unless defined $_[4]->{authentication};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[4]{"authentication"} 0 0 0
24 3 };
25  
26 ▶around '_call' => sub($orig, $self, @params) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 my $try_count = 20;
28 0 my $try_timeout = 3;
29  
30 return retry $try_count, $try_timeout,
31 sub {
32 0 ▶ my $n = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
33 0 ▶ warn "api call retry #$n\n"
Branch: 1 branch
BranchTrueFalseTotal
if $n > 1 0 0 0
34 if $n > 1;
35 0 return $orig->($self, @params);
36 }, sub {
37 0 ▶ my $res = shift;
Subroutine __ANON__: not called CC=11 SCAR=48.8
38 # relogin and retry when access token has become invalid
39 0 ▶ if ($res->code == 401
Branch: 3 branches
BranchTrueFalseTotal
if ($res->code == 401 and ref $res->data eq 'HASH' and exists $res->data->{'error'} and exists $res->data->{'error'}{'messages'} and ref $res->data->{'error'}{'messages'} eq 'ARRAY' and exists $res->data->{'error'}{'messages'}[0]{'description'} and $res->data->{'error'}{'messages'}[0]{'description'} =~ /Access token invalid/u) { } 0 0 0
elsif ($res->code == 429) { } 0 0 0
elsif ($res->code == 599 and $res->data =~ /Timed out while waiting for socket to become ready for reading/u) { } 0 0 0
Condition0%0/18
$res->code == 401 and ref $res->data eq "HASH" and exists $res->data->{"error"} and exists $res->data->{"error"}{"messages"} and ref $res->data->{"error"}{"messages"} eq "ARRAY" and exists $res->data->{"error"}{"messages"}[0]{"description"} and $res->data->{"error"}{"messages"}[0]{"description"} =~ /Access token invalid/u
!ll&&!rl&&r
000
$res->code == 401 and ref $res->data eq "HASH" and exists $res->data->{"error"} and exists $res->data->{"error"}{"messages"} and ref $res->data->{"error"}{"messages"} eq "ARRAY" and exists $res->data->{"error"}{"messages"}[0]{"description"}
!ll&&!rl&&r
000
$res->code == 401 and ref $res->data eq "HASH" and exists $res->data->{"error"} and exists $res->data->{"error"}{"messages"} and ref $res->data->{"error"}{"messages"} eq "ARRAY"
!ll&&!rl&&r
000
$res->code == 401 and ref $res->data eq "HASH" and exists $res->data->{"error"} and exists $res->data->{"error"}{"messages"}
!ll&&!rl&&r
000
$res->code == 401 and ref $res->data eq "HASH" and exists $res->data->{"error"}
!ll&&!rl&&r
000
$res->code == 401 and ref $res->data eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/7
$res->code == 401 and ref $res->data eq "HASH" and exists $res->data->{"error"} and exists $res->data->{"error"}{"messages"} and ref $res->data->{"error"}{"messages"} eq "ARRAY" and exists $res->data->{"error"}{"messages"}[0]{"description"} and $res->data->{"error"}{"messages"}[0]{"description"} =~ /Access token invalid/u
$res->code == 401 ref $res->data eq "HASH" exists $res->data->{"error"} exists $res->data->{"error"}{"messages"} ref $res->data->{"error"}{"messages"} eq "ARRAY" exists $res->data->{"error"}{"messages"}[0]{"description"} $res->data->{"error"}{"messages"}[0]{"description"} =~ /Access token invalid/u
Truth table0%0/8
A and B and C and D and E and F and G
A= $res->code == 401B= ref $res->data eq "HASH"C= exists $res->data->{"error"}D= exists $res->data->{"error"}{"messages"}E= ref $res->data->{"error"}{"messages"} eq "ARRAY"F= exists $res->data->{"error"}{"messages"}[0]{"description"}G= $res->data->{"error"}{"messages"}[0]{"description"} =~ /Access token invalid/u
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
40 && ref $res->data eq 'HASH'
41 && exists $res->data->{error}
42 && exists $res->data->{error}->{messages}
43 && ref $res->data->{error}->{messages} eq 'ARRAY'
44 && exists $res->data->{error}->{messages}[0]->{description}
45 && $res->data->{error}->{messages}[0]->{description} =~ /Access token invalid/) {
46 0 warn "access token invalid, relogin and retrying in $try_timeout seconds\n";
47 0 $self->relogin;
48 0 return 1;
49 }
50 # retry on error 429
51 elsif ($res->code == 429) {
52 0 warn "got error 429 too many requests, retrying in $try_timeout seconds\n";
53 0 return 1;
54 }
55 # pseudo-code from HTTP::Tiny
56 ▶ elsif ($res->code == 599
Condition0%0/3
$res->code == 599 and $res->data =~ /Timed out while waiting for socket to become ready for reading/u
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code == 599 and $res->data =~ /Timed out while waiting for socket to become ready for reading/u
$res->code == 599 $res->data =~ /Timed out while waiting for socket to become ready for reading/u
Truth table0%0/3
A and B
A= $res->code == 599B= $res->data =~ /Timed out while waiting for socket to become ready for reading/u
ABresult
0X0
100
111
57 && $res->data =~ /Timed out while waiting for socket to become ready for reading/) {
58 0 warn "timeout, retrying in $try_timeout seconds\n";
59 0 return 1;
60 }
61 #elsif ($res->response->is_error ) {
62 # warn 'code ' . $res->code . ': ' . $res->data;
63 #}
64 0 return 0;
65 0 };
66 3 };
67  
68 3 1;
69  
70 __END__
71  
72 =pod
73  
74 =encoding UTF-8
75  
76 =head1 NAME
77  
78 Net::Cisco::FMC::v1::Role::REST::Client - Cisco Firepower Management Center (FMC) REST client
79  
80 =head1 VERSION
81  
82 version 0.009001
83  
84 =head1 AUTHOR
85  
86 Alexander Hartmaier <abraxxa@cpan.org>
87  
88 =head1 COPYRIGHT AND LICENSE
89  
90 This software is copyright (c) 2018 - 2023 by Alexander Hartmaier.
91  
92 This is free software; you can redistribute it and/or modify it under
93 the same terms as the Perl 5 programming language system itself.
94  
95 =cut
« v1.pm ↑ Summary v1/Role/FixAccessruleLiterals.pm »