| 1 | package WebService::Amazon::Route53::API; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use Digest::HMAC_SHA1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use HTTP::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use MIME::Base64; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Tie::IxHash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 3 | ▶ | use XML::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 3 | require Exporter; | |||||||||
| 14 | 3 | our @ISA = qw(Exporter); | |||||||||
| 15 | 3 | our @EXPORT = qw(_ordered_hash); | |||||||||
| 16 | |||||||||||
| 17 | |||||||||||
| 18 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 19 | 0 | my ($class, %args) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 0 | my $self = {}; | |||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | if (!defined $args{id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | carp "Required parameter 'id' is not defined"; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | if (!defined $args{key}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | carp "Required parameter 'key' is not defined"; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 0 | $self->{id} = $args{id}; | |||||||||
| 32 | 0 | $self->{key} = $args{key}; | |||||||||
| 33 | |||||||||||
| 34 | # Initialize an instance of XML::Simple | ||||||||||
| 35 | 0 | $self->{xs} = XML::Simple->new; | |||||||||
| 36 | |||||||||||
| 37 | # Initialize the user agent object | ||||||||||
| 38 | 0 | $self->{ua} = HTTP::Tiny->new( | |||||||||
| 39 | agent => 'WebService::Amazon::Route53/' . | ||||||||||
| 40 | $WebService::Amazon::Route53::VERSION . ' (Perl)' | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | # Keep track of the last error | ||||||||||
| 44 | 0 | $self->{error} = {}; | |||||||||
| 45 | |||||||||||
| 46 | 0 | $self->{base_url} = 'https://route53.amazonaws.com/'; | |||||||||
| 47 | |||||||||||
| 48 | 0 | return bless $self, $class; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub error { | ||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 52 | 0 | my ($self) = @_; | |||||||||
| 53 | |||||||||||
| 54 | 0 | return $self->{error}; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | # "Private" methods | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub _get_server_date { | ||||||||
|
Subroutine
_get_server_date: not called CC=2 SCAR=17.9
| |||||||||||
| 60 | 0 | my ($self) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $response = $self->{ua}->get($self->{base_url} . 'date'); | |||||||||
| 63 | 0 | my $date = $response->{headers}->{'date'}; | |||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | if (!$date) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | carp "Can't get Amazon server date"; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 0 | return $date; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | sub _request { | ||||||||
|
Subroutine
_request: not called CC=2 SCAR=17.9
| |||||||||||
| 73 | 0 | my ($self, $method, $url, $options) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 0 | my $date = $self->_get_server_date; | |||||||||
| 76 | |||||||||||
| 77 | 0 | my $hmac = Digest::HMAC_SHA1->new($self->{'key'}); | |||||||||
| 78 | 0 | $hmac->add($date); | |||||||||
| 79 | 0 | my $sig = encode_base64($hmac->digest, undef); | |||||||||
| 80 | |||||||||||
| 81 | 0 | my $auth = 'AWS3-HTTPS AWSAccessKeyId=' . $self->{'id'} . ',' . | |||||||||
| 82 | 'Algorithm=HmacSHA1,Signature=' . $sig; | ||||||||||
| 83 | # Remove trailing newlines, if any | ||||||||||
| 84 | 0 | $auth =~ s/\n//g; | |||||||||
| 85 | |||||||||||
| 86 | 0 | ▶ | $options = {} if !defined $options; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | 0 | $options->{headers}->{'Content-Type'} = 'text/xml'; | |||||||||
| 89 | 0 | $options->{headers}->{'Date'} = $date; | |||||||||
| 90 | 0 | $options->{headers}->{'X-Amzn-Authorization'} = $auth; | |||||||||
| 91 | |||||||||||
| 92 | 0 | my $response = $self->{ua}->request($method, $url, $options); | |||||||||
| 93 | |||||||||||
| 94 | 0 | return $response; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 0 | ▶ | sub _parse_error { | ||||||||
|
Subroutine
_parse_error: not called CC=1 SCAR=6.9
| |||||||||||
| 98 | 0 | my ($self, $xml) = @_; | |||||||||
| 99 | |||||||||||
| 100 | 0 | my $data = $self->{xs}->XMLin($xml); | |||||||||
| 101 | |||||||||||
| 102 | $self->{error} = { | ||||||||||
| 103 | type => $data->{Error}->{Type}, | ||||||||||
| 104 | code => $data->{Error}->{Code}, | ||||||||||
| 105 | message => $data->{Error}->{Message} | ||||||||||
| 106 | 0 | }; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | # Helpful subroutines | ||||||||||
| 110 | |||||||||||
| 111 | # Amazon expects XML elements in specific order, so we'll need to pass the data | ||||||||||
| 112 | # to XML::Simple as ordered hashes | ||||||||||
| 113 | 0 | ▶ | sub _ordered_hash (%) { | ||||||||
|
Subroutine
_ordered_hash: not called CC=1 SCAR=6.9
| |||||||||||
| 114 | 0 | tie my %hash => 'Tie::IxHash'; | |||||||||
| 115 | 0 | %hash = @_; | |||||||||
| 116 | 0 | \%hash | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 3 | 1; | |||||||||
| 120 | |||||||||||
| 121 | __END__ | ||||||||||