ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
[RFC] XML::SAX::Consumer::XMLRPC
by Aaron Straup Cope other posts by this author
Mar 29 2002 11:29PM messages near this date
view in the new Beta List Site
Re: XML::Parser & "invalid character" | Re: [RFC] XML::SAX::Consumer::XMLRPC
Hi all,

As part of the process of wrapping my mind around all this SAX (pipelines)
stuff, I wrote a quick and dirty Consumer widget (see below) for sending
the XML to a remote URI as an HTTP POST request. There is nothing XML-RPC
specific here, really.

It works. If I write the following...

my $xmlrpc =
XML::SAX::Consumer::XMLRPC-> new(Uri=>'http://betty.userland.com/RPC2');
my $writer = XML::SAX::Writer-> new(Output=>$xmlrpc);
# write XML-RPC message here

...the &finalize method spits out a nice XML-RPC response. Cool.

What I'm wondering is :

1) Is the ::Consumer namespace the right "place" for this kind of thing?

2) Would the "right thing" to do be to open a socket in the constructor
and write to it in &output. Storing all the data in the object seems to
defeat the spirit of SAX. Or am I just thinking about it too much?

3) Presumably, it should also inherit from XML::SAX::Base so that the
constructor so that the package can also be fed handlers to deal with the
reply?

Comments and suggestions would be appreciated. Thanks,

***

package XML::SAX::Consumer::XMLRPC;

@XML::SAX::Consumer::XMLRPC::ISA = qw
(XML::SAX::Writer::ConsumerInterface);
use XML::SAX::Writer;

use LWP::UserAgent;
use HTTP::Request;

sub new {
  my $pkg = shift;
  my $args = {@_};

  my $self = {};
  bless $self, $pkg;

  $self-> {'__uri'}= $args->{'Uri'};
  return $self;
}

sub output {
  my $self = shift;
  my $data = shift;
  $self-> {'__post'} .= $data;
}

sub finalize {
  my $self = shift;

  my $ua  = LWP::UserAgent-> new();
  my $req = HTTP::Request-> new(POST=>$self->{'__uri'});
  $req-> content($self->{'__post'});

  my $res = $ua-> request($req);

  if (! $res-> is_success()) {
    die $res-> code();
  }

  print $res-> content();
}

return 1;










_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Aaron Straup Cope
Robin Berjon
Matt Sergeant
Aaron Straup Cope

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved