File Coverage

lib/Amazon/SimpleDB.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package Amazon::SimpleDB;
2 2     2   27877 use strict;
  2         6  
  2         81  
3 2     2   11 use warnings;
  2         4  
  2         97  
4              
5             our $VERSION = '0.03';
6              
7 2     2   2304 use URI;
  2         23300  
  2         591  
8 2     2   5375 use LWP::UserAgent;
  2         100622  
  2         69  
9 2     2   1501 use Digest::HMAC_SHA1;
  2         15580  
  2         82  
10 2     2   1758 use MIME::Base64 qw(encode_base64);
  2         1447  
  2         141  
11 2     2   13 use Carp qw( croak );
  2         4  
  2         93  
12              
13 2     2   495 use Amazon::SimpleDB::Domain;
  0            
  0            
14             use Amazon::SimpleDB::Response;
15              
16             use constant SERVICE_URI => 'http://sdb.amazonaws.com/';
17             use constant KEEP_ALIVE_CACHESIZE => 10;
18              
19             sub new {
20             my $class = shift;
21             my $args = shift || {};
22             my $self = bless $args, $class;
23             croak "No aws_access_key_id" unless $self->{aws_access_key_id};
24             croak "No aws_secret_access_key" unless $self->{aws_secret_access_key};
25             unless ($self->{agent}) {
26             my $agent = LWP::UserAgent->new(keep_alive => KEEP_ALIVE_CACHESIZE);
27             $agent->timeout(10);
28             $agent->env_proxy;
29             $self->{agent} = $agent;
30             }
31             return $self;
32             }
33              
34             sub domains {
35             my $self = shift;
36             my $args = shift || {};
37             my $params = {};
38             $params->{MaxNumberOfDomains} = $args->{'limit'} if $args->{'limit'};
39             $params->{NextToken} = $args->{'next'} if $args->{'next'};
40             my $res = $self->request('ListDomains', $params);
41             return
42             Amazon::SimpleDB::Response->new(
43             {
44             http_response => $res,
45             account => $self
46             }
47             );
48             }
49              
50             sub domain {
51             return Amazon::SimpleDB::Domain->new({name => $_[1], account => $_[0]});
52             }
53              
54             sub create_domain { # note no more than 100 per account.
55             my ($self, $name) = @_;
56             my $res = $self->request('CreateDomain', {DomainName => $name});
57             return
58             Amazon::SimpleDB::Response->new(
59             {
60             http_response => $res,
61             account => $self
62             }
63             );
64             }
65              
66             sub delete_domain {
67             my ($self, $name) = @_;
68             my $res = $self->request('DeleteDomain', {DomainName => $name});
69             return
70             Amazon::SimpleDB::Response->new(
71             {
72             http_response => $res,
73             account => $self
74             }
75             );
76             }
77              
78             #--- utility methods
79              
80             sub request { # returns "raw" HTTP Response from SimpleDB
81             my ($self, $action, $params) = @_;
82             croak "No Action parameter" unless $action;
83             $params ||= {};
84             $params->{Action} = $action;
85             $params->{AWSAccessKeyId} = $self->{aws_access_key_id};
86             $params->{Version} = '2007-11-07';
87             $params->{SignatureVersion} = 1 unless defined $params->{SignatureVersion};
88             $params->{Timestamp} = timestamp() unless $params->{Expires};
89             my $time = $params->{Expires} || $params->{Timestamp};
90             my $sig = '';
91             if ($params->{SignatureVersion} == 1) {
92             $sig .= $_ . $params->{$_}
93             for (sort { lc($a) cmp lc($b) } keys %$params)
94             ; # Must be alphabetical in a case-insensitive manner.
95             } else {
96             $sig = $params->{Action} . $time;
97             }
98             my $hash = Digest::HMAC_SHA1->new($self->{aws_secret_access_key});
99             $hash->add($sig);
100             $params->{Signature} = encode_base64($hash->digest, '');
101             my $uri = URI->new(SERVICE_URI);
102             $uri->query_form($params);
103             return $self->{agent}->get($uri->as_string);
104             }
105              
106             sub timestamp {
107             my $t = shift;
108             $t = time unless defined $t;
109             my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
110             gmtime($t);
111             return
112             sprintf("%4i-%02i-%02iT%02i:%02i:%02iZ",
113             ($year + 1900),
114             ($mon + 1),
115             $mday, $hour, $min, $sec);
116             }
117              
118             1;
119              
120             __END__