Amazon-MWS

 view release on metacpan or  search on metacpan

lib/Amazon/MWS/Routines.pm  view on Meta::CPAN

            force_array($root, 'Error');
            Amazon::MWS::Exception::Response->throw(errors => $root->{Error}, xml => $content);
        }

        if ($response->code == 503) {
            my $hash = $xs->xml_in($content);
            my $root = $hash->{ErrorResponse};
            force_array($root, 'Error');
            Amazon::MWS::Exception::Throttled->throw(errors => $root->{Error}, xml => $content);
        }

        unless ($response->is_success) {
            Amazon::MWS::Exception::Transport->throw(request => $request, response => $response);
        }

        if (my $md5 = $response->header('Content-MD5')) {
            Amazon::MWS::Exception::BedChecksum->throw(response => $response) 
                unless ($md5 eq md5_base64($content) . '==');
        }

        return $content if ($spec->{raw_body} || $args->{raw_body});

        my $hash = $xs->xml_in($content);

        my $root = $hash->{$method_name . 'Response'}
            ->{$method_name . 'Result'};

        return $spec->{respond}->($root);
    };

    my $module_name = $spec->{module_name} || 'Amazon::MWS::Client';
    my $fqn = join '::', "$module_name", $method_name;

    no strict 'refs';
    *$fqn = $method;

}

sub force_array {

    my ($hash, $key) = @_;
    my $val = $hash->{$key};

    if (!defined $val) {
        $val = [];
    }
    elsif (ref $val ne 'ARRAY') {
        $val = [ $val ];
    }

    $hash->{$key} = $val;
}

sub sign_request {
    my ($self, $request, %form) = @_;

    my $uri = $request->uri;
    my %params = ($request->method eq 'GET' ) ? $uri->query_form : %form;

    my $canonical = join '&', map {
        my $param = uri_escape($_);
        my $value = uri_escape($params{$_});
        "$param=$value";
    } sort keys %params;

    my $path = $uri->path || '/';
    my $string = $request->method . "\n"
	. $uri->authority . "\n"
        . $path . "\n"
        . $canonical;

    $params{Signature} = Digest::SHA::hmac_sha256_base64($string, $self->{secret_key});
     while (length($params{Signature}) % 4) {
                $params{Signature} .= '=';
        }

    if ($request->{_method} eq 'GET' || $request->{_content} ) {
    	$uri->query_form(\%params);
    } else {
	$request->{_content} = "$canonical&Signature=$params{Signature}";
    }
	$request->uri($uri);
	return $request;

}

sub convert {
    my ($hash, $key, $type) = @_;
    $hash->{$key} = from_amazon($type, $hash->{$key});
}


sub new {
	
 my($pkg, %opts) = @_;

 $opts{configfile} ||= 'amazon.xml';

  if (-r $opts{configfile} ) {

    my $xmlconfig = XML::Simple::XMLin("$opts{configfile}");

    $opts{access_key_id} ||= $xmlconfig->{access_key_id};
    $opts{secret_key} ||= $xmlconfig->{secret_key};
    $opts{merchant_id} ||= $xmlconfig->{merchant_id};
    $opts{marketplace_id} ||= $xmlconfig->{marketplace_id};
    $opts{endpoint} ||= $xmlconfig->{endpoint};
    $opts{debug} ||= $xmlconfig->{debug};
    $opts{logfile} ||= $xmlconfig->{logfile};
 }

 my $attr = $opts->{agent_attributes};
    $attr->{Language} = 'Perl';

    my $attr_str = join ';', map { "$_=$attr->{$_}" } keys %$attr;
    my $appname  = $opts{Application} || 'Amazon::MWS::Client';
    my $version  = $opts{Version}     || 0.5;

    my $agent_string = "$appname/$version ($attr_str)";

    die 'No access key id' unless  $opts{access_key_id};
    die 'No secret key' unless $opts{secret_key};



( run in 1.421 second using v1.01-cache-2.11-cpan-9581c071862 )