File Coverage

blib/lib/CloudHealth/API/CallObjectFormer.pm
Criterion Covered Total %
statement 65 65 100.0
branch 17 22 77.2
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 90 97 92.7


line stmt bran cond sub pod time code
1             package CloudHealth::API::CallObjectFormer;
2 2     2   456 use Moo;
  2         5  
  2         14  
3 2     2   2123 use HTTP::Tiny;
  2         99491  
  2         83  
4 2     2   485 use JSON::MaybeXS;
  2         5610  
  2         170  
5 2     2   16 use Module::Runtime qw/require_module/;
  2         5  
  2         19  
6 2     2   1098 use CloudHealth::API::Error;
  2         9  
  2         72  
7 2     2   948 use CloudHealth::API::HTTPRequest;
  2         7  
  2         1242  
8              
9             has _json => (is => 'ro', default => sub { JSON::MaybeXS->new });
10              
11             sub callinfo_class {
12 10     10 0 28 my ($self, $call) = @_;
13 10         30 my $class = "CloudHealth::API::Call::$call";
14 10         46 require_module $class;
15 10         314 return $class;
16             }
17              
18             sub params2request {
19 10     10 0 23774 my ($self, $call, $creds, $user_params) = @_;
20              
21 10         24 my $call_object = eval { $self->callinfo_class($call)->new(@$user_params) };
  10         75  
22 10 100       25365 if ($@) {
23 3         8 my $msg = $@;
24 3         48 CloudHealth::API::Error->throw(
25             type => 'InvalidParameters',
26             message => "Error in parameters to method $call",
27             detail => $msg,
28             );
29             }
30              
31 7         18 my $body_struct;
32 7 100       48 if ($call_object->can('_body_params')) {
33 2         6 $body_struct = {};
34 2         5 foreach my $param (@{ $call_object->_body_params }) {
  2         9  
35 10         17 my $key = $param->{ name };
36 10         29 my $value = $call_object->$key;
37 10 100       24 next if (not defined $value);
38              
39 5 50       15 my $location = defined $param->{ location } ? $param->{ location } : $key;
40 5         12 $body_struct->{ $location } = $value;
41             }
42             }
43              
44             CloudHealth::API::Error->throw(
45 7 50       177 type => 'NoCredentials',
46             message => 'Cannot find credentials for the request'
47             ) if (not $creds->is_set);
48              
49 7         115 my $params = {
50             api_key => $creds->api_key,
51             };
52 7         17 foreach my $param (@{ $call_object->_query_params }) {
  7         27  
53 19         35 my $key = $param->{ name };
54 19         41 my $value = $call_object->$key;
55 19 100       45 next if (not defined $value);
56              
57 7 50       18 my $location = defined $param->{ location } ? $param->{ location } : $key;
58 7         18 $params->{ $location } = $value;
59             }
60              
61 7         25 my $url_params = {};
62 7         13 foreach my $param (@{ $call_object->_url_params }) {
  7         23  
63 1         4 my $key = $param->{ name };
64 1         5 my $value = $call_object->$key;
65 1 50       5 next if (not defined $value);
66              
67 1 50       5 my $location = defined $param->{ location } ? $param->{ location } : $key;
68 1         3 $url_params->{ $location } = $value;
69             }
70 7         29 my $url = $call_object->_url;
71 7         32 $url =~ s/\:([a-z0-9_-]+)/$url_params->{ $1 }/ge;
  1         5  
72              
73 7         42 my $qstring = HTTP::Tiny->www_form_urlencode($params);
74 7         733 my $req = CloudHealth::API::HTTPRequest->new;
75 7         3433 $req->method($call_object->_method);
76 7         363 $req->url(
77             "$url?$qstring",
78             );
79 7 100       357 $req->headers({
80             (defined $body_struct) ? ('Content-Type' => 'application/json') : (),
81             Accept => 'application/json',
82             });
83 7 100       277 $req->content($self->_json->encode($body_struct)) if (defined $body_struct);
84              
85 7         132 return $req;
86             }
87             1;