AWS-SQS-Simple

 view release on metacpan or  search on metacpan

lib/AWS/SQS/Simple.pm  view on Meta::CPAN

Constructs a new AWS::SQS::Simple object

Following are the parametes taken by the constructor

    my $ob = AWS::SQS::Simple->new(
                ACCESS_KEY        => '..'  , 
                SECRET_ACCESS_KEY => '..'  , 

                AWS_ACCOUNT_ID    => '..'  , 

                END_POINT         => '..'  , 

              );

=cut

sub new {
    
    my $class = shift;
    
    my %parameter_hash;

    my $count = @_;

    my $usage_howto = "

Usage:

    my \$ob = AWS::SQS::Simple->new(
                ACCESS_KEY        => '..'  , 
                SECRET_ACCESS_KEY => '..'  , 

                AWS_ACCOUNT_ID    => '..'  , 

                END_POINT         => '..'  , 

              );

";

    %parameter_hash = @_;

    croak $usage_howto							                unless( $parameter_hash{ AWS_ACCOUNT_ID    } ) ;

    croak $usage_howto						                        unless( $parameter_hash{ ACCESS_KEY        } ) ;
    croak $usage_howto									unless( $parameter_hash{ SECRET_ACCESS_KEY } ) ;

    croak $usage_howto									unless( $parameter_hash{ END_POINT         } ) ;

    my $self = {

	ACCESS_KEY        => $parameter_hash{ ACCESS_KEY        } ,
	SECRET_ACCESS_KEY => $parameter_hash{ SECRET_ACCESS_KEY } ,

	AWS_ACCOUNT_ID    => $parameter_hash{ AWS_ACCOUNT_ID    } ,

	END_POINT         => $parameter_hash{ END_POINT         } ,

    };

    ## Private and class data here. 

    bless( $self, $class );

    return $self;

}


=head1 FUNCTIONS

No functions are exported by default.

Following functions are all available through the AWS::SQS::Simple Object.

=head2 create_queue
	
This function creates a new queue.

Usage :

 my %params_hash = (

      QUEUE_NAME              => QUEUE Name        ,

      'AttributeName.1.Name'  => Attribute Name    , 
      'AttributeName.1.Value' => Attribute Value   , [ Required if there is a corresponding Name Attribute.n.name parameter ]

      'AttributeName.2.Name'  => Attribute Name    , 
      'AttributeName.2.Value' => Attribute Value   , [ Required if there is a corresponding Name Attribute.n.name parameter ]

    .....

     );

$ob->create_queue->( \%params_hash )


=cut

sub create_queue {

    my $self   = shift ;
    my $params = shift ;

    my $params_to_pass = {
        'Action'                => 'CreateQueue'              ,
	'QueueName'             => $params->{ QUEUE_NAME }    ,
        'AWSAccessKeyId'        => $self->{ ACCESS_KEY }      ,
        'Timestamp'             => _generate_timestamp()      ,
        'SignatureVersion'      => 2                          ,
        'Version'               => '2011-10-01'               ,
        'SignatureMethod'       => 'HmacSHA256'               ,

	%{ $params }
    };

    my $url      = $self->_get_url( $params_to_pass ) ;
    my $response = $self->_make_request( $url )       ;
    
    return $response                                  ;

lib/AWS/SQS/Simple.pm  view on Meta::CPAN

    my $params = shift ;

    my $to_sign ;
    for my $key( sort keys %$params ) {

        $to_sign .= '&' if $to_sign ;

        my $key_octets   = encode('utf-8-strict', $key              ) ;
        my $value_octets = encode('utf-8-strict', $params->{ $key } ) ;

        $to_sign .= escape( $key_octets ) . '=' . escape( $value_octets ) ;

    }
    
    return $to_sign ;
}


=head2 escape 

    URI escape only the characters that should be escaped, according to RFC 3986

=cut

sub escape {

    my ($str) = @_;

    return uri_escape_utf8( $str,'^A-Za-z0-9\-_.~' ) ;
}

=head2 _generate_timestamp 

 Calculate current TimeStamp 

=cut 

sub _generate_timestamp {

    return sprintf("%04d-%02d-%02dT%02d:%02d:%02d.000Z",
                   sub { ($_[5]+1900,
                          $_[4]+1,
                          $_[3],
                          $_[2],
                          $_[1],
                          $_[0])
                   }->(gmtime(time)));
}

=head2 _make_request 

=cut

sub _make_request {

    my $self          = shift ;
    my $url_to_access = shift ;

    my $contents                             ;
    my $attempts = 0                         ;
    my $got_data = 0                         ;
    
    my $this_profile_location                ;
    
    my $response;
    
    until( $got_data or $attempts > 5 ) { 
	
	my $request = HTTP::Request->new(
	    GET => $url_to_access
	    );
	
	my $ua = LWP::UserAgent->new             ;
	$ua->timeout(60)                         ;
	$ua->env_proxy                           ;
	$ua->agent( 'AWIS-INFO_GET/'.$VERSION ) ;
	
	$response = $ua->request( $request )  ;

	if( $response->is_success() ) {
	    
	    $contents = $response->content;
	    $got_data = 1;
	    
	} else  {

	    $contents = $response->content          ;
	    
	    print STDERR "ERROR : $contents"        ;  

	    $attempts++             ;
	    sleep( $attempts * 10 ) ;
	    
	}
	
	$contents = $response->content          ;
	
	$attempts++                             ;
	
    }
    

    my $response_content = $response->content     ;

    return $response_content                      ;

}


=head1 AUTHOR

Ankita, C<< <sankita.11 at gmail.com> >>


=head1 COPYRIGHT & LICENSE

Copyright 2014 Ankita Singhal, all rights reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1; # End of AWS::SQS::Simple



( run in 0.946 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )