File Coverage

blib/lib/Paws/Net/RestJsonCaller.pm
Criterion Covered Total %
statement 65 85 76.4
branch 24 38 63.1
condition 5 12 41.6
subroutine 9 9 100.0
pod 0 1 0.0
total 103 145 71.0


line stmt bran cond sub pod time code
1             package Paws::Net::RestJsonCaller;
2 7     7   42898 use Moose::Role;
  7         23  
  7         102  
3 7     7   36936 use HTTP::Request::Common;
  7         7797  
  7         550  
4 7     7   55 use POSIX qw(strftime);
  7         19  
  7         68  
5 7     7   463 use URI::Template;
  7         17  
  7         162  
6 7     7   33 use JSON::MaybeXS;
  7         19  
  7         5787  
7              
8             # converts the objects that represent the call into parameters that the API can understand
9             sub _to_jsoncaller_params {
10 4     4   11 my ($self, $params) = @_;
11 4         9 my %p;
12 4         21 foreach my $att (grep { $_ !~ m/^_/ } $params->meta->get_attribute_list) {
  23         168  
13 23         79 my $attribute = $params->meta->get_attribute($att);
14              
15 23 50 33     638 next if ($attribute->does('ParamInHeader') or
      33        
      33        
16             $attribute->does('ParamInQuery') or
17             $attribute->does('ParamInURI') or
18             $attribute->does('ParamInBody')
19             );
20              
21 23 50       6524 my $key = $attribute->does('Paws::API::Attribute::Trait::NameInRequest')?$attribute->request_name:$att;
22 23 100       2516 if (defined $params->$att) {
23 11         328 my $att_type = $attribute->type_constraint;
24 11 50       112 if ($att_type eq 'Bool') {
    50          
    100          
    50          
    100          
    50          
    50          
    50          
25 0 0       0 $p{ $key } = ($params->$att)?\1:\0;
26             } elsif ($att_type eq 'Int') {
27 0         0 $p{ $key } = int($params->$att);
28             } elsif ($att_type eq 'Str') {
29             # concatenate an empty string so numbers get transmitted as strings
30 7         773 $p{ $key } = "" . $params->$att;
31             } elsif ($self->_is_internal_type($att_type)) {
32 0         0 $p{ $key } = $params->$att;
33             } elsif ($att_type =~ m/^ArrayRef\[(.*)\]/) {
34 2 50       341 if ($self->_is_internal_type("$1")){
35 2         49 $p{ $key } = $params->$att;
36             } else {
37 0         0 $p{ $key } = [ map { $self->_to_jsoncaller_params($_) } @{ $params->$att } ];
  0         0  
  0         0  
38             }
39             } elsif ($att_type->isa('Moose::Meta::TypeConstraint::Enum')) {
40 0         0 $p{ $key } = $params->$att;
41             } elsif ($params->$att->does('Paws::API::StrToNativeMapParser')){
42 0         0 $p{ $key } = { %{ $params->$att->Map } };
  0         0  
43             } elsif ($params->$att->does('Paws::API::StrToObjMapParser')){
44 0         0 my $type = $params->$att->meta->get_attribute('Map')->type_constraint;
45 0 0       0 if (my ($inner) = ("$type" =~ m/^HashRef\[ArrayRef\[(.*?)\]/)) {
46 0         0 $p{ $key } = { map { my $k = $_; ( $k => [ map { $self->_to_jsoncaller_params($_) } @{$params->$att->Map->{$_} } ] ) } keys %{ $params->$att->Map } };
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
47             } else {
48 0         0 $p{ $key } = { map { $_ => $self->_to_jsoncaller_params($params->$att->Map->{$_}) } keys %{ $params->$att->Map } };
  0         0  
  0         0  
49             }
50             } else {
51 2         459 $p{ $key } = $self->_to_jsoncaller_params($params->$att);
52             }
53             }
54             }
55 4         19 return \%p;
56             }
57              
58             sub _call_uri {
59 4     4   13 my ($self, $call) = @_;
60 4         17 my $uri_template = $call->meta->name->_api_uri;
61 4         41 my $t = URI::Template->new( $uri_template );
62              
63 4         231 my $vars = {};
64              
65 4         16 foreach my $attribute ($call->meta->get_all_attributes) {
66 26         3429 my $att_name = $attribute->name;
67 26 100       85 if ($attribute->does('Paws::API::Attribute::Trait::ParamInURI')) {
68 1         320 $vars->{ $attribute->uri_name } = $call->$att_name
69             }
70             }
71              
72 4         349 my $uri = $t->process($vars);
73 4         480 return $uri;
74             }
75              
76             sub _to_header_params {
77 4     4   13 my ($self, $request, $call) = @_;
78 4         18 foreach my $attribute ($call->meta->get_all_attributes) {
79 26 100 66     3219 if ($attribute->does('Paws::API::Attribute::Trait::ParamInHeader') and $attribute->has_value($call)) {
80 3         847 $request->headers->header( $attribute->header_name => $attribute->get_value($call) );
81             }
82             }
83             }
84              
85             sub prepare_request_for_call {
86 4     4 0 45 my ($self, $call) = @_;
87              
88 4         42 my $request = Paws::Net::APIRequest->new();
89              
90 4         2398 my $uri = $self->_call_uri($call);
91              
92 4         25 my $qparams = { $uri->query_form };
93 4         129 foreach my $attribute ($call->meta->get_all_attributes) {
94 26         2639 my $att_name = $attribute->name;
95 26 100       78 if ($attribute->does('Paws::API::Attribute::Trait::ParamInQuery')) {
96 3 50       792 $qparams->{ $attribute->query_name } = $call->$att_name if (defined $call->$att_name);
97             }
98             }
99 4         233 $uri->query_form(%$qparams);
100              
101 4         223 $request->uri($uri->as_string);
102 4         116 my $url = $self->_api_endpoint . $uri->as_string;
103 4         115 $request->url($url);
104              
105 4         18 $self->_to_header_params($request, $call);
106            
107 4 100       561 if ($call->can('_stream_param')) {
108 2         57 my $param_name = $call->_stream_param;
109 2         47 $request->content($call->$param_name);
110             #$request->headers->header( 'content-length' => $request->content_length );
111             #$request->headers->header( 'content-type' => $self->content_type );
112             } else {
113 2         11 my $data = $self->_to_jsoncaller_params($call);
114 2         73 $request->content(encode_json($data));
115             }
116            
117 4         103 $request->method($call->_api_method);
118              
119 4         32 $self->sign($request);
120              
121 4         162 return $request;
122             }
123             1;