Sunday, August 23, 2009

LWP, cURL, OpenSSL and Posterous

The fun of programming is there is always subtle mechanism in implementation. It costs time to discover but sometime is deserving. for example.

$ curl --basic -u <user> http://example.com/api/

$ echo -n "user:pass" | openssl base64 -e

and

#!/usr/bin/env perl
use LWP::UserAgent;
use HTTP::Request;
use UNIVERSAL::dump;
use XML::Simple;
use Data::Dumper;
use YAML qw(LoadFile);
$config = LoadFile "$ENV{HOME}/.posterous";
my $req = HTTP::Request->new(GET => "http://posterous.com/api/getsites");
$req->authorization_basic($config->{core}->{user}, $config->{core}->{pass});
$content = LWP::UserAgent->new->request($req)->content;
$content =~ s/name/title/g;
print $content;
print Dumper XMLin($content);
view raw lwpex.pl hosted with ❤ by GitHub


to

#!/usr/bin/env perl
use LWP::UserAgent;
use HTTP::Request;
use UNIVERSAL::dump;
use YAML qw(LoadFile);
package HTTP::Request;
sub authorization_basic {
my ($self, $user, $pass) = @_;
$self->SUPER::authorization_basic($user, $pass);
return $self;
}
sub submit_by {
my ($self, $ua) = @_;
$ua->request($self);
}
package HTTP::Response;
use XML::Simple;
sub xmlized_content {
my ($self) = @_;
$content = $self->content;
$content =~ s/name/title/g;
XMLin($content);
}
package main;
use Data::Dumper;
my $config = LoadFile("$ENV{HOME}/.posterous")->{core};
$UA = LWP::UserAgent->new;
print Dumper(
HTTP::Request
->new(GET => "http://posterous.com/api/getsites")
->authorization_basic($config->{user}, $config->{pass})
->submit_by($UA)
->xmlized_content
);
view raw lwpex.pl hosted with ❤ by GitHub


 

No comments: