| 1 | package XAS::Lib::Mixins::JSON::Client; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.02'; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Params::Validate 'HASHREF'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | use XAS::Class | ||||||||||
| 8 | 1 | debug => 0, | |||||||||
| 9 | version => $VERSION, | ||||||||||
| 10 | base => 'XAS::Base', | ||||||||||
| 11 | codec => 'JSON', | ||||||||||
| 12 | constants => ':jsonrpc', | ||||||||||
| 13 | mixins => 'call', | ||||||||||
| 14 | 1 | ▶ | ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | #use Data::Dumper; | ||||||||||
| 17 | |||||||||||
| 18 | # ---------------------------------------------------------------------- | ||||||||||
| 19 | # Public Methods | ||||||||||
| 20 | # ---------------------------------------------------------------------- | ||||||||||
| 21 | |||||||||||
| 22 | sub call { | ||||||||||
| 23 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
call: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 24 | 0 | my $p = $self->validate_params(\@_, { | |||||||||
| 25 | -method => 1, | ||||||||||
| 26 | -id => 1, | ||||||||||
| 27 | -params => { type => HASHREF } | ||||||||||
| 28 | }); | ||||||||||
| 29 | |||||||||||
| 30 | 0 | my $params; | |||||||||
| 31 | my $response; | ||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | while (my ($key, $value) = each(%{$p->{'params'}})) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | |||||||||||
| 35 | 0 | $key =~ s/^-//; | |||||||||
| 36 | 0 | $params->{$key} = $value; | |||||||||
| 37 | |||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | my $packet = { | ||||||||||
| 41 | jsonrpc => RPC_JSON, | ||||||||||
| 42 | id => $p->{'id'}, | ||||||||||
| 43 | 0 | method => $p->{'method'}, | |||||||||
| 44 | params => $params | ||||||||||
| 45 | }; | ||||||||||
| 46 | |||||||||||
| 47 | 0 | $self->puts(encode($packet)); | |||||||||
| 48 | 0 | $response = $self->gets(); | |||||||||
| 49 | |||||||||||
| 50 | 0 | $response = decode($response); | |||||||||
| 51 | |||||||||||
| 52 | 0 | ▶ | if ($response->{id} eq $p->{'id'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | if ($response->{error}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | if ($response->{error}->{code} eq RPC_ERR_APP) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | my ($type, $info) = split(' - ', $response->{error}->{data}); | |||||||||
| 59 | |||||||||||
| 60 | 0 | $self->throw_msg( | |||||||||
| 61 | $type, | ||||||||||
| 62 | 'json_rpc_errorapp', | ||||||||||
| 63 | $info | ||||||||||
| 64 | ); | ||||||||||
| 65 | |||||||||||
| 66 | } else { | ||||||||||
| 67 | |||||||||||
| 68 | $self->throw_msg( | ||||||||||
| 69 | 'xas.lib.mixin.json.client', | ||||||||||
| 70 | 'json_rpc_error', | ||||||||||
| 71 | $response->{error}->{code}, | ||||||||||
| 72 | $response->{error}->{message}, | ||||||||||
| 73 | $response->{error}->{data} | ||||||||||
| 74 | 0 | ); | |||||||||
| 75 | |||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | } else { | ||||||||||
| 81 | |||||||||||
| 82 | 0 | $self->throw_msg( | |||||||||
| 83 | 'xas.lib.mixin.json.client', | ||||||||||
| 84 | 'rpc_invalid_id', | ||||||||||
| 85 | ); | ||||||||||
| 86 | |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | return $response->{result}; | |||||||||
| 90 | |||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | # ---------------------------------------------------------------------- | ||||||||||
| 94 | # Private Methods | ||||||||||
| 95 | # ---------------------------------------------------------------------- | ||||||||||
| 96 | |||||||||||
| 97 | 1 | 1; | |||||||||
| 98 | |||||||||||
| 99 | __END__ | ||||||||||
| 100 | |||||||||||
| 101 | =head1 NAME | ||||||||||
| 102 | |||||||||||
| 103 | XAS::Lib::Mixins::JSON::Client - A mixin for a JSON RPC interface | ||||||||||
| 104 | |||||||||||
| 105 | =head1 SYNOPSIS | ||||||||||
| 106 | |||||||||||
| 107 | package Client | ||||||||||
| 108 | |||||||||||
| 109 | use XAS::Class | ||||||||||
| 110 | debug => 0, | ||||||||||
| 111 | version => '0.01', | ||||||||||
| 112 | base => 'XAS::Lib::Net::Client', | ||||||||||
| 113 | mixin => 'XAS::Lib::Mixins::JSON::Client', | ||||||||||
| 114 | ; | ||||||||||
| 115 | |||||||||||
| 116 | package main | ||||||||||
| 117 | |||||||||||
| 118 | my $client = Client->new( | ||||||||||
| 119 | -port => 9505, | ||||||||||
| 120 | -host => 'localhost', | ||||||||||
| 121 | ); | ||||||||||
| 122 | |||||||||||
| 123 | $client->connect(); | ||||||||||
| 124 | |||||||||||
| 125 | my $data = $client->call( | ||||||||||
| 126 | -method => 'test' | ||||||||||
| 127 | -id => $id, | ||||||||||
| 128 | -params => {} | ||||||||||
| 129 | ); | ||||||||||
| 130 | |||||||||||
| 131 | $client->disconnect(); | ||||||||||
| 132 | |||||||||||
| 133 | =head1 DESCRIPTION | ||||||||||
| 134 | |||||||||||
| 135 | This modules implements a simple L<JSON RPC v2.0|http://www.jsonrpc.org/specification> client as a mixin. It | ||||||||||
| 136 | doesn't support "Notification" calls. | ||||||||||
| 137 | |||||||||||
| 138 | =head1 METHODS | ||||||||||
| 139 | |||||||||||
| 140 | =head2 call | ||||||||||
| 141 | |||||||||||
| 142 | This method is used to format the JSON packet and send it to the server. | ||||||||||
| 143 | Any errors returned from the server are parsed and then thrown. | ||||||||||
| 144 | |||||||||||
| 145 | =over 4 | ||||||||||
| 146 | |||||||||||
| 147 | =item B<-method> | ||||||||||
| 148 | |||||||||||
| 149 | The name of the RPC method to invoke. | ||||||||||
| 150 | |||||||||||
| 151 | =item B<-id> | ||||||||||
| 152 | |||||||||||
| 153 | The id used to identify this method call. | ||||||||||
| 154 | |||||||||||
| 155 | =item B<-params> | ||||||||||
| 156 | |||||||||||
| 157 | A hashref of the parameters to be passed to the method. | ||||||||||
| 158 | |||||||||||
| 159 | =back | ||||||||||
| 160 | |||||||||||
| 161 | =head1 SEE ALSO | ||||||||||
| 162 | |||||||||||
| 163 | =over 4 | ||||||||||
| 164 | |||||||||||
| 165 | =item L<XAS|XAS> | ||||||||||
| 166 | |||||||||||
| 167 | =back | ||||||||||
| 168 | |||||||||||
| 169 | =head1 AUTHOR | ||||||||||
| 170 | |||||||||||
| 171 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||||||||||
| 172 | |||||||||||
| 173 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 174 | |||||||||||
| 175 | Copyright (C) 2014 Kevin L. Esteb | ||||||||||
| 176 | |||||||||||
| 177 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 178 | the terms of the Artistic License 2.0. For details, see the full text | ||||||||||
| 179 | of the license at http://www.perlfoundation.org/artistic_license_2_0. | ||||||||||
| 180 | |||||||||||
| 181 | =cut | ||||||||||