File Coverage

blib/lib/Kubernetes/REST/ListToRequest.pm
Criterion Covered Total %
statement 58 68 85.2
branch 17 26 65.3
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 83 104 79.8


line stmt bran cond sub pod time code
1             package Kubernetes::REST::ListToRequest;
2 2     2   1589 use Moo;
  2         4  
  2         12  
3 2     2   1970 use HTTP::Tiny;
  2         99267  
  2         84  
4 2     2   440 use JSON::MaybeXS;
  2         5511  
  2         126  
5 2     2   409 use Kubernetes::REST::Error;
  2         6  
  2         54  
6 2     2   878 use Kubernetes::REST::HTTPRequest;
  2         7  
  2         71  
7 2     2   16 use Module::Runtime qw/require_module/;
  2         4  
  2         15  
8              
9             has _json => (is => 'ro', default => sub { JSON::MaybeXS->new });
10              
11             sub callinfo_class {
12 2     2 0 6 my ($self, $call) = @_;
13 2         8 my $class = "Kubernetes::REST::Call::$call";
14 2         11 require_module($class);
15 2         8 return $class;
16             }
17              
18             sub params2request {
19 2     2 0 7422 my ($self, $call_ctx) = @_;
20              
21 2         8 my $call = $call_ctx->method;
22              
23 2         5 my $call_object = eval { $self->callinfo_class($call)->new(@{ $call_ctx->params }) };
  2         7  
  2         19  
24 2 50       2944 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 2         5 my $body_struct;
34 2 100       13 if ($call_object->can('_body_params')) {
35 1         3 foreach my $param (@{ $call_object->_body_params }) {
  1         5  
36 1         3 my $key = $param->{ name };
37 1         4 my $value = $call_object->$key;
38 1 50       8 next if (not defined $value);
39              
40 1         3 $body_struct = $value;
41             }
42             }
43              
44 2         7 my $params;
45 2 100       10 if ($call_object->can('_query_params')) {
46 1         3 $params = {};
47 1         2 foreach my $param (@{ $call_object->_query_params }) {
  1         4  
48 3         5 my $key = $param->{ name };
49 3         8 my $value = $call_object->$key;
50 3 50       9 next if (not defined $value);
51            
52 0 0       0 my $location = defined $param->{ location } ? $param->{ location } : $key;
53 0         0 $params->{ $location } = $value;
54             }
55             }
56              
57 2         8 my $url = $call_object->_url;
58 2         4 my $url_params;
59 2 100       10 if ($call_object->can('_url_params')) {
60 1         2 $url_params = {};
61 1         2 foreach my $param (@{ $call_object->_url_params }) {
  1         5  
62 0         0 my $key = $param->{ name };
63 0         0 my $value = $call_object->$key;
64 0 0       0 next if (not defined $value);
65            
66 0 0       0 my $location = defined $param->{ location } ? $param->{ location } : $key;
67 0         0 $url_params->{ $location } = $value;
68             }
69 1         3 $url =~ s/\{([a-z0-9_-]+)\}/$url_params->{ $1 }/ge;
  0         0  
70             }
71 2 100       12 my $qstring = HTTP::Tiny->www_form_urlencode($params) if (defined $params);
72              
73 2         62 my $req = Kubernetes::REST::HTTPRequest->new(
74             server => $call_ctx->server,
75             credentials => $call_ctx->credentials,
76             );
77 2         59 $req->method($call_object->_method);
78 2 100       102 $req->uri((defined $qstring) ? "${url}?$qstring" : "${url}");
79 2 100       93 $req->headers({
80             (defined $body_struct) ? ('Content-Type' => 'application/json') : (),
81             Accept => 'application/json',
82             });
83 2 100       82 $req->content($self->_json->encode($body_struct)) if (defined $body_struct);
84              
85 2         49 return $req;
86             }
87              
88             1;