« Webservice/OVH/Email/Domain/Domain/Task.pm
↑ Summary
Webservice/OVH/Email/Domain/Domain/Account.pm »
| 1 | package OVH::OvhApi; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 36 | our $VERSION = 0.48; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 36 | ▶ | use OVH::OvhApi::Answer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 36 | ▶ | use Carp qw{ carp croak }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 36 | ▶ | use List::Util 'first'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 36 | ▶ | use LWP::UserAgent (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 36 | ▶ | use JSON (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 36 | ▶ | use Digest::SHA1 'sha1_hex'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||||||||||||||||||||||||||||
| 20 | # Class constants | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 36 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 36 | OVH_API_EU => 'https://eu.api.ovh.com/1.0', | |||||||||||||||||||||||||||
| 24 | OVH_API_CA => 'https://ca.api.ovh.com/1.0', | ||||||||||||||||||||||||||||
| 25 | 36 | }; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # End - Class constants | ||||||||||||||||||||||||||||
| 28 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | ||||||||||||||||||||||||||||
| 33 | # Class variables | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 36 | my $UserAgent = LWP::UserAgent->new(timeout => 10); | |||||||||||||||||||||||||||
| 36 | 36 | my $Json = JSON->new->allow_nonref; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 36 | 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$params{"type"} and grep {$params{"type"} eq $_;} "https://eu.api.ovh.com/1.0", "https://ca.api.ovh.com/1.0"
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"
| |||||||||||||||||||||||||||||
| 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'
MC/DC0%0/1
$params{'type'} || 'https://eu.api.ovh.com/1.0'
$params{'type'}
Truth table0%0/2
A
A= $params{'type'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$method ne "get" and $method ne "delete"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 OVH::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
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | croak 'Invalid parameter: accessRules' if ref $params{'accessRules'} ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | my @rules = map { | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | croak 'Invalid access rule: must be HASH ref' if ref ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$rule{"method"} and $rule{"path"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($rule{'method'} eq 'ALL') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 36 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 36 | 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
| |||||||||||||||||||||||||||||
| 191 | { | ||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if (my $ServerTimeResponse = $self->get(path => 'auth/time', noSignature => 1)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 36 | return 42; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||