OvhApi.pm

filter: Statementstmt 41.7% 38 / 91 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 58.8% 10 / 17 Podpod 75.0% 3 / 4 totaltotal 31.8% 51 / 160 CC 20 SCAR 32.8
CC
20
Cov
29%
requestCredentials
72.0
new
30.0
rawCall
30.0
CRAP 26.7
↑ Summary OvhApi/Answer.pm »
1 package OvhApi;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = 0.2;
7  
8  
9 1 ▶use OvhApi::Answer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Carp qw{ carp croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Util 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Digest::SHA1 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18  
19 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
20 # Class constants
21  
22 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 OVH_API_EU => 'https://eu.api.ovh.com/1.0',
24 OVH_API_CA => 'https://ca.api.ovh.com/1.0',
25 1 };
26  
27 # End - Class constants
28 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
29  
30  
31  
32 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
33 # Class variables
34  
35 1 my $UserAgent = LWP::UserAgent->new(timeout => 10);
36 1 my $Json = JSON->new->allow_nonref;
37  
38 1 my @accessRuleMethods = qw{ GET POST PUT DELETE };
39  
40 # End - Class variables
41 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
42  
43  
44  
45 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
46 # Class methods
47  
48 sub new
49 0 ▶{
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
50 0 my @keys = qw{ applicationKey applicationSecret consumerKey };
51  
52 0 my ($class, %params) = @_;
53  
54 0 ▶ if (my @missingParameters = grep { not $params{$_} } qw{ applicationKey applicationSecret })
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missingParameters) = grep({not $params{$_};} "applicationKey", "applicationSecret")) 0 0 0
55 {
56 0 local $" = ', ';
57 0 croak "Missing parameter: @missingParameters";
58 }
59  
60 0 ▶ unless ($params{'type'} and grep { $params{'type'} eq $_ } (OVH_API_EU, OVH_API_CA))
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"type"} and grep {$params{"type"} eq $_;} "https://eu.api.ovh.com/1.0", "https://ca.api.ovh.com/1.0") 0 0 0
Condition0%0/3
$params{"type"} and grep {$params{"type"} eq $_;} "https://eu.api.ovh.com/1.0", "https://ca.api.ovh.com/1.0"
!ll&&!rl&&r
000
MC/DC0%0/2
$params{"type"} and grep {$params{"type"} eq $_;} "https://eu.api.ovh.com/1.0", "https://ca.api.ovh.com/1.0"
$params{"type"} grep {$params{"type"} eq $_;} "https://eu.api.ovh.com/1.0", "https://ca.api.ovh.com/1.0"
Truth table0%0/3
A and B
A= $params{"type"}B= grep {$params{"type"} eq $_;} "https://eu.api.ovh.com/1.0", "https://ca.api.ovh.com/1.0"
ABresult
0X0
100
111
61 {
62 0 carp 'Missing or invalid type parameter: defaulting to OVH_API_EU';
63 }
64  
65 my $self = {
66 0 ▶ _type => ($params{'type'} or OVH_API_EU),
Condition0%0/2
$params{'type'} || 'https://eu.api.ovh.com/1.0'
l!l
00
MC/DC0%0/1
$params{'type'} || 'https://eu.api.ovh.com/1.0'
$params{'type'}
Truth table0%0/2
A
A= $params{'type'}
Aresult
11
00
67 };
68  
69 0 @$self{@keys} = @params{@keys};
70  
71 0 bless $self, $class;
72 }
73  
74 sub setRequestTimeout
75 0 ▶{
Subroutine setRequestTimeout: not called CC=3 SCAR=24.8
Pod documented
76 0 my ($class, %params) = @_;
77  
78 0 ▶ if ($params{'timeout'} =~ /^\d+$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($params{'timeout'} =~ /^\d+$/) { } 0 0 0
elsif (exists $params{'timeout'}) { } 0 0 0
79 {
80 0 $UserAgent->timeout($params{'timeout'});
81 }
82 elsif (exists $params{'timeout'})
83 {
84 0 carp "Invalid timeout: $params{'timeout'}";
85 }
86 else
87 {
88 0 carp 'Missing parameter: timeout';
89 }
90 }
91  
92 # End - Class methods
93 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
94  
95  
96  
97 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
98 # Instance methods
99  
100 sub rawCall
101 0 ▶{
Subroutine rawCall: not called CC=5 SCAR=34.0
Pod documented
102 0 my ($self, %params) = @_;
103  
104 0 my $method = lc $params{'method'};
105 0 ▶ my $url = $self->{'_type'} . (substr($params{'path'}, 0, 1) eq '/' ? '' : '/') . $params{'path'};
Branch: 1 branch
BranchTrueFalseTotal
substr($params{'path'}, 0, 1) eq '/' ? : 0 0 0
106  
107 0 my %httpHeaders;
108  
109 0 my $body = '';
110 0 my %content;
111  
112 0 ▶ if ($method ne 'get' and $method ne 'delete')
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "get" and $method ne "delete") 0 0 0
Condition0%0/3
$method ne "get" and $method ne "delete"
!ll&&!rl&&r
000
MC/DC0%0/2
$method ne "get" and $method ne "delete"
$method ne "get" $method ne "delete"
Truth table0%0/3
A and B
A= $method ne "get"B= $method ne "delete"
ABresult
0X0
100
111
113 {
114 0 $body = $Json->encode($params{'body'});
115  
116 0 $httpHeaders{'Content-type'} = 'application/json';
117 0 $content{'Content'} = $body;
118 }
119  
120 0 ▶ unless ($params{'noSignature'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"noSignature"}) 0 0 0
121 {
122 0 my $now = $self->_timeDelta + time;
123  
124 $httpHeaders{'X-Ovh-Consumer'} = $self->{'consumerKey'},
125 $httpHeaders{'X-Ovh-Timestamp'} = $now,
126 $httpHeaders{'X-Ovh-Signature'} = '$1$' . sha1_hex(join('+', (
127 # Full signature is '$1$' followed by the hex digest of the SHA1 of all these data joined by a + sign
128 $self->{'applicationSecret'}, # Application secret
129 0 $self->{'consumerKey'}, # Consumer key
130 uc $method, # HTTP method (uppercased)
131 $url, # Full URL
132 $body, # Full body
133 $now, # Curent OVH server time
134 )));
135 }
136  
137 0 $httpHeaders{'X-Ovh-Application'} = $self->{'applicationKey'},
138  
139 return OvhApi::Answer->new(response => $UserAgent->$method($url, %httpHeaders, %content));
140 }
141  
142 sub requestCredentials
143 0 ▶{
Subroutine requestCredentials: not called CC=8 SCAR=42.8
Pod documented
144 0 my ($self, %params) = @_;
145  
146 0 ▶ croak 'Missing parameter: accessRules' unless $params{'accessRules'};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"accessRules"} 0 0 0
147 0 ▶ croak 'Invalid parameter: accessRules' if ref $params{'accessRules'} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $params{"accessRules"} ne "ARRAY" 0 0 0
148  
149 my @rules = map {
150 0 ▶ croak 'Invalid access rule: must be HASH ref' if ref ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ ne "HASH" 0 0 0
151  
152 0 my %rule = %$_;
153  
154 0 $rule{'method'} = uc $rule{'method'};
155  
156 0 ▶ croak 'Access rule must have method and path keys' unless $rule{'method'} and $rule{'path'};
Branch: 1 branch
BranchTrueFalseTotal
unless $rule{"method"} and $rule{"path"} 0 0 0
Condition0%0/3
$rule{"method"} and $rule{"path"}
!ll&&!rl&&r
000
MC/DC0%0/2
$rule{"method"} and $rule{"path"}
$rule{"method"} $rule{"path"}
Truth table0%0/3
A and B
A= $rule{"method"}B= $rule{"path"}
ABresult
0X0
100
111
157 0 ▶ croak 'Invalid access rule method' unless first { $_ eq $rule{'method'} } (@accessRuleMethods, 'ALL');
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $rule{"method"}; } , @accessRuleMethods, "ALL") 0 0 0
158  
159 0 ▶ if ($rule{'method'} eq 'ALL')
Branch: 1 branch
BranchTrueFalseTotal
if ($rule{'method'} eq 'ALL') { } 0 0 0
160 {
161 0 map { path => $rule{'path'}, method => $_ }, @accessRuleMethods;
162 }
163 else
164 {
165 0 \%rule
166 }
167 0 } @{ $params{'accessRules'} };
168  
169 0 return $self->post(path => '/auth/credential/', noSignature => 1, body => { accessRules => \@rules });
170 }
171  
172 # Generation of helper subs: simple wrappers to rawCall
173 # Generate: get(), post(), put(), delete()
174 {
175 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 1 for my $method (qw{ get post put delete })
178 {
179 0 ▶ *$method = sub { rawCall(@_, 'method', $method ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
180 }
181 }
182  
183 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
184 # Private part
185  
186 sub _timeDelta
187 0 ▶{
Subroutine _timeDelta: not called CC=3 SCAR=24.8
188 0 my ($self, %params) = @_;
189  
190 0 ▶ unless (defined $self->{'_timeDelta'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_timeDelta"}) 0 0 0
191 {
192 0 ▶ if (my $ServerTimeResponse = $self->get(path => 'auth/time', noSignature => 1))
Branch: 1 branch
BranchTrueFalseTotal
if (my $ServerTimeResponse = $self->get('path', 'auth/time', 'noSignature', 1)) { } 0 0 0
193 {
194 0 $self->{'_timeDelta'} = ($ServerTimeResponse->content - time);
195 }
196 else
197 {
198 0 return 0;
199 }
200 }
201  
202 0 return $self->{'_timeDelta'};
203 }
204  
205 # End - Instance methods
206 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
207  
208  
209 1 return 42;
210  
211  
212 __END__
↑ Summary OvhApi/Answer.pm »