Amazon-API
view release on metacpan or search on metacpan
lib/Amazon/API.pm view on Meta::CPAN
DescribeRule
DisableRule
EnableRule
ListRuleNamesByTarget
ListRules
ListTargetsByRule
PutEvents
PutPermission
PutRule
PutTargets
RemovePermission
RemoveTargets
TestEventPattern/;
sub new {
my $class = shift;
my $options = shift || {};
$class->SUPER::new({
%$options,
service_url_base => 'events',
version => undef,
api => 'AWSEvents',
api_methods => \@API_METHODS,
content_type => 'application/x-amz-json-1.1'
});
}
1;
=head1 DESCRIPTION
Class to use for constructing AWS API interfaces. Typically used as
the parent class, but can be used directly. See
C<Amazon::CloudWatchEvents> for an example or sub-classing. See
L</IMPLEMENTATION NOTES> for using C<Amazon::API> directly to call AWS services.
=head1 ERRORS
Errors encountered are returned as an C<Amazon::API::Error> exception
object. See L<Amazon::API::Error>/
=cut
use strict;
use warnings;
use parent qw/Class::Accessor Exporter/;
use Amazon::API::Error;
use Amazon::Credentials;
use AWS::Signature4;
use Data::Dumper;
use HTTP::Request;
use JSON qw/to_json from_json/;
use LWP::UserAgent;
use Scalar::Util qw/reftype/;
use XML::Simple;
__PACKAGE__->follow_best_practice;
__PACKAGE__->mk_accessors(qw/action api api_methods version content_type
http_method credentials response protocol
region url service_url_base
signer target user_agent debug last_action
aws_access_key_id aws_secret_access_key token
/);
use vars qw/@EXPORT $VERSION/;
@EXPORT=qw/$VERSION/;
our $VERSION = '1.1.4-1'; $VERSION=~s/\-.*$//;
=pod
=head1 METHODS
=head2 new
new( options )
=over 5
=item credentials (required)
C<Amazon::Credentials> object or at least an object that
C<->can(get_aws_access_key_id)> and
C<->can(get_aws_secret_access_key)> and C<->can(get_token)>
=item user_agent
Your own user agent object or by default C<LWP::UserAgent>. Using
C<Furl>, if you have it avaiable may result in faster response.
=item api (reqired)
The name of the AWS service. Example: AWSEvents
=item url
The service url. Example: https://events.us-east-1.amazonaws.com
=item debug
0/1 - will dump request/response if set to true.
=item action
The API method. Example: PutEvents
=item content_type
Default content for references passed to the C<invoke_api()> method. The default is C<application/x-amz-json-1.1>.
=item protocol
One of 'http' or 'https'. Some Amazon services do not support https (yet).
=back
lib/Amazon/API.pm view on Meta::CPAN
The class will stub out methods for the API if you pass an array of
API method names. The stub is equivalent to:
sub some_api {
my $self = shift;
$self ->invoke_api('SomeApi', @_);
}
Some will also be happy to know that the class will create an
equivalent CamelCase version of the method. If you choose to override
the method, you should override the snake case version of the method.
As an example, here is a possible implementation of
C<Amazon::CloudWatchEvents> that implements one of the API calls.
package Amazon::CloudWatchEvents;
use parent qw/Amazon::API/;
sub new {
my $class = shift;
my $options = shift || {};
$options->{api} 'AWSEvents';
$options->{url} 'https://events.us-east-1.amazonaws.com';
$options->{api_methods} => [ 'ListRules' ];
return $class->SUPER::new($options);
}
1;
Then...
my $cwe = new Amazon::CloudWatchEvents();
$cwe->ListRules({});
Of course, creating a class for the service is optional. It may be
desirable however to create higher level and more convenient methods
that aid the developer in utilizing a particular API.
my $api = new Amazon::API({ credentials => new Amazon::Credentials, api => 'AWSEvents', url => 'https://events.us-east-1.amazonaws.com' });
$api->invoke_api('ListRules', {});
=head2 Content-Type
Yet another piece of evidence that suggests the I<organic> nature of
the Amazon API ecosystem is their use of multiple forms of input to
their methods indicated by the required Content-Type for different
services. Some of the variations include:
application/json
application/x-amz-json-1.0
application/x-amz-json-1.1
application/x-www-form-urlencoded
Accordingly, the C<invoke_api()> can be passed the Content-Type or
will try to make "best guess" based on the input parameter you passed.
It guesses using the following decision tree:
=over 5
=item * If the Content-Type parameter is passed as the third argument, that is used. Full stop.
=item * If the C<parameters> value to C<invoke_api()> is a reference, then the Content-Type is either the value of C<get_content_type> or C<application/x-amzn-json-1.1>.
=item * If the C<parameters> value to C<invoke_api()> is a scalar, then the Content-Type is C<application/x-www-form-urlencoded>.
=back
You can set the default Content-Type used for the calling service when
a reference is passed to the C<invoke_api()> method by passing the
C<content_type> option to the constructor.
$class->SUPER::new({%@_, content_type => 'application/x-amz-json-1.1', api => 'AWSEvents',
url => 'https://events.us-east-1.amazonaws.com'});
=head1 SEE OTHER
C<Amazon::Credentials>, C<Amazon::API::Error>
=head1 AUTHOR
Rob Lauer - <rlauer6@comcast.net>
=cut
1;
( run in 0.787 second using v1.01-cache-2.11-cpan-39bf76dae61 )