MetaAPI/API/Service.pm

filter: Statementstmt 100% 66 / 66 Branchbran 65.3% 17 / 26 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 82.5% 99 / 120 CC 16 SCAR 14.8
CC
16
Cov
88%
root_uri
7.9
setup_method
6.7
can_method
3.0
CRAP 4.4
« MetaAPI/API/Images.pm ↑ Summary MetaAPI/API/Network.pm »
1 package OpenStack::MetaAPI::API::Service;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use OpenStack::MetaAPI::API::Specs::Default ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 has 'auth' => (is => 'ro', required => 1);
10 5 has 'name' => (is => 'ro', required => 1);
11 5 has 'region' => (is => 'ro', required => 1);
12  
13 5 has 'interface' => (is => 'ro', default => 'public'); # admin internal or public
14  
15 has 'client' => (
16 is => 'ro',
17 lazy => 1,
18 default => sub {
19 8 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 8 return $self->auth->service(
22 $self->name,
23 region => $self->region,
24 interface => $self->interface);
25 },
26 5 handles => [qw/endpoint get put post delete/]
27  
28 );
29  
30 5 has 'api' => (is => 'ro', required => 1)
31 ; # this is backreference to the mainapi so we can call other services
32  
33 5 has 'api_specs' => (is => 'ro', lazy => 1, default => \&BUILD_api_specs);
34  
35 ## FIXME: this needs a refactor...
36 # idea always strip the version from endpoint so we can add it to the uri later..
37 # this would make uri consistent... and improve root_uri
38 5 has 'version' => ('is' => 'ro', lazy => 1, default => \&BUILD_version);
39 5 has 'version_prefix' => ('is' => 'ro'); # added to very routes [optional]
40  
41 8 ▶has 'methods' => (is => 'ro', default => sub { return {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 sub BUILD {
44 8 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
45  
46 8 $self->api_specs->setup_api_methods_for_service($self);
47  
48 8 return;
49 }
50  
51 sub BUILD_version {
52 5 ▶ my ($self) = @_;
Subroutine BUILD_version: called CC=2 SCAR=6.9
Pod undocumented
53  
54 5 my $url = $self->client->endpoint;
55 5 ▶ if ($url =~ m{/(v[0-9\.]+)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[/(v[0-9\.]+)]) 3 2 5
56 3 return $1;
57 }
58 2 return 'default';
59 }
60  
61 sub BUILD_api_specs { # load specs
62 8 ▶ my ($self) = @_;
Subroutine BUILD_api_specs: called CC=2 SCAR=6.9
Pod undocumented
63  
64 8 my $v = $self->version;
65 8 $v =~ s{\.}{_};
66 8 my $pkg =
67 'OpenStack::MetaAPI::API::Specs::' . ucfirst($self->name) . '::' . $v;
68  
69 8 my $load = eval qq{ require $pkg; 1 };
70 8 ▶ if ($load) {
Branch: 1 branch
BranchTrueFalseTotal
if ($load) 6 2 8
71 6 return $pkg->new();
72 }
73  
74 # default void specs [ undefined ]
75 # we do not have to define all specs for now
76 2 return OpenStack::MetaAPI::API::Specs::Default->new();
77 }
78  
79 sub root_uri {
80 19 ▶ my ($self, $uri) = @_;
Subroutine root_uri: called CC=7 SCAR=20.6
Pod undocumented
81  
82 19 ▶ return unless defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri 0 19 19
83  
84 19 ▶ return $uri if $uri =~ m{^v}; # already contains a version
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ /^v/ 0 19 19
85  
86 # endpoint already contains a version
87 19 ▶ return if $self->endpoint && $self->endpoint =~ m{:[\d]/v}a;
Branch: 1 branch
BranchTrueFalseTotal
if $self->endpoint and $self->endpoint =~ m[:[\d]/v]a 0 19 19
Condition33%1/3
$self->endpoint and $self->endpoint =~ m[:[\d]/v]a
!ll&&!rl&&r
0190
MC/DC0%0/2
$self->endpoint and $self->endpoint =~ m[:[\d]/v]a
$self->endpoint $self->endpoint =~ m[:[\d]/v]a
Truth table33%1/3
A and B
A= $self->endpointB= $self->endpoint =~ m[:[\d]/v]a
ABresult
0X0
100
111
88  
89 # append our prefix to the endpoint
90 19 ▶ if ($self->version_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->version_prefix) 8 11 19
91 8 my $base = $self->version_prefix;
92 8 ▶ $base .= '/' unless $uri =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $uri =~ m[^/] 0 8 8
93 8 $base .= $uri;
94 8 return $base;
95 }
96  
97 11 return $uri;
98 }
99  
100 sub setup_method {
101 30 ▶ my ($self, $name, $sub) = @_;
Subroutine setup_method: called CC=6 SCAR=19.0
Pod undocumented
102  
103 30 ▶ die unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 30 30
104 30 ▶ die "missing name" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 30 30
105 30 ▶ die unless ref $sub eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 30 30
106  
107 30 my $methods = $self->methods();
108 30 ▶ die "Method '$name' already exists" if defined $methods->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $methods->{$name} 0 30 30
109 30 ▶ die "Function '$name' already exists for " . ref($self)
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($name) 0 30 30
110 if $self->can($name);
111  
112 30 $methods->{$name} = $sub;
113  
114 30 return 1;
115 }
116  
117 ### not sure how to overwrite 'can' method to let Moo/Class::XSAccessor see the methods
118 sub can_method {
119 39 ▶ my ($self, $method) = @_; # Not shift, using goto.
Subroutine can_method: called CC=3 SCAR=11.0
Pod undocumented
120  
121 #my $sub = UNIVERSAL::can($self, $method);
122 39 my $sub = $self->can($method);
123  
124 39 ▶ if (not defined $sub && ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub and ref $self) 28 11 39
Condition66%2/3
defined $sub and ref $self
!ll&&!rl&&r
28011
MC/DC50%1/2
defined $sub and ref $self
defined $sub ref $self
Truth table66%2/3
A and B
A= defined $subB= ref $self
ABresult
0X0
100
111
125 28 $sub = $self->methods()->{$method};
126 }
127  
128 39 return $sub; # May be undefined.
129 }
130  
131 5 1;
132  
133 __END__
134  
135 =pod
136  
137 =encoding UTF-8
138  
139 =head1 NAME
140  
141 OpenStack::MetaAPI::API::Service
142  
143 =head1 VERSION
144  
145 version 0.002
146  
147 =head1 AUTHOR
148  
149 Nicolas R <atoomic@cpan.org>
150  
151 =head1 COPYRIGHT AND LICENSE
152  
153 This software is copyright (c) 2019 by cPanel, Inc.
154  
155 This is free software; you can redistribute it and/or modify it under
156 the same terms as the Perl 5 programming language system itself.
157  
158 =cut
« MetaAPI/API/Images.pm ↑ Summary MetaAPI/API/Network.pm »