move RPC over HTTP calls to HTTPTalker.pm
[xmlrtorrent.git] / xmlrtorrent / HTTPTalker.pm
index bab2209..4eb6a8d 100644 (file)
@@ -8,7 +8,7 @@ use xmlrtorrent::TalkerBase;
 @ISA = qw(xmlrtorrent::TalkerBase);
 
 use Data::Dumper;
-
+use RPC::XML::Client;
 use strict;
 
 sub new {
@@ -17,6 +17,12 @@ sub new {
 
     $self->{'NAME'} = 'http';
     $self->{'DESC'} = 'talker using RPC over HTTP';
+    $self->{'_PARAMS'} = {
+       'XMLURL' => ['', 'URL of SCGI script'],
+       'USERNAME' => ['', 'username for RPC credentials (optional)'],
+       'PASSWORD' => ['', 'password for RPC credentials (optional)'],
+    };
+    $self->{'_LASTXMLURL'} = undef;
 
     bless($self, $class);
 
@@ -25,4 +31,26 @@ sub new {
     return $self;
 }
 
+sub send_request {
+    my $self = shift;
+    my @params = @_;
+
+    unless (exists($self->{'XMLURL'}) and defined($self->{'XMLURL'})) {
+       return 'http talker: XMLURL not set';
+    }
+
+    if ($self->{'XMLURL'} ne $self->{'_LASTXMLURL'}) {
+       $self->{'_LASTXMLURL'} = $self->{'XMLURL'};
+       $self->{'__RPCClient'} = RPC::XML::Client->new($self->{'XMLURL'});
+    }  
+    
+    if ((exists($self->{'USERNAME'}) and exists($self->{'PASSWORD'})) and
+       ($self->{'USERNAME'} ne '')) {
+       # mitch: let me guess, the realm must be configurable, too!
+       $self->{'__RPCClient'}->credentials('', $self->{'USERNAME'}, $self->{'PASSWORD'});
+    }
+
+    return $self->{'__RPCClient'}->send_request(@params);
+}
+
 1;