Megaport/Client.pm

filter: Statementstmt 34.2% 25 / 73 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 25.9% 36 / 139 CC 24 SCAR 42.7
CC
24
Cov
21%
login
156.0
request
72.0
verify
20.0
CRAP 71.5
↑ Summary Megaport/Internal/_Result.pm »
1 package Megaport::Client;
2  
3 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = "1.00";
8  
9 3 ▶use Carp qw(carp cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Class::Tiny qw(token uri no_verify debug errstr), {
Subroutine BEGIN: called CC=1 SCAR=0.2
15 0 ▶ ua => sub { LWP::UserAgent->new(agent => __PACKAGE__ . '/' . $VERSION) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 3 };
17  
18 0 ▶sub login {
Subroutine login: not called CC=12 SCAR=50.5
Pod documented
19 0 my ($self, $args) = @_;
20  
21 0 ▶ if (exists $args->{token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"token"}) 0 0 0
22 0 $self->token($args->{token});
23 0 ▶ return $self->no_verify ? $self : $self->verify;
Branch: 1 branch
BranchTrueFalseTotal
$self->no_verify ? : 0 0 0
24 }
25  
26 0 ▶ if ($args->{username} && $args->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"username"} and $args->{"password"}) 0 0 0
Condition0%0/3
$args->{"username"} and $args->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"username"} and $args->{"password"}
$args->{"username"} $args->{"password"}
Truth table0%0/3
A and B
A= $args->{"username"}B= $args->{"password"}
ABresult
0X0
100
111
27 my $response = $self->ua->post($self->uri . '/login', {
28 username => $args->{username},
29 password => $args->{password}
30 0 });
31  
32 0 ▶ $self->_dump_response($response) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
33  
34 0 my $obj;
35 0 eval { $obj = decode_json $response->decoded_content };
36 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
37 0 ▶ $self->errstr('LoginError: API did not return valid JSON') and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->errstr("LoginError: API did not return valid JSON") and return 0 0 0
38 }
39  
40 0 ▶ if (!$response->is_success || !$obj->{data}->{session}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success and $obj->{"data"}{"session"}) 0 0 0
Condition0%0/3
$response->is_success and $obj->{"data"}{"session"}
!ll&&!rl&&r
000
MC/DC0%0/2
$response->is_success and $obj->{"data"}{"session"}
$response->is_success $obj->{"data"}{"session"}
Truth table0%0/3
A and B
A= $response->is_successB= $obj->{"data"}{"session"}
ABresult
0X0
100
111
41 0 ▶ $self->errst('LoginError: ' . $obj->{message}) and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->errst('LoginError: ' . $obj->{"message"}) and return 0 0 0
42 }
43  
44 0 $self->token($obj->{data}->{session});
45 0 ▶ return $self->no_verify ? $self : $self->verify;
Branch: 1 branch
BranchTrueFalseTotal
$self->no_verify ? : 0 0 0
46 }
47 }
48  
49 0 ▶sub verify {
Subroutine verify: not called CC=4 SCAR=30.0
Pod documented
50 0 my ($self) = @_;
51  
52 0 my $response = $self->ua->post($self->uri . '/login/' . $self->token);
53  
54 0 ▶ $self->_dump_response($response) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
55  
56 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
57 0 ▶ $self->_dump_response($response) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
58 0 $self->errstr('LoginError: Verifying session token failed')
59 }
60  
61 0 return $self;
62 }
63  
64 0 ▶sub request {
Subroutine request: not called CC=8 SCAR=42.8
Pod documented
65 0 my ($self, $op, $path, %args) = @_;
66  
67 my $headers = [
68 'X-Auth-Token' => $self->token,
69 0 ▶ exists $args{headers} ? @{$args{headers}} : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'headers'} ? : 0 0 0
70 ];
71  
72 0 my $uri = $self->uri . $path;
73 0 my $request = HTTP::Request->new($op => $uri, $headers);
74 0 ▶ $request->content($args{content}) if $args{content};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"content"} 0 0 0
75  
76 0 my $response = $self->ua->request($request);
77  
78 0 ▶ $self->_dump_response($response) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
79  
80 0 my $obj;
81 0 eval { $obj = decode_json $response->decoded_content };
82 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
83 0 ▶ $self->errstr('RequestError: API did not return valid JSON') and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->errstr("RequestError: API did not return valid JSON") and return 0 0 0
84 }
85  
86 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
87 0 ▶ $self->errst('RequestError: ' . $obj->{message}) and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->errst('RequestError: ' . $obj->{"message"}) and return 0 0 0
88 }
89  
90 0 return $obj->{data};
91 }
92  
93 0 ▶sub _dump_response {
Subroutine _dump_response: not called CC=3 SCAR=24.8
94 0 my ($self, $response) = @_;
95  
96 0 my @error = split /\n/, $response->as_string;
97 0 $_ = " > $_" foreach @error;
98  
99 0 say STDERR "================ [ DEBUG ] ================";
100 0 say STDERR ' > ' . $response->request->method . ' ' . $response->request->uri . "\n";
101 0 say STDERR join("\n", @error);
102 0 ▶ cluck if !$response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
103 0 say STDERR "===========================================\n";
104 }
105  
106 3 1;
107 __END__
↑ Summary Megaport/Internal/_Result.pm »