| 1 | # Copyrights 2009-2013 by [Mark Overmeer]. | ||||||||||||||||||||
| 2 | # For other contributors see ChangeLog. | ||||||||||||||||||||
| 3 | # See the manual pages for details on the licensing terms. | ||||||||||||||||||||
| 4 | # Pod stripped from pm file by OODoc 2.01. | ||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package XML::Compile::RPC::Client; | ||||||||||||||||||||
| 9 | 4 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 4 | $VERSION = '0.17'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 4 | ▶ | use XML::Compile::RPC (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 4 | ▶ | use XML::Compile::RPC::Util qw/fault_code/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 4 | ▶ | use Log::Report 'xml-compile-rpc', syntax => 'LONG'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 4 | ▶ | use Time::HiRes qw/gettimeofday tv_interval/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 4 | ▶ | use HTTP::Request (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 4 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 3 | ▶ | sub new(@) { my $class = shift; (bless {}, $class)->init({@_}) } | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | sub init($) | ||||||||||||||||||||
| 25 | 3 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||
|
Subroutine
init: called CC=8 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||
| 26 | 3 | ▶ | $self->{user_agent} = $args->{user_agent} || LWP::UserAgent->new; | ||||||||||||||||||
|
Condition33%1/3
$args->{'user_agent'} || 'LWP::UserAgent'->new
MC/DC0%0/2
$args->{'user_agent'} || 'LWP::UserAgent'->new
$args->{'user_agent'}
'LWP::UserAgent'->new
Truth table33%1/3
A || B
A= $args->{'user_agent'}B= 'LWP::UserAgent'->new
| |||||||||||||||||||||
| 27 | 3 | ▶ | $self->{xmlformat} = $args->{xmlformat} || 0; | ||||||||||||||||||
|
Condition50%1/2
$args->{'xmlformat'} || 0
MC/DC0%0/1
$args->{'xmlformat'} || 0
$args->{'xmlformat'}
Truth table50%1/2
A
A= $args->{'xmlformat'}
| |||||||||||||||||||||
| 28 | 3 | $self->{auto_under} = $args->{autoload_underscore_is}; | |||||||||||||||||||
| 29 | $self->{destination} = $args->{destination} | ||||||||||||||||||||
| 30 | 3 | ▶ | or report ERROR => __x"client requires a destination parameter"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | # convert header template into header object | ||||||||||||||||||||
| 33 | 3 | my $headers = $args->{http_header}; | |||||||||||||||||||
| 34 | 3 | ▶ | $headers = HTTP::Headers->new(@{$headers || []}) | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 35 | unless UNIVERSAL::isa($headers, 'HTTP::Headers'); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # be sure we have a content-type | ||||||||||||||||||||
| 38 | 3 | ▶ | $headers->content_type | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | or $headers->content_type('text/xml'); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 3 | $self->{headers} = $headers; | |||||||||||||||||||
| 42 | 3 | ▶ | $self->{schemas} = $args->{schemas} ||= XML::Compile::RPC->new; | ||||||||||||||||||
|
Condition33%1/3
$args->{"schemas"} ||= "XML::Compile::RPC"->new
MC/DC0%0/2
$args->{"schemas"} ||= "XML::Compile::RPC"->new
$args->{"schemas"}
"XML::Compile::RPC"->new
Truth table33%1/3
A ||= B
A= $args->{"schemas"}B= "XML::Compile::RPC"->new
| |||||||||||||||||||||
| 43 | 3 | $self; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | sub headers() {shift->{headers}} | ||||||||||||||||||
|
Subroutine
headers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | sub schemas() {shift->{schemas}} | ||||||||||||||||||
|
Subroutine
schemas: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 4 | my %trace; | |||||||||||||||||||
| 54 | 0 | ▶ | sub trace() {\%trace} | ||||||||||||||||||
|
Subroutine
trace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | sub printTrace(;$) | ||||||||||||||||||||
| 58 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
printTrace: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 59 | 0 | ▶ | my $fh = shift || \*STDERR; | ||||||||||||||||||
|
Condition0%0/2
shift() || \*STDERR
MC/DC0%0/1
shift() || \*STDERR
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | $fh->print("response: ",$trace{response}->status_line, "\n"); | |||||||||||||||||||
| 62 | 0 | $fh->print("elapse: $trace{total_elapse}\n"); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub call($@) | ||||||||||||||||||||
| 67 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
call: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | my $start = [gettimeofday]; | |||||||||||||||||||
| 69 | 0 | my $request = $self->_request($self->_callmsg(@_)); | |||||||||||||||||||
| 70 | 0 | my $format = [gettimeofday]; | |||||||||||||||||||
| 71 | 0 | my $response = $self->{user_agent}->request($request); | |||||||||||||||||||
| 72 | 0 | my $network = [gettimeofday]; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | %trace = | |||||||||||||||||||
| 75 | ( request => $request | ||||||||||||||||||||
| 76 | , response => $response | ||||||||||||||||||||
| 77 | , start_time => ($start->[0] + $start->[1]*10e-6) | ||||||||||||||||||||
| 78 | , format_elapse => tv_interval($start, $format) | ||||||||||||||||||||
| 79 | , network_elapse => tv_interval($format, $network) | ||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | $response->is_success | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | or return ($response->code, $response->status_line); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | my ($rc, $decoded) = $self->_respmsg($response->decoded_content); | |||||||||||||||||||
| 86 | 0 | $trace{decode_elapse} = tv_interval $network; | |||||||||||||||||||
| 87 | 0 | $trace{total_elapse} = tv_interval $start; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 0 | ($rc, $decoded); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | sub _callmsg($@) | ||||||||||||||||||||
| 93 | 4 | ▶ | { my ($self, $method) = (shift, shift); | ||||||||||||||||||
|
Subroutine
_callmsg: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 4 | my @params; | |||||||||||||||||||
| 96 | 4 | ▶ | while(@_) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 4 | { my $type = shift; | |||||||||||||||||||
| 98 | 4 | ▶ | my $value = UNIVERSAL::isa($type, 'HASH') ? $type : {$type => shift}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | 4 | push @params, { value => $value }; | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 4 | my $doc = XML::LibXML::Document->new('1.0', 'UTF-8'); | |||||||||||||||||||
| 103 | 4 | my $xml = $self->{schemas}->writer('methodCall')->($doc | |||||||||||||||||||
| 104 | , { methodName => $method, params => { param => \@params }}); | ||||||||||||||||||||
| 105 | 4 | $doc->setDocumentElement($xml); | |||||||||||||||||||
| 106 | 4 | $doc; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | sub _request($) | ||||||||||||||||||||
| 110 | 0 | ▶ | { my ($self, $doc) = @_; | ||||||||||||||||||
|
Subroutine
_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 111 | HTTP::Request->new | ||||||||||||||||||||
| 112 | ( POST => $self->{destination} | ||||||||||||||||||||
| 113 | , $self->{headers} | ||||||||||||||||||||
| 114 | , $doc->toString($self->{xmlformat}) | ||||||||||||||||||||
| 115 | 0 | ); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | sub _respmsg($) | ||||||||||||||||||||
| 119 | 5 | ▶ | { my ($self, $xml) = @_; | ||||||||||||||||||
|
Subroutine
_respmsg: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 120 | 5 | ▶ | length $xml or return (1, "no xml received"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 5 | my $data = $self->{schemas}->reader('methodResponse')->($xml); | |||||||||||||||||||
| 122 | return fault_code $data->{fault} | ||||||||||||||||||||
| 123 | 5 | ▶ | if $data->{fault}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 4 | my ($type, $value) = %{$data->{params}{param}{value}}; | |||||||||||||||||||
| 126 | 4 | (0, $value); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | sub AUTOLOAD | ||||||||||||||||||||
| 130 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 131 | 0 | (my $proc = our $AUTOLOAD) =~ s/.*\:\://; | |||||||||||||||||||
| 132 | $proc =~ s/_/$self->{auto_under}/g | ||||||||||||||||||||
| 133 | 0 | ▶ | if defined $self->{auto_under}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | $self->call($proc, @_); | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | ▶ | sub DESTROY {} # avoid DESTROY to AUTOLOAD | |||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 4 | 1; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||