blib/lib/Webservice/OVH.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 138 SCAR 24.8
CC
3
Cov
0%
new_from_json
6.0
new
6.0
set_timeout
2.0
CRAP 12.0
1 package Webservice::OVH;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Webservice::OVH - A perl representation of the ovh-api
8  
9 =head1 SYNOPSIS
10  
11 use Webservice::OVH;
12  
13 my $ovh = Webservice::OVH->new("credentials.json");
14  
15 my $ovh = Webservice::OVH->new(application_key => $key, application_secret => $secret, consumer_key => $token);
16  
17 my $services = $ovh->domain->services;
18  
19 foreach my $service (@$services) {
20
21 my $last_update = $service->last_update;
22 print $last_update->datetime;
23 }
24  
25 =head1 DESCRIPTION
26  
27 This module reflects the path structure of the ovh web-api.
28 This is the base object from where all api calls originate.
29  
30 This module uses the perl api module provided by ovh.
31  
32 =begin html
33  
34 <p><center><img src="https://raw.githubusercontent.com/itnode/Webservice-OVH/master/inc/API_HowTo.png"></center></p>
35  
36 =end html
37  
38  
39 =head1 METHODS
40  
41 =cut
42  
43 use strict;
44 use warnings;
45 use Carp qw{ carp croak };
46  
47 our $VERSION = 0.47;
48  
49 # api module provided by ovh
50 use OVH::OvhApi;
51  
52 # sub-modules
53 use Webservice::OVH::Domain;
54 use Webservice::OVH::Me;
55 use Webservice::OVH::Order;
56 use Webservice::OVH::Email;
57 use Webservice::OVH::Cloud;
58 use Webservice::OVH::Hosting;
59  
60 # other requirements
61 use JSON;
62 use File::Slurp qw(read_file);
63  
64 =head2 new_from_json
65  
66 Creates an api Object based on credentials in a json File
67  
68 =over
69  
70 =item * Parameter: $file_json - dir to json file
71  
72 =item * Return: L<Webservice::OVH>
73  
74 =item * Synopsis: Webservice::OVH->new_from_json("path/file");
75  
76 =back
77  
78 =over 2
79  
80 =item * application_key is generated when creating an application via ovh web interface
81  
82 =item * application_secret is generated when creating an application via ovh web interface
83  
84 =item * consumer_key must be requested through ovh authentification
85  
86 =item * timeout timeout in milliseconds, warning some request may take a while
87  
88 =back
89  
90 =cut
91  
92 sub new_from_json {
93  
94 my ( $class, $file_json ) = @_;
95  
96 # slurp file
97 my $json = read_file($file_json, { binmode => ':raw' });
98  
99 # decode json
100 my $Json = JSON->new->allow_nonref;
101 my $data = $Json->decode($json);
102  
103 # check for missing parameters in the json file
104 my @keys = qw{ application_key application_secret consumer_key };
105 if ( my @missing_parameters = grep { not $data->{$_} } @keys ) {
106  
107 croak "Missing parameter: @missing_parameters";
108 }
109  
110 my $self = bless {}, $class;
111  
112 # Create internal objects to mirror the web api of ovh
113 my $api_wrapper = OVH::OvhApi->new( 'type' => "https://eu.api.ovh.com/1.0", applicationKey => $data->{application_key}, applicationSecret => $data->{application_secret}, consumerKey => $data->{consumer_key} );
114 my $domain = Webservice::OVH::Domain->_new( wrapper => $api_wrapper, module => $self );
115 my $me = Webservice::OVH::Me->_new( wrapper => $api_wrapper, module => $self );
116 my $order = Webservice::OVH::Order->_new( wrapper => $api_wrapper, module => $self );
117 my $email = Webservice::OVH::Email->_new( wrapper => $api_wrapper, module => $self );
118 my $cloud = Webservice::OVH::Cloud->_new( wrapper => $api_wrapper, module => $self );
119 my $hosting = Webservice::OVH::Hosting->_new( wrapper => $api_wrapper, module => $self );
120  
121 # Timeout can be also set in the json file
122 OVH::OvhApi->setRequestTimeout( timeout => $data->{timeout} || 120 );
123  
124 # Setting private variables
125 $self->{_domain} = $domain;
126 $self->{_me} = $me;
127 $self->{_order} = $order;
128 $self->{_api_wrapper} = $api_wrapper;
129 $self->{_email} = $email;
130 $self->{_cloud} = $cloud;
131 $self->{_hosting} = $hosting;
132  
133 return $self;
134 }
135  
136 =head2 new
137  
138 Create the api object. Credentials are given directly via %params
139 Credentials can be generated via ovh web interface and ovh authentification
140  
141 =over
142  
143 =item * Parameter: %params - application_key => value, application_secret => value, consumer_key => value
144  
145 =item * Return: L<Webservice::OVH>
146  
147 =item * Synopsis: Webservice::OVH->new(application_key => $key, application_secret => $secret, consumer_key => $token);
148  
149 =back
150  
151 =cut
152  
153 sub new {
154  
155 my ( $class, %params ) = @_;
156  
157 my @keys = qw{ application_key application_secret consumer_key };
158  
159 if ( my @missing_parameters = grep { not $params{$_} } @keys ) {
160  
161 croak "Missing parameter: @missing_parameters";
162 }
163  
164 my $self = bless {}, $class;
165  
166 my $api_wrapper = OVH::OvhApi->new( 'type' => "https://eu.api.ovh.com/1.0", applicationKey => $params{application_key}, applicationSecret => $params{application_secret}, consumerKey => $params{consumer_key} );
167 my $domain = Webservice::OVH::Domain->_new( wrapper => $api_wrapper, module => $self );
168 my $me = Webservice::OVH::Me->_new( wrapper => $api_wrapper, module => $self );
169 my $order = Webservice::OVH::Order->_new( wrapper => $api_wrapper, module => $self );
170 my $email = Webservice::OVH::Email->_new( wrapper => $api_wrapper, module => $self );
171 my $cloud = Webservice::OVH::Cloud->_new( wrapper => $api_wrapper, module => $self );
172 my $hosting = Webservice::OVH::Hosting->_new( wrapper => $api_wrapper, module => $self );
173  
174 OVH::OvhApi->setRequestTimeout( timeout => $params{timeout} || 120 );
175  
176 $self->{_domain} = $domain;
177 $self->{_me} = $me;
178 $self->{_order} = $order;
179 $self->{_api_wrapper} = $api_wrapper;
180 $self->{_email} = $email;
181 $self->{_cloud} = $cloud;
182 $self->{_hosting} = $hosting;
183  
184 return $self;
185 }
186  
187 =head2 set_timeout
188  
189 Sets the timeout of the underlying LWP::Agent
190  
191 =over
192  
193 =item * Parameter: timeout - in milliseconds default 120
194  
195 =item * Synopsis: Webservice::OVH->set_timeout(120);
196  
197 =back
198  
199 =cut
200  
201 sub set_timeout {
202  
203 my ( $class, $timeout ) = @_;
204  
205 OVH::OvhApi->setRequestTimeout( timeout => $timeout );
206 }
207  
208 =head2 domain
209  
210 Main access to all /domain/ api methods
211  
212 =over
213  
214 =item * Return: L<Webservice::OVH::Domain>
215  
216 =item * Synopsis: $ovh->domain;
217  
218 =back
219  
220 =cut
221  
222 sub domain {
223  
224 my ($self) = @_;
225  
226 return $self->{_domain};
227 }
228  
229 =head2 me
230  
231 Main access to all /me/ api methods
232  
233 =over
234  
235 =item * Return: L<Webservice::OVH::Me>
236  
237 =item * Synopsis: $ovh->me;
238  
239 =back
240  
241 =cut
242  
243 sub me {
244  
245 my ($self) = @_;
246  
247 return $self->{_me};
248 }
249  
250 =head2 order
251  
252 Main access to all /order/ api methods
253  
254 =over
255  
256 =item * Return: L<Webservice::OVH::Order>
257  
258 =item * Synopsis: $ovh->order;
259  
260 =back
261  
262 =cut
263  
264 sub order {
265  
266 my ($self) = @_;
267  
268 return $self->{_order};
269 }
270  
271 =head2 email
272  
273 Main access to all /email/ api methods
274  
275 =over
276  
277 =item * Return: L<Webservice::OVH::Email>
278  
279 =item * Synopsis: $ovh->email;
280  
281 =back
282  
283 =cut
284  
285 sub email {
286  
287 my ($self) = @_;
288  
289 return $self->{_email};
290 }
291  
292 =head2 cloud
293  
294 Main access to all /cloud/ api methods
295  
296 =over
297  
298 =item * Return: L<Webservice::OVH::Cloud>
299  
300 =item * Synopsis: $ovh->cloud;
301  
302 =back
303  
304 =cut
305  
306 sub cloud {
307  
308 my ($self) = @_;
309  
310 return $self->{_cloud};
311 }
312  
313 =head2 hosting
314  
315 Main access to all /hosting/ api methods
316  
317 =over
318  
319 =item * Return: L<Webservice::OVH::Cloud>
320  
321 =item * Synopsis: $ovh->cloud;
322  
323 =back
324  
325 =cut
326  
327 sub hosting {
328  
329 my ($self) = @_;
330  
331 return $self->{_hosting};
332 }
333  
334 =head1 AUTHOR
335  
336 Patrick Jendral
337  
338 =head1 COPYRIGHT AND LICENSE
339  
340 This library is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
341  
342 =cut
343  
344 1;