blib/lib/WebService/BaseClientRole.pm

filter: Statementstmt 28.5% 20 / 70 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 26.6% 4 / 15 Podpod 100% 6 / 6 totaltotal 25.6% 30 / 117 CC 12 SCAR 26.0
CC
12
Cov
22%
req
42.0
get
12.0
__ANON__
6.0
CRAP 13.5
↑ Summary
1 package WebService::BaseClientRole;
2 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = '0.0008'; # VERSION
5  
6 1 ▶use HTTP::Request::Common qw(DELETE GET POST PUT);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has base_url => ( is => 'ro', required => 1 );
11  
12 has ua => (
13 is => 'ro',
14 lazy => 1,
15 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 0 my $self = shift;
17 0 my $ua = LWP::UserAgent->new;
18 0 $ua->timeout($self->timeout);
19 0 return $ua;
20 },
21 1 );
22  
23 1 has timeout => ( is => 'ro', default => 10 );
24  
25 1 has retries => ( is => 'ro', default => 0 );
26  
27 1 has logger => ( is => 'ro' );
28  
29 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
30 0 my ($self, $path, $params) = @_;
31 0 ▶ $params ||= {};
Condition0%0/2
$params ||= {}
l!l
00
MC/DC0%0/1
$params ||= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
32 0 my $q = '';
33 0 ▶ if (%$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$params) 0 0 0
34 0 $q = '?' . join '&', map { "$_=$params->{$_}" } keys %$params;
35 }
36 0 return $self->req(GET "$path$q");
37 }
38  
39 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
40 0 my ($self, $path, $params) = @_;
41 0 return $self->req(POST $path, content => encode_json $params);
42 }
43  
44 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
45 0 my ($self, $path, $params) = @_;
46 0 return $self->req(PUT $path, content => encode_json $params);
47 }
48  
49 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
50 0 my ($self, $path) = @_;
51 0 return $self->req(DELETE $path);
52 }
53  
54 # Prefix the path param of the http methods with the base_url
55 0 ▶around qw(delete get post put) => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
56 0 my $orig = shift;
57 0 my $self = shift;
58 0 my $path = shift;
59 0 ▶ die 'Path is missing' unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
60 0 my $url = $self->_url($path);
61 0 return $self->$orig($url, @_);
62 1 };
63  
64 0 ▶sub req {
Subroutine req: not called CC=6 SCAR=37.4
Pod documented
65 0 my ($self, $req) = @_;
66 0 warn 'WebService::BaseClientRole has been DEPRECATED in favor of'
67 . ' WebService::Client.';
68 0 $req->header(content_type => 'application/json');
69 0 $self->_log_request($req);
70 0 my $res = $self->ua->request($req);
71 0 Moo::Role->apply_roles_to_object($res, 'HTTP::Response::Stringable');
72 0 $self->_log_response($res);
73  
74 0 my $retries = $self->retries;
75 0 ▶ while ($res->code =~ /^5/ and $retries--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code =~ /^5/ and $retries--) 0 0 0
Condition0%0/3
$res->code =~ /^5/ and $retries--
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code =~ /^5/ and $retries--
$res->code =~ /^5/ $retries--
Truth table0%0/3
A and B
A= $res->code =~ /^5/B= $retries--
ABresult
0X0
100
111
76 0 sleep 1;
77 0 $res = $self->ua->request($req);
78 0 $self->_log_response($res);
79 }
80  
81 0 ▶ return undef if $res->code =~ /404|410/;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code =~ /404|410/ 0 0 0
82 0 ▶ die $res unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
83 0 ▶ return $res->content ? decode_json($res->content) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$res->content ? : 0 0 0
84 }
85  
86 0 ▶sub _url {
Subroutine _url: not called CC=2 SCAR=17.9
87 0 my ($self, $path) = @_;
88 0 ▶ return $path =~ /^http/ ? $path : $self->base_url . $path;
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /^http/ ? : 0 0 0
89 }
90  
91 0 ▶sub _log_request {
Subroutine _log_request: not called CC=2 SCAR=17.9
92 0 my ($self, $req) = @_;
93 0 $self->log($req->method . ' => ' . $req->uri);
94 0 my $content = $req->content;
95 0 ▶ return unless length $content;
Branch: 1 branch
BranchTrueFalseTotal
unless length $content 0 0 0
96 0 $self->log($content);
97 }
98  
99 0 ▶sub _log_response {
Subroutine _log_response: not called CC=1 SCAR=6.9
100 0 my ($self, $res) = @_;
101 0 $self->log("$res");
102 }
103  
104 0 ▶sub log {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
105 0 my ($self, $msg) = @_;
106 0 ▶ return unless $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logger 0 0 0
107 0 $self->logger->DEBUG($msg);
108 }
109  
110 # ABSTRACT: [DEPRECATED] A base role for quickly and easily creating web service clients
111  
112  
113 1 1;
114  
115 __END__
↑ Summary