Client.pm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #
  2. # Copyright (C) 1998 Ken MacLeod
  3. # Frontier::Client is free software; you can redistribute it
  4. # and/or modify it under the same terms as Perl itself.
  5. #
  6. # $Id: Client.pm,v 1.8 2001/10/03 01:30:54 kmacleod Exp $
  7. #
  8. # NOTE: see Net::pRPC for a Perl RPC implementation
  9. use strict;
  10. package Frontier::Client;
  11. use Frontier::RPC2;
  12. use LWP::UserAgent;
  13. use HTTP::Request;
  14. use vars qw{$AUTOLOAD};
  15. sub new {
  16. my $class = shift;
  17. my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
  18. bless $self, $class;
  19. die "Frontier::RPC::new: no url defined\n"
  20. if !defined $self->{'url'};
  21. $self->{'ua'} = LWP::UserAgent->new;
  22. $self->{'ua'}->proxy('http', $self->{'proxy'})
  23. if(defined $self->{'proxy'});
  24. $self->{'rq'} = HTTP::Request->new (POST => $self->{'url'});
  25. $self->{'rq'}->header('Content-Type' => 'text/xml');
  26. my @options;
  27. if(defined $self->{'encoding'}) {
  28. push @options, 'encoding' => $self->{'encoding'};
  29. }
  30. if (defined $self->{'use_objects'} && $self->{'use_objects'}) {
  31. push @options, 'use_objects' => $self->{'use_objects'};
  32. }
  33. $self->{'enc'} = Frontier::RPC2->new(@options);
  34. return $self;
  35. }
  36. sub call {
  37. my $self = shift;
  38. my $text = $self->{'enc'}->encode_call(@_);
  39. if ($self->{'debug'}) {
  40. print "---- request ----\n";
  41. print $text;
  42. }
  43. $self->{'rq'}->content($text);
  44. my $response = $self->{'ua'}->request($self->{'rq'});
  45. if (!$response->is_success) {
  46. die $response->status_line . "\n";
  47. }
  48. my $content = $response->content;
  49. if ($self->{'debug'}) {
  50. print "---- response ----\n";
  51. print $content;
  52. }
  53. my $result = $self->{'enc'}->decode($content);
  54. if ($result->{'type'} eq 'fault') {
  55. die "Fault returned from XML RPC Server, fault code " . $result->{'value'}[0]{'faultCode'} . ": "
  56. . $result->{'value'}[0]{'faultString'} . "\n";
  57. }
  58. return $result->{'value'}[0];
  59. }
  60. # shortcuts
  61. sub base64 {
  62. my $self = shift;
  63. return Frontier::RPC2::Base64->new(@_);
  64. }
  65. sub boolean {
  66. my $self = shift;
  67. return Frontier::RPC2::Boolean->new(@_);
  68. }
  69. sub double {
  70. my $self = shift;
  71. return Frontier::RPC2::Double->new(@_);
  72. }
  73. sub int {
  74. my $self = shift;
  75. return Frontier::RPC2::Integer->new(@_);
  76. }
  77. sub string {
  78. my $self = shift;
  79. return Frontier::RPC2::String->new(@_);
  80. }
  81. sub date_time {
  82. my $self = shift;
  83. return Frontier::RPC2::DateTime::ISO8601->new(@_);
  84. }
  85. # something like this could be used to get an effect of
  86. #
  87. # $server->examples_getStateName(41)
  88. #
  89. # instead of
  90. #
  91. # $server->call('examples.getStateName', 41)
  92. #
  93. # for Frontier's
  94. #
  95. # [server].examples.getStateName 41
  96. #
  97. # sub AUTOLOAD {
  98. # my ($pkg, $method) = ($AUTOLOAD =~ m/^(.*::)(.*)$/);
  99. # return if $method eq 'DESTROY';
  100. #
  101. # $method =~ s/__/=/g;
  102. # $method =~ tr/_=/._/;
  103. #
  104. # splice(@_, 1, 0, $method);
  105. #
  106. # goto &call;
  107. # }
  108. =head1 NAME
  109. Frontier::Client - issue Frontier XML RPC requests to a server
  110. =head1 SYNOPSIS
  111. use Frontier::Client;
  112. $server = Frontier::Client->new( I<OPTIONS> );
  113. $result = $server->call($method, @args);
  114. $boolean = $server->boolean($value);
  115. $date_time = $server->date_time($value);
  116. $base64 = $server->base64($value);
  117. $value = $boolean->value;
  118. $value = $date_time->value;
  119. $value = $base64->value;
  120. =head1 DESCRIPTION
  121. I<Frontier::Client> is an XML-RPC client over HTTP.
  122. I<Frontier::Client> instances are used to make calls to XML-RPC
  123. servers and as shortcuts for creating XML-RPC special data types.
  124. =head1 METHODS
  125. =over 4
  126. =item new( I<OPTIONS> )
  127. Returns a new instance of I<Frontier::Client> and associates it with
  128. an XML-RPC server at a URL. I<OPTIONS> may be a list of key, value
  129. pairs or a hash containing the following parameters:
  130. =over 4
  131. =item url
  132. The URL of the server. This parameter is required. For example:
  133. $server = Frontier::Client->new( 'url' => 'http://betty.userland.com/RPC2' );
  134. =item proxy
  135. A URL of a proxy to forward XML-RPC calls through.
  136. =item encoding
  137. The XML encoding to be specified in the XML declaration of outgoing
  138. RPC requests. Incoming results may have a different encoding
  139. specified; XML::Parser will convert incoming data to UTF-8. The
  140. default outgoing encoding is none, which uses XML 1.0's default of
  141. UTF-8. For example:
  142. $server = Frontier::Client->new( 'url' => 'http://betty.userland.com/RPC2',
  143. 'encoding' => 'ISO-8859-1' );
  144. =item use_objects
  145. If set to a non-zero value will convert incoming E<lt>i4E<gt>,
  146. E<lt>floatE<gt>, and E<lt>stringE<gt> values to objects instead of
  147. scalars. See int(), float(), and string() below for more details.
  148. =item debug
  149. If set to a non-zero value will print the encoded XML request and the
  150. XML response received.
  151. =back
  152. =item call($method, @args)
  153. Forward a procedure call to the server, either returning the value
  154. returned by the procedure or failing with exception. `C<$method>' is
  155. the name of the server method, and `C<@args>' is a list of arguments
  156. to pass. Arguments may be Perl hashes, arrays, scalar values, or the
  157. XML-RPC special data types below.
  158. =item boolean( $value )
  159. =item date_time( $value )
  160. =item base64( $base64 )
  161. The methods `C<boolean()>', `C<date_time()>', and `C<base64()>' create
  162. and return XML-RPC-specific datatypes that can be passed to
  163. `C<call()>'. Results from servers may also contain these datatypes.
  164. The corresponding package names (for use with `C<ref()>', for example)
  165. are `C<Frontier::RPC2::Boolean>',
  166. `C<Frontier::RPC2::DateTime::ISO8601>', and
  167. `C<Frontier::RPC2::Base64>'.
  168. The value of boolean, date/time, and base64 data can be set or
  169. returned using the `C<value()>' method. For example:
  170. # To set a value:
  171. $a_boolean->value(1);
  172. # To retrieve a value
  173. $base64 = $base64_xml_rpc_data->value();
  174. Note: `C<base64()>' does I<not> encode or decode base64 data for you,
  175. you must use MIME::Base64 or similar module for that.
  176. =item int( 42 );
  177. =item float( 3.14159 );
  178. =item string( "Foo" );
  179. By default, you may pass ordinary Perl values (scalars) to be encoded.
  180. RPC2 automatically converts them to XML-RPC types if they look like an
  181. integer, float, or as a string. This assumption causes problems when
  182. you want to pass a string that looks like "0096", RPC2 will convert
  183. that to an E<lt>i4E<gt> because it looks like an integer. With these
  184. methods, you could now create a string object like this:
  185. $part_num = $server->string("0096");
  186. and be confident that it will be passed as an XML-RPC string. You can
  187. change and retrieve values from objects using value() as described
  188. above.
  189. =back
  190. =head1 SEE ALSO
  191. perl(1), Frontier::RPC2(3)
  192. <http://www.scripting.com/frontier5/xml/code/rpc.html>
  193. =head1 AUTHOR
  194. Ken MacLeod <[email protected]>
  195. =cut
  196. 1;