| 1 | package Mojo::WebSocketProxy::Backend::JSONRPC; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use parent qw(Mojo::WebSocketProxy::Backend); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use feature qw(state); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 20 | ▶ | no indirect; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 20 | ▶ | use curry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 20 | ▶ | use MojoX::JSON::RPC::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 20 | our $VERSION = '0.16'; ## VERSION | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 20 | __PACKAGE__->register_type('jsonrpc'); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 23 | ▶ | sub url { return shift->{url} } | ||||||||||||||||||||||||||
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 20 | my $request_number = 0; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head2 call_rpc | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Description: Makes a remote call to a process returning the result to the client in JSON format. | ||||||||||||||||||||||||||||
| 27 | Before, After and error actions can be specified using call backs. | ||||||||||||||||||||||||||||
| 28 | It takes the following arguments | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =over 4 | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =item - $c : L<Mojolicious::Controller> | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item - $req_storage A hashref of attributes stored with the request. This routine uses some of the, | ||||||||||||||||||||||||||||
| 35 | following named arguments. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =over 4 | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =item - url, if not specified url set on C<< $self >> object is used. Must be supplied by either method. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item - method, The name of the method at the remote end (this is appened to C<< $request_storage->{url} >> ) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =item - msg_type, a name for this method if not supplied C<method> is used. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =item - call_params, a hashref of arguments on top of C<req_storage> to send to remote method. This will be suplemented with C<< $req_storage->{args} >> | ||||||||||||||||||||||||||||
| 46 | added as an C<args> key and be merged with C<< $req_storage->{stash_params} >> with stash_params overwriting any matching | ||||||||||||||||||||||||||||
| 47 | keys in C<call_params>. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item - rpc_response_callback, If supplied this will be run with C<< Mojolicious::Controller >> instance the rpc_response and C<< $req_storage >>. | ||||||||||||||||||||||||||||
| 50 | B<Note:> if C<< rpc_response_callback >> is supplied the success and error callbacks are not used. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =item - before_get_rpc_response, array ref of subroutines to run before the remote response, is passed C<< $c >> and C<< req_storage >> | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =item - after_get_rpc_response, arrayref of subroutines to run after the remote response, is passed C<< $c >> and C<< req_storage >> | ||||||||||||||||||||||||||||
| 55 | called only when there is an actual response from the remote call . IE if there is communication error with the call it will | ||||||||||||||||||||||||||||
| 56 | not be called versus an error message being returned from the call when it will. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =item - before_call, arrayref of subroutines called before the request to the remote service is made. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =item - error, a subroutine reference that will be called with C<< Mojolicious::Controller >> the rpc_response and C<< $req_storage >> | ||||||||||||||||||||||||||||
| 61 | if a C<< $response->{error} >> error was returned from the remote call, and C<< $req_storage->{rpc_response_cb} >> was not passed. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item - success, a subroutines reference that will be called if there was no error returned from the remote call and C<< $req_storage->{rpc_response_cb} >> was not passed. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item - rpc_failure_cb, a sub routine reference to call if the remote call fails at a http level. Called with C<< Mojolicious::Controller >> the rpc_response and C<< $req_storage >> | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =back | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =back | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Returns undef. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 23 | ▶ | sub call_rpc { | ||||||||||||||||||||||||||
|
Subroutine
call_rpc: called CC=9 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 23 | my ($self, $c, $req_storage) = @_; | |||||||||||||||||||||||||||
| 77 | 23 | state $client = MojoX::JSON::RPC::Client->new; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 23 | ▶ | my $url = $req_storage->{url} // $self->url; | ||||||||||||||||||||||||||
|
Condition33%1/3
$req_storage->{"url"} // $self->url
MC/DC0%0/2
$req_storage->{"url"} // $self->url
$req_storage->{"url"}
$self->url
Truth table33%1/3
A // B
A= $req_storage->{"url"}B= $self->url
| |||||||||||||||||||||||||||||
| 80 | 23 | ▶ | die 'No url found' unless $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 23 | $url .= $req_storage->{method}; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 23 | my $method = $req_storage->{method}; | |||||||||||||||||||||||||||
| 85 | 23 | ▶ | my $msg_type = $req_storage->{msg_type} ||= $req_storage->{method}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$req_storage->{"msg_type"} ||= $req_storage->{"method"}
MC/DC0%0/2
$req_storage->{"msg_type"} ||= $req_storage->{"method"}
$req_storage->{"msg_type"}
$req_storage->{"method"}
Truth table66%2/3
A ||= B
A= $req_storage->{"msg_type"}B= $req_storage->{"method"}
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 23 | ▶ | $req_storage->{call_params} ||= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$req_storage->{"call_params"} ||= {}
MC/DC0%0/1
$req_storage->{"call_params"} ||= {}
$req_storage->{"call_params"}
Truth table50%1/2
A
A= $req_storage->{"call_params"}
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 23 | my $rpc_response_cb = $self->get_rpc_response_cb($c, $req_storage); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 23 | ▶ | my $before_get_rpc_response_hook = delete($req_storage->{before_get_rpc_response}) || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $req_storage->{'before_get_rpc_response'} || []
MC/DC0%0/1
delete $req_storage->{'before_get_rpc_response'} || []
delete $req_storage->{'before_get_rpc_response'}
Truth table50%1/2
A
A= delete $req_storage->{'before_get_rpc_response'}
| |||||||||||||||||||||||||||||
| 92 | 23 | ▶ | my $after_got_rpc_response_hook = delete($req_storage->{after_got_rpc_response}) || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $req_storage->{'after_got_rpc_response'} || []
MC/DC0%0/1
delete $req_storage->{'after_got_rpc_response'} || []
delete $req_storage->{'after_got_rpc_response'}
Truth table50%1/2
A
A= delete $req_storage->{'after_got_rpc_response'}
| |||||||||||||||||||||||||||||
| 93 | 23 | ▶ | my $before_call_hook = delete($req_storage->{before_call}) || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $req_storage->{'before_call'} || []
MC/DC0%0/1
delete $req_storage->{'before_call'} || []
delete $req_storage->{'before_call'}
Truth table50%1/2
A
A= delete $req_storage->{'before_call'}
| |||||||||||||||||||||||||||||
| 94 | 23 | my $rpc_failure_cb = delete($req_storage->{rpc_failure_cb}); | |||||||||||||||||||||||||||
| 95 | # If this flag true, then proxy will not send the rpc response to the client back. | ||||||||||||||||||||||||||||
| 96 | # It is very useful when websocket app itself (not websocket client) want to get information from rpc. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 23 | my $block_response = delete($req_storage->{block_response}); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 23 | my $callobj = { | |||||||||||||||||||||||||||
| 101 | # enough for short-term uniqueness | ||||||||||||||||||||||||||||
| 102 | id => join('_', $$, $request_number++, time, (0 + [])), | ||||||||||||||||||||||||||||
| 103 | method => $method, | ||||||||||||||||||||||||||||
| 104 | params => $self->make_call_params($c, $req_storage), | ||||||||||||||||||||||||||||
| 105 | }; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 23 | $_->($c, $req_storage) for @$before_call_hook; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | $client->call( | ||||||||||||||||||||||||||||
| 110 | $url, $callobj, | ||||||||||||||||||||||||||||
| 111 | $client->$curry::weak( | ||||||||||||||||||||||||||||
| 112 | 23 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=28.7
| |||||||||||||||||||||||||||||
| 113 | 23 | my $client = shift; | |||||||||||||||||||||||||||
| 114 | 23 | my $res = pop; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 23 | $_->($c, $req_storage) for @$before_get_rpc_response_hook; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # unconditionally stop any further processing if client is already disconnected | ||||||||||||||||||||||||||||
| 119 | 23 | ▶ | return unless $c->tx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 23 | my $api_response; | |||||||||||||||||||||||||||
| 122 | 23 | ▶ | if (!$res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 2 | my $tx = $client->tx; | |||||||||||||||||||||||||||
| 124 | 2 | $req_storage->{req_url} = $tx->req->url; | |||||||||||||||||||||||||||
| 125 | 2 | my $err = $tx->error; | |||||||||||||||||||||||||||
| 126 | $rpc_failure_cb->( | ||||||||||||||||||||||||||||
| 127 | $c, $res, | ||||||||||||||||||||||||||||
| 128 | $req_storage, | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | code => $err->{code}, | ||||||||||||||||||||||||||||
| 131 | message => $err->{message}, | ||||||||||||||||||||||||||||
| 132 | 2 | ▶ | type => 'WrongResponse', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | }) if $rpc_failure_cb; | ||||||||||||||||||||||||||||
| 134 | 2 | ▶ | return if $block_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 1 | $api_response = $c->wsp_error($msg_type, 'WrongResponse', 'Sorry, an error occurred while processing your request.'); | |||||||||||||||||||||||||||
| 136 | 1 | $c->send({json => $api_response}, $req_storage); | |||||||||||||||||||||||||||
| 137 | 1 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 21 | $_->($c, $req_storage, $res) for @$after_got_rpc_response_hook; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 21 | ▶ | if ($res->is_error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $rpc_failure_cb->( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | $c, $res, | ||||||||||||||||||||||||||||
| 145 | $req_storage, | ||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||
| 147 | code => $res->error_code, | ||||||||||||||||||||||||||||
| 148 | message => $res->error_message, | ||||||||||||||||||||||||||||
| 149 | type => 'CallError', | ||||||||||||||||||||||||||||
| 150 | }) if $rpc_failure_cb; | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | return if $block_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | $api_response = $c->wsp_error($msg_type, 'CallError', 'Sorry, an error occurred while processing your request.'); | |||||||||||||||||||||||||||
| 153 | 0 | $c->send({json => $api_response}, $req_storage); | |||||||||||||||||||||||||||
| 154 | 0 | return; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 21 | $api_response = $rpc_response_cb->($res->result); | |||||||||||||||||||||||||||
| 158 | 21 | ▶ | return if $block_response || !$api_response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$block_response or not $api_response
MC/DC0%0/2
$block_response or not $api_response
$block_response
not $api_response
Truth table33%1/3
A or not(B)
A= $block_responseB= not $api_response
| |||||||||||||||||||||||||||||
| 159 | 21 | $c->send({json => $api_response}, $req_storage); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 21 | return; | |||||||||||||||||||||||||||
| 162 | 23 | })); | |||||||||||||||||||||||||||
| 163 | 23 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 20 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||