CAB/Client/XmlRpc.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 209 CC 25 SCAR 31.5
CC
25
Cov
0%
request
72.0
connected
20.0
analyzeToken
20.0
CRAP 23.3
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Client::XmlRpc.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DTA::CAB XML-RPC server clients
6  
7 package DTA::CAB::Client::XmlRpc;
8 use DTA::CAB;
9 use DTA::CAB::Client;
10 use RPC::XML;
11 use RPC::XML::Client;
12 use DTA::CAB::Datum ':all';
13 use DTA::CAB::Utils ':all';
14 use Carp;
15 use strict;
16  
17 ##==============================================================================
18 ## Globals
19 ##==============================================================================
20  
21 our @ISA = qw(DTA::CAB::Client);
22  
23 ##==============================================================================
24 ## Constructors etc.
25 ##==============================================================================
26  
27 ## $obj = CLASS_OR_OBJ->new(%args)
28 ## + object structure: HASH ref
29 ## {
30 ## ##-- server
31 ## serverURL => $url, ##-- default: localhost:8000
32 ## serverEncoding => $encoding, ##-- default: UTF-8
33 ## timeout => $timeout, ##-- timeout in seconds, default: 300 (5 minutes)
34 ##
35 ## ##-- debugging
36 ## tracefh => $fh, ##-- dump requests to $fh if defined (default=undef)
37 ## testConnect => $bool, ##-- if true connected() will send a test query (default=true)
38 ##
39 ## ##-- underlying RPC::XML client
40 ## xcli => $xcli, ##-- RPC::XML::Client object
41 ## }
42 sub new {
43 my $that = shift;
44 return $that->SUPER::new(
45 ##-- server
46 serverURL => 'http://localhost:8000',
47 serverEncoding => 'UTF-8', ##-- default server encoding
48 timeout => 300,
49 testConnect => 1,
50 ##
51 ##-- RPC::XML stuff
52 xcli => undef,
53 xargs => {
54 compress_requests => 0, ##-- send compressed requests?
55 compress_thresh => 8192, ##-- byte limit for compressed requests
56 ##
57 message_file_thresh => 0, ##-- disable file-based message spooling
58 },
59 ##
60 ##-- user args
61 @_,
62 );
63 }
64  
65 ##==============================================================================
66 ## Methods: Generic Client API: Connections
67 ##==============================================================================
68  
69 ## $bool = $cli->connected()
70 sub connected {
71 my $cli = shift;
72 return 0 if (!$cli->{xcli});
73 return 1 if (!$cli->{testConnect});
74  
75 ##-- send a test query (system.identity())
76 my $req = $cli->newRequest('system.identity');
77 my $rsp = $cli->{xcli}->send_request( $req );
78 return ref($rsp) ? 1 : 0;
79 }
80  
81 ## $bool = $cli->connect()
82 ## + establish connection
83 sub connect {
84 my $cli = shift;
85 $cli->{xcli} = RPC::XML::Client->new($cli->{serverURL}, %{$cli->{xargs}})
86 or $cli->logdie("could not create underlying RPC::XML::Client: $!");
87 $cli->{xcli}->message_file_thresh(0)
88 if (defined($cli->{xargs}{message_file_thresh}) && !$cli->{xargs}{message_file_thresh});
89 $cli->{xcli}->useragent->timeout($cli->{timeout})
90 if (defined($cli->{timeout}));
91 return $cli->connected();
92 }
93  
94 ## $bool = $cli->disconnect()
95 sub disconnect {
96 my $cli = shift;
97 delete($cli->{xcli});
98 return 1;
99 }
100  
101 ## @analyzers = $cli->analyzers()
102 sub analyzers {
103 my $rsp = $_[0]->request( RPC::XML::request->new('dta.cab.listAnalyzers') );
104 return ref($rsp) && !$rsp->is_fault ? sort(@{ $rsp->value }) : $rsp;
105 }
106  
107 ##==============================================================================
108 ## Methods: Utils
109 ##==============================================================================
110  
111 ## $rsp_or_error = $cli->request($req)
112 ## $rsp_or_error = $cli->request($req, $doDeepEncoding=1)
113 ## + send XML-RPC request, log if error occurs
114 sub request {
115 my ($cli,$req,$doRecode) = @_;
116  
117 ##-- cache RPC::XML encoding
118 $doRecode = 1 if (!defined($doRecode));
119 my $enc_tmp = $RPC::XML::ENCODING;
120 $RPC::XML::ENCODING = $cli->{serverEncoding};
121  
122 $cli->connect() if (!$cli->{xcli});
123 $req = DTA::CAB::Utils::deep_encode($cli->{serverEncoding}, $req) if ($doRecode);
124 $cli->{tracefh}->print($req->as_string) if (defined($cli->{tracefh})); ##-- trace
125  
126 my $rsp = $cli->{xcli}->send_request( $req );
127 if (!ref($rsp)) {
128 $cli->error("RPC::XML::Client::send_request() failed:");
129 $cli->error($rsp);
130 }
131 elsif ($rsp->is_fault) {
132 $cli->error("XML-RPC fault (".$rsp->code.") ".$rsp->string);
133 }
134  
135 ##-- cleanup & return
136 $RPC::XML::ENCODING = $enc_tmp;
137 return $doRecode ? DTA::CAB::Utils::deep_decode($cli->{serverEncoding}, $rsp) : $rsp;
138 }
139  
140  
141 ##==============================================================================
142 ## Methods: Generic Client API: Queries: v0.x
143 ##==============================================================================
144  
145 ## $req = $cli->newRequest($methodName, @args)
146 ## + returns new RPC::XML::request
147 ## + encodes all elementary data types as strings
148 sub newRequest {
149 my ($cli,$method,@args) = @_;
150 my $str_tmp = $RPC::XML::FORCE_STRING_ENCODING;
151 $RPC::XML::FORCE_STRING_ENCODING = 1;
152 my $req = RPC::XML::request->new($method,@args);
153 $RPC::XML::FORCE_STRING_ENCODING = $str_tmp;
154 return $req;
155 }
156  
157 ## $tok = $cli->analyzeToken($analyzer, $tok, \%opts)
158 sub analyzeToken {
159 my ($cli,$aname,$tok,$opts) = @_;
160 my $suffix = $opts && $opts->{methodSuffix} ? $opts->{methodSuffix} : ''; ##-- e.g. methodSuffix=>1" for v1.x interface
161 my $rsp = $cli->request($cli->newRequest("$aname.analyzeToken${suffix}",
162 $tok,
163 (defined($opts) ? $opts : qw())
164 ));
165 return ref($rsp) && !$rsp->is_fault ? toToken($rsp->value) : $rsp;
166 }
167  
168 ## $sent = $cli->analyzeSentence($analyzer, $sent, \%opts)
169 sub analyzeSentence {
170 my ($cli,$aname,$sent,$opts) = @_;
171 my $suffix = $opts && $opts->{methodSuffix} ? $opts->{methodSuffix} : ''; ##-- e.g. methodSuffix=>1" for v1.x interface
172 my $rsp = $cli->request($cli->newRequest("$aname.analyzeSentence${suffix}",
173 $sent,
174 (defined($opts) ? $opts : qw())
175 ));
176 return ref($rsp) && !$rsp->is_fault ? toSentence($rsp->value) : $rsp;
177 }
178  
179 ## $doc = $cli->analyzeDocument($analyzer, $doc, \%opts)
180 sub analyzeDocument {
181 my ($cli,$aname,$doc,$opts) = @_;
182 my $suffix = $opts && $opts->{methodSuffix} ? $opts->{methodSuffix} : ''; ##-- e.g. methodSuffix=>1" for v1.x interface
183 my $rsp = $cli->request($cli->newRequest("$aname.analyzeDocument${suffix}",
184 $doc,
185 (defined($opts) ? $opts : qw())
186 ));
187 return ref($rsp) && !$rsp->is_fault ? toDocument($rsp->value) : $rsp;
188 }
189  
190 ## $data_str = $cli->analyzeData($analyzer, $input_str, \%opts)
191 sub analyzeData {
192 my ($cli,$aname,$data,$opts) = @_;
193 my $rsp = $cli->request($cli->newRequest("$aname.analyzeData",
194 RPC::XML::base64->new($data),
195 (defined($opts) ? $opts : qw())
196 ),
197 0 ##-- no deep encode/decode
198 );
199 return ref($rsp) && !$rsp->is_fault ? $rsp->value : $rsp;
200 }
201  
202 1; ##-- be happy
203  
204 __END__