File Coverage

lib/Webservice/OVH.pm
Criterion Covered Total %
statement 36 97 37.1
branch 0 4 0.0
condition 0 4 0.0
subroutine 12 21 57.1
pod 9 9 100.0
total 57 135 42.2


line stmt bran cond sub pod time code
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 36     36   3738300 use strict;
  36         479  
  36         1059  
44 36     36   191 use warnings;
  36         80  
  36         1040  
45 36     36   181 use Carp qw{ carp croak };
  36         72  
  36         2416  
46              
47             our $VERSION = 0.48;
48              
49             # api module provided by ovh
50 36     36   16095 use OVH::OvhApi;
  36         115  
  36         1458  
51              
52             # sub-modules
53 36     36   17452 use Webservice::OVH::Domain;
  36         132  
  36         1439  
54 36     36   16319 use Webservice::OVH::Me;
  36         108  
  36         1170  
55 36     36   15282 use Webservice::OVH::Order;
  36         113  
  36         1714  
56 36     36   14568 use Webservice::OVH::Email;
  36         118  
  36         1235  
57 36     36   16609 use Webservice::OVH::Cloud;
  36         144  
  36         1385  
58 36     36   15405 use Webservice::OVH::Hosting;
  36         120  
  36         1127  
59              
60             # other requirements
61 36     36   244 use JSON;
  36         82  
  36         183  
62 36     36   24856 use File::Slurp qw(read_file);
  36         850444  
  36         30661  
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 0     0 1   my ( $class, $file_json ) = @_;
95              
96             # slurp file
97 0           my $json = read_file($file_json, { binmode => ':raw' });
98              
99             # decode json
100 0           my $Json = JSON->new->allow_nonref;
101 0           my $data = $Json->decode($json);
102              
103             # check for missing parameters in the json file
104 0           my @keys = qw{ application_key application_secret consumer_key };
105 0 0         if ( my @missing_parameters = grep { not $data->{$_} } @keys ) {
  0            
106              
107 0           croak "Missing parameter: @missing_parameters";
108             }
109              
110 0           my $self = bless {}, $class;
111              
112             # Create internal objects to mirror the web api of ovh
113 0           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 0           my $domain = Webservice::OVH::Domain->_new( wrapper => $api_wrapper, module => $self );
115 0           my $me = Webservice::OVH::Me->_new( wrapper => $api_wrapper, module => $self );
116 0           my $order = Webservice::OVH::Order->_new( wrapper => $api_wrapper, module => $self );
117 0           my $email = Webservice::OVH::Email->_new( wrapper => $api_wrapper, module => $self );
118 0           my $cloud = Webservice::OVH::Cloud->_new( wrapper => $api_wrapper, module => $self );
119 0           my $hosting = Webservice::OVH::Hosting->_new( wrapper => $api_wrapper, module => $self );
120              
121             # Timeout can be also set in the json file
122 0   0       OVH::OvhApi->setRequestTimeout( timeout => $data->{timeout} || 120 );
123              
124             # Setting private variables
125 0           $self->{_domain} = $domain;
126 0           $self->{_me} = $me;
127 0           $self->{_order} = $order;
128 0           $self->{_api_wrapper} = $api_wrapper;
129 0           $self->{_email} = $email;
130 0           $self->{_cloud} = $cloud;
131 0           $self->{_hosting} = $hosting;
132              
133 0           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 0     0 1   my ( $class, %params ) = @_;
156              
157 0           my @keys = qw{ application_key application_secret consumer_key };
158              
159 0 0         if ( my @missing_parameters = grep { not $params{$_} } @keys ) {
  0            
160              
161 0           croak "Missing parameter: @missing_parameters";
162             }
163              
164 0           my $self = bless {}, $class;
165              
166 0           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 0           my $domain = Webservice::OVH::Domain->_new( wrapper => $api_wrapper, module => $self );
168 0           my $me = Webservice::OVH::Me->_new( wrapper => $api_wrapper, module => $self );
169 0           my $order = Webservice::OVH::Order->_new( wrapper => $api_wrapper, module => $self );
170 0           my $email = Webservice::OVH::Email->_new( wrapper => $api_wrapper, module => $self );
171 0           my $cloud = Webservice::OVH::Cloud->_new( wrapper => $api_wrapper, module => $self );
172 0           my $hosting = Webservice::OVH::Hosting->_new( wrapper => $api_wrapper, module => $self );
173              
174 0   0       OVH::OvhApi->setRequestTimeout( timeout => $params{timeout} || 120 );
175              
176 0           $self->{_domain} = $domain;
177 0           $self->{_me} = $me;
178 0           $self->{_order} = $order;
179 0           $self->{_api_wrapper} = $api_wrapper;
180 0           $self->{_email} = $email;
181 0           $self->{_cloud} = $cloud;
182 0           $self->{_hosting} = $hosting;
183              
184 0           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 0     0 1   my ( $class, $timeout ) = @_;
204              
205 0           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 0     0 1   my ($self) = @_;
225              
226 0           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 0     0 1   my ($self) = @_;
246              
247 0           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 0     0 1   my ($self) = @_;
267              
268 0           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 0     0 1   my ($self) = @_;
288              
289 0           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 0     0 1   my ($self) = @_;
309              
310 0           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 0     0 1   my ($self) = @_;
330              
331 0           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;