| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Megaport::Client; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 32 | use 5.10.0; | 
|  | 3 |  |  |  |  | 7 |  | 
| 4 | 3 |  |  | 3 |  | 9 | use strict; | 
|  | 3 |  |  |  |  | 2 |  | 
|  | 3 |  |  |  |  | 53 |  | 
| 5 | 3 |  |  | 3 |  | 8 | use warnings; | 
|  | 3 |  |  |  |  | 2 |  | 
|  | 3 |  |  |  |  | 104 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = "1.00"; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 3 |  |  | 3 |  | 9 | use Carp qw(carp cluck); | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 111 |  | 
| 10 | 3 |  |  | 3 |  | 1667 | use JSON::XS; | 
|  | 3 |  |  |  |  | 15015 |  | 
|  | 3 |  |  |  |  | 156 |  | 
| 11 | 3 |  |  | 3 |  | 1052 | use HTTP::Request; | 
|  | 3 |  |  |  |  | 45765 |  | 
|  | 3 |  |  |  |  | 74 |  | 
| 12 | 3 |  |  | 3 |  | 1546 | use LWP::UserAgent; | 
|  | 3 |  |  |  |  | 52147 |  | 
|  | 3 |  |  |  |  | 193 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | use Class::Tiny qw(token uri no_verify debug errstr), { | 
| 15 | 0 |  |  |  |  |  | ua => sub { LWP::UserAgent->new(agent => __PACKAGE__ . '/' . $VERSION) } | 
| 16 | 3 |  |  | 3 |  | 1540 | }; | 
|  | 3 |  |  |  |  | 6599 |  | 
|  | 3 |  |  |  |  | 22 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub login { | 
| 19 | 0 |  |  | 0 | 1 |  | my ($self, $args) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 0 | 0 |  |  |  |  | if (exists $args->{token}) { | 
| 22 | 0 |  |  |  |  |  | $self->token($args->{token}); | 
| 23 | 0 | 0 |  |  |  |  | return $self->no_verify ? $self : $self->verify; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 | 0 | 0 |  |  |  | if ($args->{username} && $args->{password}) { | 
| 27 |  |  |  |  |  |  | my $response = $self->ua->post($self->uri . '/login', { | 
| 28 |  |  |  |  |  |  | username => $args->{username}, | 
| 29 |  |  |  |  |  |  | password => $args->{password} | 
| 30 | 0 |  |  |  |  |  | }); | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 | 0 |  |  |  |  | $self->_dump_response($response) if $self->debug; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  |  | my $obj; | 
| 35 | 0 |  |  |  |  |  | eval { $obj = decode_json $response->decoded_content }; | 
|  | 0 |  |  |  |  |  |  | 
| 36 | 0 | 0 |  |  |  |  | if ($@) { | 
| 37 | 0 | 0 |  |  |  |  | $self->errstr('LoginError: API did not return valid JSON') and return; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 | 0 | 0 |  |  |  | if (!$response->is_success || !$obj->{data}->{session}) { | 
| 41 | 0 | 0 |  |  |  |  | $self->errst('LoginError: ' . $obj->{message}) and return; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 0 |  |  |  |  |  | $self->token($obj->{data}->{session}); | 
| 45 | 0 | 0 |  |  |  |  | return $self->no_verify ? $self : $self->verify; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub verify { | 
| 50 | 0 |  |  | 0 | 1 |  | my ($self) = @_; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | my $response = $self->ua->post($self->uri . '/login/' . $self->token); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 | 0 |  |  |  |  | $self->_dump_response($response) if $self->debug; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 | 0 |  |  |  |  | if (!$response->is_success) { | 
| 57 | 0 | 0 |  |  |  |  | $self->_dump_response($response) if $self->debug; | 
| 58 | 0 |  |  |  |  |  | $self->errstr('LoginError: Verifying session token failed') | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 0 |  |  |  |  |  | return $self; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub request { | 
| 65 | 0 |  |  | 0 | 1 |  | my ($self, $op, $path, %args) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | my $headers = [ | 
| 68 |  |  |  |  |  |  | 'X-Auth-Token' => $self->token, | 
| 69 | 0 | 0 |  |  |  |  | exists $args{headers} ? @{$args{headers}} : () | 
|  | 0 |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | ]; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 0 |  |  |  |  |  | my $uri = $self->uri . $path; | 
| 73 | 0 |  |  |  |  |  | my $request = HTTP::Request->new($op => $uri, $headers); | 
| 74 | 0 | 0 |  |  |  |  | $request->content($args{content}) if $args{content}; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | my $response = $self->ua->request($request); | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 | 0 |  |  |  |  | $self->_dump_response($response) if $self->debug; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  |  |  |  | my $obj; | 
| 81 | 0 |  |  |  |  |  | eval { $obj = decode_json $response->decoded_content }; | 
|  | 0 |  |  |  |  |  |  | 
| 82 | 0 | 0 |  |  |  |  | if ($@) { | 
| 83 | 0 | 0 |  |  |  |  | $self->errstr('RequestError: API did not return valid JSON') and return; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 | 0 |  |  |  |  | if (!$response->is_success) { | 
| 87 | 0 | 0 |  |  |  |  | $self->errst('RequestError: ' . $obj->{message}) and return; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 |  |  |  |  |  | return $obj->{data}; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | sub _dump_response { | 
| 94 | 0 |  |  | 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 | 0 |  |  |  |  | cluck if !$response->is_success; | 
| 103 | 0 |  |  |  |  |  | say STDERR "===========================================\n"; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | 1; | 
| 107 |  |  |  |  |  |  | __END__ |