File Coverage

blib/lib/Paws/API/Caller.pm
Criterion Covered Total %
statement 87 95 91.5
branch 42 52 80.7
condition 21 30 70.0
subroutine 8 8 100.0
pod 0 3 0.0
total 158 188 84.0


line stmt bran cond sub pod time code
1             package Paws::API::Caller;
2 20     20   949184 use Moose::Role;
  20         57  
  20         210  
3 20     20   118442 use Carp;
  20         59  
  20         1557  
4 20     20   11291 use Paws::Net::APIRequest;
  20         82  
  20         949  
5 20     20   14041 use Paws::API::Response;
  20         89  
  20         22190  
6              
7             has caller => (is => 'ro', required => 1);
8              
9             has credentials => (
10             is => 'ro',
11             does => 'Paws::Credential',
12             required => 1,
13             handles => [ 'access_key', 'secret_key', 'session_token' ],
14             );
15              
16             requires 'new_from_result_struct';
17              
18             # converts the params the user passed to the call into objects that represent the call
19             sub new_with_coercions {
20 597     597 0 65295 my ($self, $class, %params) = @_;
21              
22 597         3610 Paws->load_class($class);
23 597         1727 my %p;
24              
25 597 100       5060 if ($class->does('Paws::API::StrToObjMapParser')) {
    100          
26 14         3283 my ($subtype) = ($class->meta->find_attribute_by_name('Map')->type_constraint =~ m/^HashRef\[(.*?)\]$/);
27 14 100       1899 if (my ($array_of) = ($subtype =~ m/^ArrayRef\[(.*?)\]$/)){
28 1         5 $p{ Map } = { map { $_ => [ map { $self->new_with_coercions("$array_of", %$_) } @{ $params{ $_ } } ] } keys %params };
  1         2  
  1         8  
  1         3  
29             } else {
30 13         54 $p{ Map } = { map { $_ => $self->new_with_coercions("$subtype", %{ $params{ $_ } }) } keys %params };
  14         7217  
  14         91  
31             }
32             } elsif ($class->does('Paws::API::StrToNativeMapParser')) {
33 4         2334 $p{ Map } = { %params };
34             } else {
35 579         298570 foreach my $att (keys %params){
36 769         41598 my $att_meta = $class->meta->find_attribute_by_name($att);
37            
38 769 50       48580 croak "$class doesn't have an $att" if (not defined $att_meta);
39 769         25347 my $type = $att_meta->type_constraint;
40            
41 769 100 66     8185 if ($type eq 'Bool') {
    100 100        
    100          
    50          
42 4 100       158 $p{ $att } = ($params{ $att } == 1)?1:0;
43             } elsif ($type eq 'Str' or $type eq 'Num' or $type eq 'Int') {
44 662         54864 $p{ $att } = $params{ $att };
45             } elsif ($type =~ m/^ArrayRef\[(.*?)\]$/){
46 58         9868 my $subtype = "$1";
47 58 100 66     798 if ($subtype eq 'Str' or $subtype eq 'Str|Undef' or $subtype eq 'Num' or $subtype eq 'Int' or $subtype eq 'Bool') {
      66        
      66        
      33        
48 32         155 $p{ $att } = $params{ $att };
49             } else {
50 26         64 $p{ $att } = [ map { $self->new_with_coercions("$subtype", %{ $_ }) } @{ $params{ $att } } ];
  36         12795  
  36         253  
  26         90  
51             }
52             } elsif ($type->isa('Moose::Meta::TypeConstraint::Enum')){
53 0         0 $p{ $att } = $params{ $att };
54             } else {
55 45         7895 $p{ $att } = $self->new_with_coercions("$type", %{ $params{ $att } });
  45         1757  
56             }
57             }
58             }
59 597         43515 return $class->new(%p);
60             }
61              
62             sub _is_internal_type {
63 301     301   8092 my ($self, $att_type) = @_;
64 301   66     1198 return ($att_type eq 'Str' or $att_type eq 'Str|Undef' or $att_type eq 'Int' or $att_type eq 'Bool' or $att_type eq 'Num');
65             }
66              
67             sub to_hash {
68 98     98 0 324 my ($self, $params) = @_;
69 98         297 my $refHash = {};
70              
71 98 50       509 if ($params->does('Paws::API::StrToNativeMapParser')) {
    50          
72 0         0 return $params->Map;
73             } elsif ($params->does('Paws::API::StrToObjMapParser')) {
74 0         0 return { map { ($_ => $self->to_hash($params->Map->{$_})) } keys %{ $params->Map } };
  0         0  
  0         0  
75             }
76              
77 98         55105 foreach my $att (grep { $_ !~ m/^_/ } $params->meta->get_attribute_list) {
  483         4436  
78 483         868 my $key = $att;
79 483 100       13837 if (defined $params->$att) {
80 162         588 my $att_type = $params->meta->get_attribute($att)->type_constraint;
81 162 50       11311 if ($att_type eq 'Bool') {
    100          
    50          
    0          
82 0 0       0 $refHash->{ $key } = ($params->$att)?1:0;
83             } elsif ($self->_is_internal_type($att_type)) {
84 157         12865 $refHash->{ $key } = $params->$att;
85             } elsif ($att_type =~ m/^ArrayRef\[(.*)\]/) {
86 5 100       2851 if ($self->_is_internal_type("$1")){
87 1         59 $refHash->{ $key } = $params->$att;
88             } else {
89 4         12 $refHash->{ $key } = [ map { $self->to_hash($_) } @{ $params->$att } ];
  8         39  
  4         181  
90             }
91             } elsif ($att_type->isa('Moose::Meta::TypeConstraint::Enum')) {
92 0         0 $refHash->{ $key } = $params->$att;
93             } else {
94 0         0 $refHash->{ $key } = $self->to_hash($params->$att);
95             }
96             }
97             }
98 98         1692 return $refHash;
99             }
100              
101             sub response_to_object {
102 386     386 0 1751 my ($self, $call_object, $http_status, $content, $headers) = @_;
103              
104 386         2586 $call_object = $call_object->meta->name;
105              
106 386   66     26507 my $returns = (defined $call_object->_returns) && ($call_object->_returns ne 'Paws::API::Response');
107 386 100       12024 my $ret_class = $returns ? $call_object->_returns : 'Paws::API::Response';
108 386         3354 Paws->load_class($ret_class);
109            
110 386         1403 my $unserialized_struct;
111              
112 386 100       5597 if ($ret_class->can('_stream_param')) {
113 2         8 $unserialized_struct = {}
114             } else {
115 384 100 100     3547 if (not defined $content or $content eq '') {
116 27         83 $unserialized_struct = {}
117             } else {
118 357         1226 $unserialized_struct = eval { $self->unserialize_response( $content ) };
  357         2690  
119 357 100       20203552 if ($@){
120 7         180 return Paws::Exception->new(
121             message => $@,
122             code => 'InvalidContent',
123             request_id => '', #$request_id,
124             http_status => $http_status,
125             );
126             }
127             }
128             }
129              
130             my $request_id = $headers->{'x-amz-request-id'}
131             || $headers->{'x-amzn-requestid'}
132             || $unserialized_struct->{'requestId'}
133             || $unserialized_struct->{'RequestId'}
134             || $unserialized_struct->{'RequestID'}
135 379   66     6454 || $unserialized_struct->{ ResponseMetadata }->{ RequestId };
136            
137 379 100       19947 if ($call_object->_result_key){
138 155         4811 $unserialized_struct = $unserialized_struct->{ $call_object->_result_key };
139             }
140              
141 379         1954 $unserialized_struct->{ _request_id } = $request_id;
142            
143 379 100       1561 if ($returns){
144 349 100       3571 if ($ret_class->can('_stream_param')) {
145 2         52 $unserialized_struct->{ $ret_class->_stream_param } = $content
146             }
147              
148 349         1980 foreach my $key (keys %$headers){
149 529         1749 $unserialized_struct->{lc $key} = $headers->{$key};
150             }
151              
152 349         10944 my $o_result = $self->new_from_result_struct($call_object->_returns, $unserialized_struct);
153 343         518940 return $o_result;
154             } else {
155 30         220 return Paws::API::Response->new(
156             _request_id => $request_id,
157             );
158             }
159             }
160              
161             1;