WWW::DreamHost::API - Perl interface to DreamHost Web Panel API


WWW-DreamHost-API documentation Contained in the WWW-DreamHost-API distribution.

Index


Code Index:

NAME

Top

WWW::DreamHost::API - Perl interface to DreamHost Web Panel API

VERSION

Top

1.03

SYNOPSIS

Top

 my $key = '6SHU5P2HLDAYECUM';
 my $api = new WWW::DreamHost::API($key);
 my $res = $api->command('api-list_accessible_cmds');
 print Data::Dumper->Dump([$res]);

DESCRIPTION

Top

WWW::DreamHost::API provides a simple Perl interface to DreamHost Web Panel API.

Read more about API commands at http://wiki.dreamhost.com/Application_programming_interface

METHODS

Top

new ( $key )

Creates WWW::DreamHost::API object

uuid ( )

Returns UUID. Probably actually unique (randomly based on key and run time). Don't worry, if it's not, because it's reinitializing in case of failure (though I cannot imagine how it can happen).

reinit ( )

If unique check fails, attempt to re-initialize. You don't have to call it yourself.

command ( $cmd, [ $param => $value, ]+ )

Execute a command.

To get a list of availible commands, use something like that:

    my $res = $api->command('api-list_accessible_cmds');
    if ($res->{'success'}) {
        my @commands = @{ $res->{'data'} };
    }

Returns a hash reference with (usually) 'result' and 'data' keys. 'result' can be 'success' or 'error', and 'data' depends on command executed.

See http://wiki.dreamhost.com/Application_programming_interface for more details.

CONFIGURATION AND ENVIRONMENT

Top

WWW::DreamHost::API is based on libwww-perl which uses Crypt::SSLeay or IO::Socket::SSL, with all consequences: HTTPS_proxy environment variable and so on. See LWP documentation unless you're already familiar with it.

DEPENDENCIES

Top

LWP::UserAgent Crypt::SSLeay Data::UUID LWP::protocol::https

INCOMPATIBILITIES

Top

Not known.

BUGS AND LIMITATIONS

Top

Not known, but test suite MAY fail if DreamHost adds some other commands or change their behaviour. If you are using CPAN::Reporter in your service, I'll know about it. Consider installing it, as it really helps authors to know about possible bugs. See http://wiki.cpantesters.org/wiki/QuickStart.

AUTHOR

Top

Serguei Trouchelle stro@cpan.org

LICENSE AND COPYRIGHT

Top


WWW-DreamHost-API documentation Contained in the WWW-DreamHost-API distribution.
package WWW::DreamHost::API;

# $Id: API.pm 28 2011-03-31 14:05:13Z stro $

use strict;
use warnings;
use Carp;

our $VERSION = '1.03';

use LWP::UserAgent;
use Data::UUID;

sub new {
    my $class = shift;
    my $key   = shift;

    croak 'You should provide a key in order to use WWW::DreamHost::API' unless defined $key;

    my $self = {
        '__key'  => $key,
        '__ua'   => LWP::UserAgent->new('agent' => 'WWW-DreamHost-API/' . $VERSION),
        '__ug'   => new Data::UUID,
        '__uniq' => int(rand(time())),

    };

    $self->{'__ua'}->env_proxy();

    bless $self, $class;
    return $self;
}

sub uuid {
    my $self = shift;
    my $uuid = $self->{'__ug'}->create_from_name('WWW-DreamHost-API-'. $self->{'__key'}, $self->{'__uniq'}++);
    return $self->{'__ug'}->to_string($uuid);
}

sub reinit {
    my $self = shift;
    $self->{'__uniq'} = int(rand(time()));
    return 1;
}

sub command {
    my $self = shift;
    my $cmd = shift;
    my %extraparam = @_;
    delete $extraparam{$_} foreach (qw/ key cmd unique_id format /); # fool-proof

    while (1) {
        # Loop until UUID is unique. Though I'm VERY doubtful this can happen in real life.

        my $res = $self->{'__ua'}->post('https://api.dreamhost.com/', {
            'key'       => $self->{'__key'},
            'cmd'       => $cmd,
            'unique_id' => $self->uuid(),
            'format'    => 'perl',
            %extraparam,
        });

        if ($res->is_success()) {
            my $result;
            eval $res->content();
            if ($@) {
                eval { die $@; };
                return;
            } else { 
                if ($result->{'result'} eq 'error' and $result->{'data'} eq 'unique_id_already_used') {
                    $self->reinit();    # Reinitialize random seed
                    redo;               # Send another request
                }
                return $result;
            }
        } else {
            eval { die $res->status_line(); };
            return;
        }
    }

    return; # for Perl::Critic
}

1;