Client.pm

filter: Statementstmt 26.5% 34 / 128 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 21.2% 7 / 33 Podpod 90.4% 19 / 21 totaltotal 25.4% 60 / 236 CC 21 SCAR 21.7
CC
21
Cov
20%
openerp_login
24.5
search_detail
20.0
read_single
20.0
CRAP 8.8
↑ Summary Simple.pm »
1 package OpenERP::XMLRPC::Client;
2 # ABSTRACT: XMLRPC Client tweaked for OpenERP interaction.
3  
4 3 our $VERSION = '0.23';
5  
6 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use failures qw/openerp::fault/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 3 has 'username' => ( is => 'ro', isa => 'Str', default => 'admin');
13 3 has 'password' => ( is => 'ro', isa => 'Str', default => 'admin');
14 3 has 'dbname' => ( is => 'ro', isa => 'Str', default => 'terp');
15 3 has 'host' => ( is => 'ro', isa => 'Str', default => '127.0.0.1');
16 3 has 'port' => ( is => 'ro', isa => 'Int', default => 8069);
17 3 has 'proto' => ( is => 'ro', isa => 'Str', default => 'http');
18 3 has use_failures => (is => 'ro', isa => 'Bool', default => 0);
19  
20 3 has '_report_report_uri' => ( is => 'ro', isa => 'Str', default => 'xmlrpc/report' );
21 3 has '_object_execute_uri' => ( is => 'ro', isa => 'Str', default => 'xmlrpc/object' );
22 3 has '_object_execute_kw_uri' => ( is => 'ro', isa => 'Str', default => 'xmlrpc/object' );
23 3 has '_object_exec_workflow_uri' => ( is => 'ro', isa => 'Str', default => 'xmlrpc/object' );
24  
25 3 has 'openerp_uid' => ( is => 'rw', isa => 'Int' );
26 3 has 'base_rpc_uri' => ( is => 'rw', isa => 'Str', default => 'xmlrpc/common');
27  
28  
29 3 with 'MooseX::Role::XMLRPC::Client' =>
30 {
31 name => 'openerp',
32 login_info => 1,
33 };
34  
35 0 ▶sub _build_openerp_userid { shift->username }
Subroutine _build_openerp_userid: not called CC=1 SCAR=6.9
36 0 ▶sub _build_openerp_passwd { shift->password }
Subroutine _build_openerp_passwd: not called CC=1 SCAR=6.9
37 sub _build_openerp_uri
38 {
39 2 ▶ my $self = shift;
Subroutine _build_openerp_uri: called CC=1 SCAR=0.0
40 2 return $self->proto . '://' . $self->host . ':' . $self->port . '/' . $self->base_rpc_uri;
41 }
42  
43 sub openerp_login
44 {
45 2 ▶ my $self = shift;
Subroutine openerp_login: called CC=5 SCAR=32.0
Pod documented
46  
47 # call 'login' method to get the uid..
48 2 my $res = $self->openerp_rpc->send_request('login', $self->dbname, $self->username, \$self->password );
49  
50 0 ▶ if ( ! defined $res || ! ref $res )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res and ref $res) 0 0 0
Condition0%0/3
defined $res and ref $res
!ll&&!rl&&r
000
MC/DC0%0/2
defined $res and ref $res
defined $res ref $res
Truth table0%0/3
A and B
A= defined $resB= ref $res
ABresult
0X0
100
111
51 {
52 0 die "Failed to log into OpenERP XML RPC service";
53 }
54 0 ▶ die "Incorrect username or password" if $$res == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $$res == 0 0 0 0
55  
56  
57 # set the uid we have just had returned from logging in..
58 0 $self->openerp_uid( ${ $res } );
59 # NOTE: OpenERP seems to be filling in faultCode not faultString these days
60 # (6.1.1) so we need to check for that and display it instead.
61 0 ▶ if($self->use_failures)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_failures) { } 0 0 0
62 {
63 $self->openerp_rpc->fault_handler(sub {
64 failure::openerp::fault->throw({
65 0 ▶ msg => $_[0]->{faultCode} ? $_[0]->{faultCode}->value : $_[0]->string,
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'faultCode'} ? : 0 0 0
66 payload => { original_exception => $_[0]}
67 });
68 0 });
69 }
70 else
71 {
72 $self->openerp_rpc->fault_handler(sub {
73 0 ▶ confess $_[0]->{faultCode} ? $_[0]->{faultCode}->value : $_[0]->string
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'faultCode'} ? : 0 0 0
74 0 });
75 }
76 }
77  
78 sub openerp_logout
79 {
80 0 ▶ my $self = shift;
Subroutine openerp_logout: not called CC=1 SCAR=6.9
Pod documented
81 # do nothing on logout...nothing is required..
82 }
83  
84  
85  
86 sub BUILD
87 {
88 2 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
89 2 $RPC::XML::ENCODING = 'utf-8';
90 2 $self->openerp_login;
91 }
92  
93  
94 sub change_uri
95 {
96 0 ▶ my $self = shift;
Subroutine change_uri: not called CC=2 SCAR=17.9
Pod documented
97 0 my $base_uri = shift;
98  
99 0 my $exsting_base_uri = $self->base_rpc_uri;
100  
101 0 ▶ return $exsting_base_uri if $base_uri eq $exsting_base_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $base_uri eq $exsting_base_uri 0 0 0
102  
103 0 $self->base_rpc_uri( $base_uri ); # change the base path.
104 0 $self->openerp_rpc->uri( $self->_build_openerp_uri ); # rebuild and set the new uri.
105 0 return $exsting_base_uri; # return the old uri.
106 }
107  
108 sub object_execute
109 {
110 0 ▶ my $self = shift;
Subroutine object_execute: not called CC=1 SCAR=6.9
Pod documented
111  
112 0 my $method = shift; # eg. 'search'
113 0 my $relation = shift; # eg. 'res.partner'
114 0 my @args = @_; # All other args we just pass on.
115  
116 # change the uri to base uri we are going to query..
117 0 $self->change_uri( $self->_object_execute_uri );
118  
119 0 $self->simple_request
120 (
121 'execute',
122 $self->dbname,
123 $self->openerp_uid,
124 \$self->password,
125 $relation,
126 $method,
127 @args
128 );
129  
130 }
131  
132 sub object_execute_kw
133 {
134 0 ▶ my $self = shift;
Subroutine object_execute_kw: not called CC=1 SCAR=6.9
Pod undocumented
135  
136 0 my $method = shift; # eg. 'search'
137 0 my $relation = shift; # eg. 'res.partner'
138 0 my @args = @_; # All other args we just pass on.
139  
140 # change the uri to base uri we are going to query..
141 0 $self->change_uri( $self->_object_execute_kw_uri );
142  
143 0 $self->simple_request
144 (
145 'execute_kw',
146 $self->dbname,
147 $self->openerp_uid,
148 \$self->password,
149 $relation,
150 $method,
151 @args,
152 );
153  
154 }
155  
156 sub object_exec_workflow
157 {
158 0 ▶ my $self = shift;
Subroutine object_exec_workflow: not called CC=1 SCAR=6.9
Pod documented
159  
160 0 my $method = shift; # eg. 'search'
161 0 my $relation = shift; # eg. 'res.partner'
162 0 my @args = @_; # All other args we just pass on.
163  
164 # change the uri to base uri we are going to query..
165 0 $self->change_uri( $self->_object_exec_workflow_uri );
166  
167 0 $self->simple_request
168 (
169 'exec_workflow',
170 $self->dbname,
171 $self->openerp_uid,
172 \$self->password,
173 $relation,
174 $method,
175 @args
176 );
177  
178 }
179  
180 sub report_report
181 {
182 0 ▶ my $self = shift;
Subroutine report_report: not called CC=1 SCAR=6.9
Pod documented
183  
184 0 my $report_id = shift; # eg. 'purchase.quotation'
185 0 my $object_id = shift;
186 0 my $parameters = shift; # eg. model, id and report_type
187  
188 # change the uri to base uri we are going to query..
189 0 $self->change_uri( $self->_report_report_uri );
190  
191 0 return $self->simple_request
192 (
193 'report',
194 $self->dbname,
195 $self->openerp_uid,
196 \$self->password,
197 $report_id,
198 [$object_id],
199 $parameters,
200 @_
201 );
202 }
203  
204 sub report_report_get
205 {
206 0 ▶ my $self = shift;
Subroutine report_report_get: not called CC=2 SCAR=17.9
Pod documented
207  
208 0 my $report_id = shift; # eg. 123
209  
210 # change the uri to base uri we are going to query..
211 0 $self->change_uri( $self->_report_report_uri );
212  
213 0 my $object = $self->simple_request
214 (
215 'report_get',
216 $self->dbname,
217 $self->openerp_uid,
218 \$self->password,
219 $report_id,
220 );
221  
222 0 ▶ if($object->{state})
Branch: 1 branch
BranchTrueFalseTotal
if ($object->{"state"}) 0 0 0
223 {
224 0 my $data = $object->{result};
225 0 return decode_base64($data);
226 }
227  
228 0 return;
229 }
230  
231 sub simple_request
232 {
233 0 ▶ my $self = shift;
Subroutine simple_request: not called CC=1 SCAR=6.9
Pod undocumented
234  
235 local *RPC::XML::boolean::value = sub {
236 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
237 # this fudges the false so it's not 0
238 # which means if it was used to indicate null is probably going to work better.
239 # the downside is that we presumably lose some precision when it comes to bools
240 # and nulls.
241 0 ▶ return undef unless ${$self};
Branch: 1 branch
BranchTrueFalseTotal
unless ${$self;} 0 0 0
242 0 return 1;
243 0 };
244  
245 0 return $self->openerp_rpc->simple_request(@_);
246 }
247  
248 sub create
249 {
250 0 ▶ return shift->_three_arg_execute('create', @_);
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
251 }
252  
253 sub read
254 {
255 0 ▶ my ($self, $object, $ids, $context, $fields) = @_;
Subroutine read: not called CC=3 SCAR=24.8
Pod documented
256
257 0 ▶ $ids = [ $ids ] unless ( ref $ids eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ids eq "ARRAY" 0 0 0
258
259 0 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 0 0 0
260 0 return $self->object_execute('read', $object, $ids, $fields, $context);
261 } else {
262 0 return $self->object_execute('read', $object, $ids);
263 }
264 }
265  
266 sub search
267 {
268 0 ▶ my ($self, $object, $args, $context, $offset, $limit, $order) = @_;
Subroutine search: not called CC=3 SCAR=24.8
Pod documented
269
270 0 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) { } 0 0 0
271 0 ▶ return $self->object_execute('search', $object, $args, $offset // 0, $limit, $order, $context);
Condition0%0/2
$offset // 0
l!l
00
MC/DC0%0/1
$offset // 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
272 } else {
273 0 return $self->object_execute('search', $object, $args);
274 }
275 }
276  
277 sub field_info
278 {
279 0 ▶ return shift->_three_arg_execute('fields_view_get', @_);
Subroutine field_info: not called CC=1 SCAR=6.9
Pod documented
280 }
281  
282 sub model_fields
283 {
284 0 ▶ return shift->_three_arg_execute('fields_get', @_);
Subroutine model_fields: not called CC=1 SCAR=6.9
Pod documented
285 }
286  
287 sub update
288 {
289 0 ▶ return shift->_array_execute('write', @_);
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
290 }
291  
292 sub get_defaults
293 {
294 0 ▶ return shift->_array_execute('default_get', @_);
Subroutine get_defaults: not called CC=1 SCAR=6.9
Pod documented
295 }
296  
297 sub delete
298 {
299 0 ▶ return shift->_array_execute('unlink', @_);
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
300 }
301  
302 sub copy
303 {
304 0 ▶ return shift->_three_arg_execute('copy', @_);
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
305 }
306  
307 sub _three_arg_execute
308 {
309 0 ▶ my $self = shift;
Subroutine _three_arg_execute: not called CC=1 SCAR=6.9
310 0 my $verb = shift;
311 0 my $object = shift;
312 0 my $args = shift;
313 0 return $self->object_execute($verb, $object, $args, @_ );
314 }
315  
316 sub _array_execute
317 {
318 0 ▶ my $self = shift;
Subroutine _array_execute: not called CC=2 SCAR=17.9
319 0 my $verb = shift;
320 0 my $object = shift;
321 0 my $ids = shift;
322 0 my $args = shift;
323  
324 # ensure we pass an array of IDs to the RPC..
325 0 ▶ $ids = [ $ids ] unless ( ref $ids eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ids eq "ARRAY" 0 0 0
326  
327 0 return $self->object_execute($verb, $object, $ids, $args, @_ );
328 }
329  
330 sub search_detail
331 {
332 0 ▶ my ($self, $object, $args, $context, $offset, $limit) = @_;
Subroutine search_detail: not called CC=4 SCAR=30.0
Pod documented
333  
334 # search and get ids..
335 0 my $ids = $self->search( $object, $args, $context, $offset, $limit );
336 0 ▶ return unless ( defined $ids && ref $ids eq 'ARRAY' && scalar @$ids >= 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ids and ref $ids eq "ARRAY" and scalar @$ids >= 1 0 0 0
Condition0%0/6
defined $ids and ref $ids eq "ARRAY" and scalar @$ids >= 1
!ll&&!rl&&r
000
defined $ids and ref $ids eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ids and ref $ids eq "ARRAY" and scalar @$ids >= 1
defined $ids ref $ids eq "ARRAY" scalar @$ids >= 1
Truth table0%0/4
A and B and C
A= defined $idsB= ref $ids eq "ARRAY"C= scalar @$ids >= 1
ABCresult
0XX0
10X0
1100
1111
337  
338 # read data from all the ids..
339 # FIXME: I'm fairly sure context is in the wrong place.
340 0 return $self->read( $object, $ids, $context );
341 }
342  
343 sub read_single
344 {
345 0 ▶ my $res = shift->read( @_ );
Subroutine read_single: not called CC=4 SCAR=30.0
Pod documented
346 0 ▶ return unless ( defined $res && ref $res eq 'ARRAY' && scalar @$res >= 1 );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res and ref $res eq "ARRAY" and scalar @$res >= 1 0 0 0
Condition0%0/6
defined $res and ref $res eq "ARRAY" and scalar @$res >= 1
!ll&&!rl&&r
000
defined $res and ref $res eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $res and ref $res eq "ARRAY" and scalar @$res >= 1
defined $res ref $res eq "ARRAY" scalar @$res >= 1
Truth table0%0/4
A and B and C
A= defined $resB= ref $res eq "ARRAY"C= scalar @$res >= 1
ABCresult
0XX0
10X0
1100
1111
347 0 return $res->[0];
348 }
349  
350  
351  
352 3 1;
353  
354 __END__
355  
356 =pod
357  
358 =encoding UTF-8
359  
360 =head1 NAME
361  
362 OpenERP::XMLRPC::Client - XMLRPC Client tweaked for OpenERP interaction.
363  
364 =head1 VERSION
365  
366 version 0.23
367  
368 =head1 SYNOPSIS
369  
370 my $erp = OpenERP::XMLRPC::Client->new( dbname => 'terp', username => 'admin', password => 'admin', host => '127.0.0.1', port => '8069' )
371 my $partner_ids = $erp->object_execute( 'res.partner', 'search', [ 'name', 'ilke', 'abc' ] );
372  
373 # READ a res.partner object
374 my $partner = $erp->read( 'res.partner', $id );
375  
376 print "You Found Partner:" . $partner->{name} . "\n";
377  
378 =head1 DESCRIPTION
379  
380 I have tried to make this extendable so made use of moose roles to structure the calls to the
381 different methods available from the openerp rpc.
382  
383 This makes use of the L<MooseX::Role::XMLRPC::Client> to communicate via rpc.
384  
385 This module was built to be used by another L<OpenERP::XMLRPC::Simple> and handles
386 openerp specific rpc interactions. It could be used by something else to access
387 openerp rpc services.
388  
389 =head1 NAME
390  
391 OpenERP::XMLRPC::Client - XMLRPC Client tweaked for OpenERP interaction.
392  
393 =head1 NAME
394  
395 OpenERP::XMLRPC::Client - XML RPC Client for OpenERP
396  
397 =head1 Parameters
398  
399 username - string - openerp username (default: 'admin')
400 password - string - openerp password (default: 'admin')
401 dbname - string - openerp database name (default: 'terp')
402 host - string - openerp rpc server host (default: '127.0.0.1' )
403 port - string - openerp rpc server port (default: 8069)
404 proto - string - openerp protocol (default: http) .. untested anything else.
405  
406 =head1 Attributes
407  
408 openerp_uid - int - filled when the connection is logged in.
409 base_rpc_uri - string - used to hold uri the rpc is currently pointing to.
410 openerp_rpc - L<RPC::XML::Client> - Provided by L<MooseX::Role::XMLRPC::Client>
411  
412 =head1 METHODS
413  
414 These methods re-present the OpenERP XML RPC but in a slightly more user friendly way.
415  
416 The methods have been tested using the 'res.partner' object name and the demo database
417 provided when you install OpenERP.
418  
419 =head2 BUILD
420  
421 When the object is instanciated, this method is run. This calls openerp_login.
422  
423 =head2 openerp_login
424  
425 Logs the client in. Called automatically when the object is created.
426  
427 =head2 openerp_logout
428  
429 Basically a no-op.
430  
431 =head2 object_execute
432  
433 Low level method for making a call to the Open ERP server. Normally called by a
434 wrapper function like L<create> or L<read>.
435  
436 =head2 object_exec_workflow
437  
438 Makes an 'exec_workflow' call to Open ERP.
439  
440 =head2 report_report
441  
442 Sends a 'report' call to Open ERP.
443  
444 =head2 report_report_get
445  
446 Sends a 'report_get' call to Open ERP.
447  
448 =head2 change_uri
449  
450 OpenERP makes methods available via different URI's, this method is used to change which
451 URI the rpc client is pointing at.
452  
453 Arguments:
454 $_[0] - object ref. ($self)
455 $_[1] - string (e.g. "xmlrpc/object") base uri path.
456  
457 Returns:
458 string - the old uri - the one this new one replaced.
459  
460 =head2 read ( OBJECTNAME, [IDS] )
461  
462 Can pass this a sinlge ID or an ARRAYREF of ID's, it will return an ARRAYREF of
463 OBJECT records (HASHREF's).
464  
465 Example:
466 $partner = $erp->read('res.partner', 1 );
467 print "This is the returned record name:" . $partner->[0]->{name} . "\n";
468  
469 $partners = $erp->read('res.partner', [1,2] );
470 print "This is the returned record 1:" . $partners->[0]->{name} . "\n";
471 print "This is the returned record 2:" . $partners->[1]->{name} . "\n";
472  
473 Returns: ArrayRef of HashRef's - All the objects with IDs passed.
474  
475 =head2 search ( OBJECTNAME, [ [ COLNAME, COMPARATOR, VALUE ] ] )
476  
477 Used to search and return IDs of objects matching the searcgh.
478  
479 Returns: ArrayRef of ID's - All the objects ID's matching the search.
480  
481 Example:
482 $results = $erp->search('res.partner', [ [ 'name', 'ilke', 'abc' ] ] );
483 print "This is the 1st ID found:" . $results->[0] . "\n";
484  
485 =head2 copy ( model, id )
486  
487 Copies the object specified, returning the id of the new object.
488  
489 =head2 create ( OBJECTNAME, { COLNAME => COLVALUE } )
490  
491 Returns: ID - the ID of the object created.
492  
493 Example:
494 $new_id = $erp->create('res.partner', { 'name' => 'new company name' } );
495  
496 =head2 update ( OBJECTNAME, ID, { COLNAME => COLVALUE } )
497  
498 Returns: boolean - updated or not.
499  
500 Example:
501 $success = $erp->update('res.partner', 1, { 'name' => 'changed company name' } );
502  
503 =head2 delete ( OBJECTNAME, ID )
504  
505 Returns: boolean - deleted or not.
506  
507 Example:
508 $success = $erp->delete('res.partner', 1 );
509  
510 =head2 field_info ( OBJECTNAME )
511  
512 Returns: hash containing all field info, this contains field names and field types.
513  
514 =head2 model_fields ( OBJECTNAME )
515  
516 Returns: hash containing all the models fields.
517  
518 =head2 get_defaults ( OBJECTNAME, [ FIELDS ] )
519  
520 Returns: hash containing the default values for those fields.
521  
522 =head2 search_detail ( OBJECTNAME, [ [ COLNAME, COMPARATOR, VALUE ] ], CONTEXT )
523  
524 Used to search and read details on a perticular OBJECT. This uses 'search' to find IDs,
525 then calls 'read' to get details on each ID returned.
526  
527 Returns: ArrayRef of HashRef's - All the objects found with all their details.
528  
529 Example:
530 $results = $erp->search_detail('res.partner', [ [ 'name', 'ilke', 'abc' ] ] );
531 print "This is the 1st found record name:" . $results->[0]->{name} . "\n";
532  
533 The C<CONTEXT> argument is optional. This allows a hasref containing the current
534 search context to be provided, e.g.
535  
536 my $results = $erp->search_detail(
537 'stock.location',
538 [
539 ['usage' => '=' => 'internal']
540 ],
541 {
542 active_id => $self->id,
543 active_ids => [$self->id],
544 active_model => 'product.product',
545 full => 1,
546 product_id => $self->id,
547 search_default_in_location => 1,
548 section_id => undef,
549 tz => undef,
550 }
551 )
552  
553 =head2 read_single ( OBJECTNAME, ID )
554  
555 Pass this a sinlge ID and get a single OBJECT record (HASHREF).
556  
557 Example:
558 $partner = $erp->read_single('res.partner', 1 );
559 print "This name of partner with ID 1:" . $partner->{name} . "\n";
560  
561 Returns: HashRef - The objects data
562  
563 =head1 SEE ALSO
564  
565 L<RPC::XML::Client>
566  
567 =head1 AUTHORS
568  
569 =over 4
570  
571 =item *
572  
573 Benjamin Martin <ben@madeofpaper.co.uk>
574  
575 =item *
576  
577 Colin Newell <colin@opusvl.com>
578  
579 =item *
580  
581 Jon Allen (JJ) <jj@opusvl.com>
582  
583 =back
584  
585 =head1 COPYRIGHT AND LICENSE
586  
587 This software is copyright (c) 2016 by OpusVL.
588  
589 This is free software; you can redistribute it and/or modify it under
590 the same terms as the Perl 5 programming language system itself.
591  
592 =cut
↑ Summary Simple.pm »