
Web::API - A Simple base module to implement almost every RESTful API with just a few lines of configuration

Version 0.6.6

Implement the RESTful API of your choice in 10 minutes, roughly.
package Net::CloudProvider;
use Any::Moose;
with 'Web::API';
our $VERSION = "0.1";
has 'commands' => (
is => 'rw',
default => sub {
{
list_nodes => { method => 'GET' },
node_info => { method => 'GET', require_id => 1 },
create_node => {
method => 'POST',
default_attributes => {
allowed_hot_migrate => 1,
required_virtual_machine_build => 1,
cpu_shares => 5,
required_ip_address_assignment => 1,
primary_network_id => 1,
required_automatic_backup => 0,
swap_disk_size => 1,
},
mandatory => [
'label',
'hostname',
'template_id',
'cpus',
'memory',
'primary_disk_size',
'required_virtual_machine_build',
'cpu_shares',
'primary_network_id',
'required_ip_address_assignment',
'required_automatic_backup',
'swap_disk_size',
]
},
update_node => { method => 'PUT', require_id => 1 },
delete_node => { method => 'DELETE', require_id => 1 },
start_node => {
method => 'POST',
require_id => 1,
post_id_path => 'startup',
},
stop_node => {
method => 'POST',
require_id => 1,
post_id_path => 'shutdown',
},
suspend_node => {
method => 'POST',
require_id => 1,
post_id_path => 'suspend',
},
};
},
);
sub commands {
my ($self) = @_;
return $self->commands;
}
sub BUILD {
my ($self) = @_;
$self->user_agent(__PACKAGE__ . ' ' . $VERSION);
$self->base_url('https://ams01.cloudprovider.net/virtual_machines');
$self->content_type('application/json');
$self->extension('json');
$self->wrapper_key('virtual_machine');
$self->mapping({
os => 'template_id',
debian => 1,
id => 'label',
disk_size => 'primary_disk_size',
});
return $self;
}
1;
later use as:
use Net::CloudProvider;
my $nc = Net::CloudProvider(user => 'foobar', api_key => 'secret');
my $response = $nc->create_node({
id => 'funnybox',
hostname => 'node.funnybox.com',
os => 'debian',
cpus => 2,
memory => 256,
disk_size => 5,
allowed_hot_migrate => 1,
required_virtual_machine_build => 1,
cpu_shares => 5,
required_ip_address_assignment => 1,
});

most important configuration part of the module which has to be provided by the module you are writing.
the following keys are valid/possible:
method
require_id
path
pre_id_path
post_id_path
wrapper_key
default_attributes
mandatory
extension
content_type
incoming_content_type
outgoing_content_type
the request path for non require_id commands is being build as:
$base_url/$path.$extension
accordingly requests with require_id:
$base_url/$pre_id_path/$id/$post_id_path.$extension
whereas $id can be any arbitrary object like a domain, that the API in question does operations on.
get/set base URL to API, can include paths
get/set api_key
get/set username/account name
get/set name of the hash key in the POST data structure that has to hold the api_key
supply mapping table, hashref of format { key => value }
default: undef
get/set custom headers sent with each request
get/set authentication type. currently supported are only 'basic', 'hash_key' or 'none'
default: none
get/set default HTTP method
default: GET
get/set file extension, e.g. '.json'
get/set User Agent String
default: "Web::API $VERSION"
get/set LWP::UserAgent timeout
get/set LWP::UserAgent object
default: 'text/plain'
default: undef
default: undef
default: 0
default: HTTP::Cookies->new


Tobias Kirschstein, <mail at lev.geek.nz>

Please report any bugs or feature requests to bug-Web-API at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Web-API. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


You can find documentation for this module with the perldoc command.
perldoc Web::API
You can also look for information at:


Copyright 2012 Tobias Kirschstein.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.