DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Rapleaf Address Book API In Perl
Retrieve e-mail contacts from several services using the Rapleaf Address Book API.
It accesses the Rapleaf API Web server and executes a request to retrieve the contact list of a given user of either Gmail, Yahoo, Hotmail and AOL.
Returns an associative array with the contacts names and e-mail addresses, as well the HTTP response status and any errors.
package Rapleaf;
use strict;
use LWP::UserAgent;
use HTTP::Request;
use XML::Simple;
sub getData {
my ($email, $pass, $api_key, $url) = @_;
my $post_data = "login=$email&password=$pass";
$url ||= 'http://api.rapleaf.com/v2/abook';
my $agent = LWP::UserAgent->new();
my $request = HTTP::Request->new(POST => $url);
$request->content($post_data);
$request->header( 'Authorization' => $api_key );
my $response;
$response = $agent->request($request);
my %result;
if($response->code == 200) {
my $xml = new XML::Simple;
%result = %{$xml->XMLin($response->content)};
# if a single contact if found, XMLin returns a result set of a different format, therefore we need to manually format it
if ($result{'contact'}->{'name'}) {
$result{'contact'}->{$result{'contact'}->{'name'}}
= {'email'=>$result{'contact'}->{'email'}};
delete $result{'contact'}->{'name'};
delete $result{'contact'}->{'email'};
}
} elsif ($response->code == 400) {
$result{'error'} = 'The request did not contain all required parameters: '.$response;
} elsif ($response->code == 401) {
$result{'error'} = 'API key was not provided or is invalid.';
} elsif ($response->code == 420) {
$result{'error'} = 'Login failed.';
} elsif ($response->code == 500) {
$result{'error'} = 'There was an unexpected error on our server. This should be very rare and if you see it please contact developer@rapleaf.com.';
} elsif ($response->code == 520) {
$result{'error'} = 'There was an error while reading the contacts from the address book.';
}
$result{'status'} = $response->code;
return \%result;
}
1;





