| 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
| |||||||||||||||||||||||||||||
| 22 | 0 | $self->token($args->{token}); | |||||||||||||||||||||||||||
| 23 | 0 | ▶ | return $self->no_verify ? $self : $self->verify; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ($args->{username} && $args->{password}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"username"} and $args->{"password"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $obj; | |||||||||||||||||||||||||||
| 35 | 0 | eval { $obj = decode_json $response->decoded_content }; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $self->errstr('LoginError: API did not return valid JSON') and return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if (!$response->is_success || !$obj->{data}->{session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->is_success and $obj->{"data"}{"session"}
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"}
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $self->errst('LoginError: ' . $obj->{message}) and return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | $self->token($obj->{data}->{session}); | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | return $self->no_verify ? $self : $self->verify; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (!$response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $self->_dump_response($response) if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $response = $self->ua->request($request); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $self->_dump_response($response) if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $obj; | |||||||||||||||||||||||||||
| 81 | 0 | eval { $obj = decode_json $response->decoded_content }; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $self->errstr('RequestError: API did not return valid JSON') and return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if (!$response->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $self->errst('RequestError: ' . $obj->{message}) and return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 0 | say STDERR "===========================================\n"; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | 1; | |||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||