CLI/Request.pm

filter: Statementstmt 31.6% 19 / 60 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 4 / 8 Podpod 100% 4 / 4 totaltotal 31.7% 27 / 85 CC 8 SCAR 27.6
CC
8
Cov
26%
from_openapi
30.0
request
20.0
content
2.0
CRAP 15.9
« CLI.pm ↑ Summary CLI/Cmd.pm »
1 # ABSTRACT: Does the actual request to the API
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 package API::CLI::Request;
6  
7 2 our $VERSION = '0.001_001'; # TRIAL VERSION
8  
9 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 has openapi => ( is => 'ro' );
12 2 has method => ( is => 'ro' );
13 2 has path => ( is => 'ro' );
14 2 has req => ( is => 'rw' );
15 2 has url => ( is => 'rw' );
16 2 has verbose => ( is => 'ro' );
17  
18 0 ▶sub from_openapi {
Subroutine from_openapi: not called CC=5 SCAR=34.0
Pod documented
19 0 my ($class, %args) = @_;
20  
21 0 my $method = $args{method};
22 0 my $path = delete $args{path};
23 0 my $opt = delete $args{options};
24 0 my $params = delete $args{parameters};
25  
26 my $self = $class->new(
27 openapi => delete $args{openapi},
28 method => delete $args{method},
29 0 path => $path,
30 %args,
31 );
32  
33 0 my $host = $self->openapi->{host};
34 0 my $scheme = $self->openapi->{schemes}->[0];
35  
36 0 ▶ my $basePath = $self->openapi->{basePath} // '';
Condition0%0/2
$self->openapi->{"basePath"} // ""
l!l
00
MC/DC0%0/1
$self->openapi->{"basePath"} // ""
$self->openapi->{"basePath"}
Truth table0%0/2
A
A= $self->openapi->{"basePath"}
Aresult
11
00
37 0 ▶ $basePath = '' if $basePath eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $basePath eq "/" 0 0 0
38 0 my $url = URI->new("$scheme://$host$basePath$path");
39 0 my %query;
40 0 for my $name (sort keys %$opt) {
41 0 my $value = $opt->{ $name };
42 0 ▶ if ($name =~ s/^q-//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^q-//) 0 0 0
43 0 $query{ $name } = $value;
44 }
45 }
46 0 $url->query_form(%query);
47 0 $self->url($url);
48  
49 0 my $req = HTTP::Request->new( $self->method => $self->url );
50 0 $self->req($req);
51  
52 0 return $self;
53 }
54  
55 0 ▶sub request {
Subroutine request: not called CC=4 SCAR=30.0
Pod documented
56 0 my ($self) = @_;
57  
58 0 my $ua = LWP::UserAgent->new;
59 0 my $req = $self->req;
60 0 my $res = $ua->request($req);
61 0 my $code = $res->code;
62 0 my $content = $res->decoded_content;
63 0 my $status = $res->status_line;
64  
65 0 my $ct = $res->content_type;
66 0 ▶ my $out = $self->verbose ? "Response: $status ($ct)\n" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->verbose ? : 0 0 0
67 0 my $data;
68 0 my $ok = 0;
69 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 0 0 0
70 0 $ok = 1;
71 0 ▶ if ($ct eq 'application/json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "application/json") 0 0 0
72 0 my $coder = JSON::XS->new->ascii->pretty->allow_nonref;
73 0 $data = $coder->decode($content);
74 0 $content = $coder->encode($data);
75 }
76 }
77  
78 0 return ($ok, $out, $content);
79 }
80  
81 0 ▶sub content {
Subroutine content: not called CC=1 SCAR=6.9
Pod documented
82 0 shift->req->content(@_);
83 }
84  
85 0 ▶sub header {
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
86 0 shift->req->header(@_);
87 }
88  
89 2 1;
90  
91 __END__
« CLI.pm ↑ Summary CLI/Cmd.pm »