| 1 | # ABSTRACT: Base class for remote Actions | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | package Pinto::Remote::Action; | ||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 9 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 9 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 9 | ▶ | use MooseX::MarkAsMethods ( autoclean => 1 ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 9 | ▶ | use MooseX::Types::Moose qw(Str Maybe); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 9 | ▶ | use URI; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 9 | ▶ | use JSON; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 9 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 9 | ▶ | use Pinto::Result; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 9 | ▶ | use Pinto::Constants qw(:protocol); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | 9 | ▶ | use Pinto::Util qw(current_time_offset); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | 9 | ▶ | use Pinto::Types qw(Uri); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | 9 | our $VERSION = '0.14'; # VERSION | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 9 | with qw(Pinto::Role::Plated Pinto::Role::UserAgent); | |||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 9 | has name => ( | |||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||
| 31 | isa => Str, | ||||||||||||||||||||||
| 32 | required => 1, | ||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||
| 34 | |||||||||||||||||||||||
| 35 | 9 | has root => ( | |||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||
| 37 | isa => Uri, | ||||||||||||||||||||||
| 38 | required => 1, | ||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | has args => ( | ||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||
| 43 | isa => 'HashRef', | ||||||||||||||||||||||
| 44 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 45 | 9 | ); | |||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | 9 | has username => ( | |||||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||||
| 49 | isa => Str, | ||||||||||||||||||||||
| 50 | required => 1 | ||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | 9 | has password => ( | |||||||||||||||||||||
| 54 | is => 'ro', | ||||||||||||||||||||||
| 55 | isa => Maybe [Str], | ||||||||||||||||||||||
| 56 | required => 1, | ||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | |||||||||||||||||||||||
| 62 | 0 | ▶ | sub execute { | ||||||||||||||||||||
|
Subroutine
execute: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | 0 | my $request = $self->_make_request; | |||||||||||||||||||||
| 66 | 0 | my $result = $self->_send_request( req => $request ); | |||||||||||||||||||||
| 67 | |||||||||||||||||||||||
| 68 | 0 | return $result; | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 72 | |||||||||||||||||||||||
| 73 | 0 | ▶ | sub _make_request { | ||||||||||||||||||||
|
Subroutine
_make_request: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||
| 74 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 0 | ▶ | my $action_name = $args{name} || $self->name; | ||||||||||||||||||||
|
Condition0%0/3
$args{'name'} || $self->name
MC/DC0%0/2
$args{'name'} || $self->name
$args{'name'}
$self->name
Truth table0%0/3
A || B
A= $args{'name'}B= $self->name
| |||||||||||||||||||||||
| 77 | 0 | ▶ | my $request_body = $args{body} || $self->_make_request_body; | ||||||||||||||||||||
|
Condition0%0/3
$args{'body'} || $self->_make_request_body
MC/DC0%0/2
$args{'body'} || $self->_make_request_body
$args{'body'}
$self->_make_request_body
Truth table0%0/3
A || B
A= $args{'body'}B= $self->_make_request_body
| |||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | 0 | my $uri = URI->new( $self->root ); | |||||||||||||||||||||
| 80 | 0 | $uri->path_segments( '', 'action', lc $action_name ); | |||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | 0 | my $request = POST( | |||||||||||||||||||||
| 83 | $uri, | ||||||||||||||||||||||
| 84 | Accept => $PINTO_PROTOCOL_ACCEPT, | ||||||||||||||||||||||
| 85 | Content => $request_body, | ||||||||||||||||||||||
| 86 | Content_Type => 'form-data', | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | 0 | ▶ | if ( defined $self->password ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 91 | 0 | $request->authorization_basic( $self->username, $self->password ); | |||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | 0 | return $request; | |||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 0 | ▶ | sub _make_request_body { | ||||||||||||||||||||
|
Subroutine
_make_request_body: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 100 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 101 | |||||||||||||||||||||||
| 102 | 0 | return [ $self->_chrome_args, $self->_pinto_args, $self->_action_args ]; | |||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 0 | ▶ | sub _chrome_args { | ||||||||||||||||||||
|
Subroutine
_chrome_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 108 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | 0 | my $chrome_args = { | |||||||||||||||||||||
| 111 | verbose => $self->chrome->verbose, | ||||||||||||||||||||||
| 112 | color => $self->chrome->color, | ||||||||||||||||||||||
| 113 | palette => $self->chrome->palette, | ||||||||||||||||||||||
| 114 | quiet => $self->chrome->quiet | ||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||
| 116 | |||||||||||||||||||||||
| 117 | 0 | return ( chrome => encode_json($chrome_args) ); | |||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | |||||||||||||||||||||||
| 121 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 122 | |||||||||||||||||||||||
| 123 | 0 | ▶ | sub _pinto_args { | ||||||||||||||||||||
|
Subroutine
_pinto_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 124 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | 0 | my $pinto_args = { | |||||||||||||||||||||
| 127 | username => $self->username, | ||||||||||||||||||||||
| 128 | time_offset => current_time_offset, | ||||||||||||||||||||||
| 129 | }; | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 0 | return ( pinto => encode_json($pinto_args) ); | |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | 0 | ▶ | sub _action_args { | ||||||||||||||||||||
|
Subroutine
_action_args: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 137 | 0 | my ($self) = @_; | |||||||||||||||||||||
| 138 | |||||||||||||||||||||||
| 139 | 0 | my $action_args = $self->args; | |||||||||||||||||||||
| 140 | |||||||||||||||||||||||
| 141 | 0 | return ( action => encode_json($action_args) ); | |||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | 0 | ▶ | sub _send_request { | ||||||||||||||||||||
|
Subroutine
_send_request: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 147 | 0 | my ( $self, %args ) = @_; | |||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 0 | ▶ | my $request = $args{req} || $self->_make_request; | ||||||||||||||||||||
|
Condition0%0/3
$args{'req'} || $self->_make_request
MC/DC0%0/2
$args{'req'} || $self->_make_request
$args{'req'}
$self->_make_request
Truth table0%0/3
A || B
A= $args{'req'}B= $self->_make_request
| |||||||||||||||||||||||
| 150 | 0 | my $status = 0; | |||||||||||||||||||||
| 151 | 0 | my $buffer = ''; | |||||||||||||||||||||
| 152 | |||||||||||||||||||||||
| 153 | # Currying in some extra args to the callback... | ||||||||||||||||||||||
| 154 | 0 | ▶ | my $callback = sub { $self->_response_callback( \$status, \$buffer, @_ ) }; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 155 | 0 | my $response = $self->request( $request, $callback ); | |||||||||||||||||||||
| 156 | |||||||||||||||||||||||
| 157 | 0 | ▶ | if ( not $response->is_success ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 158 | 0 | $self->error( $response->content ); | |||||||||||||||||||||
| 159 | 0 | return Pinto::Result->new( was_successful => 0 ); | |||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 0 | return Pinto::Result->new( was_successful => $status ); | |||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 166 | |||||||||||||||||||||||
| 167 | 0 | ▶ | sub _response_callback { | ||||||||||||||||||||
|
Subroutine
_response_callback: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||
| 168 | 0 | my ( $self, $status, $buffer, $data ) = @_; | |||||||||||||||||||||
| 169 | |||||||||||||||||||||||
| 170 | 0 | $data = ${$buffer}.$data; | |||||||||||||||||||||
| 171 | 0 | ▶ | while($data =~ /\G([^\n]*)\n/gc) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 172 | 0 | my $line = $1; | |||||||||||||||||||||
| 173 | 0 | ▶ | if ( $line eq $PINTO_PROTOCOL_STATUS_OK ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 174 | 0 | ${$status} = 1; | |||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||
| 176 | elsif ( $line eq $PINTO_PROTOCOL_PROGRESS_MESSAGE ) { | ||||||||||||||||||||||
| 177 | 0 | $self->chrome->show_progress; | |||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||
| 179 | elsif ( $line eq $PINTO_PROTOCOL_NULL_MESSAGE ) { | ||||||||||||||||||||||
| 180 | # Do nothing, discard message | ||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||
| 182 | elsif ( $line =~ m{^ \Q$PINTO_PROTOCOL_DIAG_PREFIX\E (.*)}x ) { | ||||||||||||||||||||||
| 183 | 0 | $self->chrome->diag($1); | |||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||
| 186 | 0 | $self->chrome->show($line); | |||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||
| 189 | #Save leftovers, use them in next packet | ||||||||||||||||||||||
| 190 | 0 | (${$buffer}) = ($data =~ /\G(.*)$/g); | |||||||||||||||||||||
| 191 | |||||||||||||||||||||||
| 192 | 0 | return 1; | |||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||
| 194 | |||||||||||||||||||||||
| 195 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||
| 196 | |||||||||||||||||||||||
| 197 | 9 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||
| 198 | |||||||||||||||||||||||
| 199 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||
| 200 | 9 | 1; | |||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | __END__ | ||||||||||||||||||||||