File Coverage

blib/lib/Kubernetes/REST/ListToRequest.pm
Criterion Covered Total %
statement 42 70 60.0
branch 8 28 28.5
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 58 108 53.7


line stmt bran cond sub pod time code
1             package Kubernetes::REST::ListToRequest;
2 2     2   1729 use Moo;
  2         4  
  2         10  
3 2     2   1870 use HTTP::Tiny;
  2         82780  
  2         73  
4 2     2   374 use JSON::MaybeXS;
  2         4487  
  2         125  
5 2     2   370 use Kubernetes::REST::Error;
  2         5  
  2         45  
6 2     2   792 use Kubernetes::REST::HTTPRequest;
  2         6  
  2         64  
7 2     2   13 use Module::Runtime qw/require_module/;
  2         3  
  2         13  
8              
9             has _json => (is => 'ro', default => sub { JSON::MaybeXS->new });
10              
11             sub callinfo_class {
12 1     1 0 3 my ($self, $call) = @_;
13 1         3 my $class = "Kubernetes::REST::Call::$call";
14 1         4 require_module($class);
15 1         3 return $class;
16             }
17              
18             sub params2request {
19 1     1 0 5713 my ($self, $call_ctx) = @_;
20              
21 1         5 my $call = $call_ctx->method;
22              
23 1         1 my $call_object = eval { $self->callinfo_class($call)->new(@{ $call_ctx->params }) };
  1         3  
  1         12  
24 1 50       33 if ($@) {
25 0         0 my $msg = $@;
26 0         0 Kubernetes::REST::Error->throw(
27             type => 'InvalidParameters',
28             message => "Error in parameters to method $call",
29             detail => $msg,
30             );
31             }
32              
33 1         2 my $body_struct;
34 1 50       7 if ($call_object->can('_body_params')) {
35 0         0 $body_struct = {};
36 0         0 foreach my $param (@{ $call_object->_body_params }) {
  0         0  
37 0         0 my $key = $param->{ name };
38 0         0 my $value = $call_object->$key;
39 0 0       0 next if (not defined $value);
40              
41 0 0       0 my $location = defined $param->{ location } ? $param->{ location } : $key;
42 0         0 $body_struct->{ $location } = $value;
43             }
44             }
45              
46 1         2 my $params;
47 1 50       5 if ($call_object->can('_query_params')) {
48 0         0 $params = {};
49 0         0 foreach my $param (@{ $call_object->_query_params }) {
  0         0  
50 0         0 my $key = $param->{ name };
51 0         0 my $value = $call_object->$key;
52 0 0       0 next if (not defined $value);
53            
54 0 0       0 my $location = defined $param->{ location } ? $param->{ location } : $key;
55 0         0 $params->{ $location } = $value;
56             }
57             }
58              
59 1         3 my $url = $call_object->_url;
60 1         1 my $url_params;
61 1 50       6 if ($call_object->can('_url_params')) {
62 0         0 $url_params = {};
63 0         0 foreach my $param (@{ $call_object->_url_params }) {
  0         0  
64 0         0 my $key = $param->{ name };
65 0         0 my $value = $call_object->$key;
66 0 0       0 next if (not defined $value);
67            
68 0 0       0 my $location = defined $param->{ location } ? $param->{ location } : $key;
69 0         0 $url_params->{ $location } = $value;
70             }
71 0         0 $url =~ s/\{([a-z0-9_-]+)\}/$url_params->{ $1 }/ge;
  0         0  
72             }
73 1 50       3 my $qstring = HTTP::Tiny->www_form_urlencode($params) if (defined $params);
74              
75 1         8 my $req = Kubernetes::REST::HTTPRequest->new(
76             server => $call_ctx->server,
77             credentials => $call_ctx->credentials,
78             );
79 1         24 $req->method($call_object->_method);
80 1 50       42 $req->uri((defined $qstring) ? "${url}?$qstring" : "${url}");
81 1 50       42 $req->headers({
82             (defined $body_struct) ? ('Content-Type' => 'application/json') : (),
83             Accept => 'application/json',
84             });
85 1 50       24 $req->content($self->_json->encode($body_struct)) if (defined $body_struct);
86              
87 1         6 return $req;
88             }
89              
90             1;