File Coverage

blib/lib/Net/Cisco/FMC/v1/Role/REST/Client.pm
Criterion Covered Total %
statement 20 20 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Net::Cisco::FMC::v1::Role::REST::Client;
2             $Net::Cisco::FMC::v1::Role::REST::Client::VERSION = '0.009001';
3             # ABSTRACT: Cisco Firepower Management Center (FMC) REST client
4              
5 1     1   570 use 5.024;
  1         4  
6 1     1   6 use feature 'signatures';
  1         2  
  1         126  
7 1     1   7 use Syntax::Keyword::Try;
  1         3  
  1         9  
8 1     1   91 use Time::HiRes qw( sleep );
  1         3  
  1         8  
9 1     1   560 use Sub::Retry;
  1         529  
  1         53  
10 1     1   7 use Moo::Role; # last for cleanup
  1         2  
  1         7  
11              
12 1     1   481 no warnings "experimental::signatures";
  1         2  
  1         597  
13              
14             with 'Role::REST::Client';
15             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             unless defined $_[4]->{authentication};
24             };
25              
26             around '_call' => sub($orig, $self, @params) {
27             my $try_count = 20;
28             my $try_timeout = 3;
29              
30             return retry $try_count, $try_timeout,
31             sub {
32             my $n = shift;
33             warn "api call retry #$n\n"
34             if $n > 1;
35             return $orig->($self, @params);
36             }, sub {
37             my $res = shift;
38             # relogin and retry when access token has become invalid
39             if ($res->code == 401
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             warn "access token invalid, relogin and retrying in $try_timeout seconds\n";
47             $self->relogin;
48             return 1;
49             }
50             # retry on error 429
51             elsif ($res->code == 429) {
52             warn "got error 429 too many requests, retrying in $try_timeout seconds\n";
53             return 1;
54             }
55             # pseudo-code from HTTP::Tiny
56             elsif ($res->code == 599
57             && $res->data =~ /Timed out while waiting for socket to become ready for reading/) {
58             warn "timeout, retrying in $try_timeout seconds\n";
59             return 1;
60             }
61             #elsif ($res->response->is_error ) {
62             # warn 'code ' . $res->code . ': ' . $res->data;
63             #}
64             return 0;
65             };
66             };
67              
68             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