File Coverage

blib/lib/Paws/Net/RestJsonCaller.pm
Criterion Covered Total %
statement 70 85 82.3
branch 27 38 71.0
condition 5 12 41.6
subroutine 9 9 100.0
pod 0 1 0.0
total 111 145 76.5


line stmt bran cond sub pod time code
1             package Paws::Net::RestJsonCaller;
2 7     7   58431 use Moose::Role;
  7         18  
  7         70  
3 7     7   36803 use HTTP::Request::Common;
  7         7308  
  7         572  
4 7     7   45 use POSIX qw(strftime);
  7         15  
  7         62  
5 7     7   506 use URI::Template;
  7         15  
  7         183  
6 7     7   37 use JSON::MaybeXS;
  7         13  
  7         6286  
7              
8             # converts the objects that represent the call into parameters that the API can understand
9             sub _to_jsoncaller_params {
10 6     6   16 my ($self, $params) = @_;
11 6         15 my %p;
12 6         24 foreach my $att (grep { $_ !~ m/^_/ } $params->meta->get_attribute_list) {
  33         222  
13 33         97 my $attribute = $params->meta->get_attribute($att);
14              
15 33 50 33     837 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 33 100       15720 my $key = $attribute->does('Paws::API::Attribute::Trait::NameInRequest')?$attribute->request_name:$att;
22 33 100       2419 if (defined $params->$att) {
23 17         465 my $att_type = $attribute->type_constraint;
24 17 50       149 if ($att_type eq 'Bool') {
    50          
    100          
    50          
    100          
    50          
    100          
    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 11         1165 $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 3 100       523 if ($self->_is_internal_type("$1")){
35 2         51 $p{ $key } = $params->$att;
36             } else {
37 1         3 $p{ $key } = [ map { $self->_to_jsoncaller_params($_) } @{ $params->$att } ];
  1         6  
  1         23  
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 1         191 $p{ $key } = { %{ $params->$att->Map } };
  1         24  
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         428 $p{ $key } = $self->_to_jsoncaller_params($params->$att);
52             }
53             }
54             }
55 6         28 return \%p;
56             }
57              
58             sub _call_uri {
59 5     5   15 my ($self, $call) = @_;
60 5         20 my $uri_template = $call->meta->name->_api_uri;
61 5         51 my $t = URI::Template->new( $uri_template );
62              
63 5         258 my $vars = {};
64              
65 5         20 foreach my $attribute ($call->meta->get_all_attributes) {
66 35         4852 my $att_name = $attribute->name;
67 35 100       87 if ($attribute->does('Paws::API::Attribute::Trait::ParamInURI')) {
68 1         223 $vars->{ $attribute->uri_name } = $call->$att_name
69             }
70             }
71              
72 5         982 my $uri = $t->process($vars);
73 5         617 return $uri;
74             }
75              
76             sub _to_header_params {
77 5     5   16 my ($self, $request, $call) = @_;
78 5         18 foreach my $attribute ($call->meta->get_all_attributes) {
79 35 100 66     4522 if ($attribute->does('Paws::API::Attribute::Trait::ParamInHeader') and $attribute->has_value($call)) {
80 3         811 $request->headers->header( $attribute->header_name => $attribute->get_value($call) );
81             }
82             }
83             }
84              
85             sub prepare_request_for_call {
86 5     5 0 58 my ($self, $call) = @_;
87              
88 5         47 my $request = Paws::Net::APIRequest->new();
89              
90 5         3065 my $uri = $self->_call_uri($call);
91              
92 5         27 my $qparams = { $uri->query_form };
93 5         145 foreach my $attribute ($call->meta->get_all_attributes) {
94 35         4158 my $att_name = $attribute->name;
95 35 100       83 if ($attribute->does('Paws::API::Attribute::Trait::ParamInQuery')) {
96 3 50       686 $qparams->{ $attribute->query_name } = $call->$att_name if (defined $call->$att_name);
97             }
98             }
99 5         995 $uri->query_form(%$qparams);
100              
101 5         235 $request->uri($uri->as_string);
102 5         134 my $url = $self->_api_endpoint . $uri->as_string;
103 5         152 $request->url($url);
104              
105 5         25 $self->_to_header_params($request, $call);
106            
107 5 100       1037 if ($call->can('_stream_param')) {
108 2         53 my $param_name = $call->_stream_param;
109 2         77 $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 3         20 my $data = $self->_to_jsoncaller_params($call);
114 3         113 $request->content(encode_json($data));
115             }
116            
117 5         143 $request->method($call->_api_method);
118              
119 5         38 $self->sign($request);
120              
121 5         182 return $request;
122             }
123             1;