File Coverage

blib/lib/WWW/Freebox.pm
Criterion Covered Total %
statement 27 100 27.0
branch 0 22 0.0
condition 0 4 0.0
subroutine 9 15 60.0
pod 6 6 100.0
total 42 147 28.5


line stmt bran cond sub pod time code
1             package WWW::Freebox;
2              
3 1     1   13945 use 5.006;
  1         3  
  1         31  
4 1     1   7 use strict;
  1         1  
  1         31  
5 1     1   3 use warnings;
  1         5  
  1         24  
6 1     1   438 use LWP;
  1         32230  
  1         31  
7 1     1   546 use LWP::Simple;
  1         13292  
  1         7  
8 1     1   318 use HTTP::Request;
  1         2  
  1         17  
9 1     1   452 use HTTP::Request::Common;
  1         1525  
  1         65  
10 1     1   583 use JSON;
  1         8492  
  1         7  
11 1     1   536 use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
  1         6657  
  1         1002  
12              
13             =head1 NAME
14              
15             WWW::Freebox - Access to FreeboxOS API
16              
17             =head1 VERSION
18              
19             Version 0.01
20              
21             =cut
22              
23             our $VERSION = '0.01';
24              
25             =head1 SYNOPSIS
26              
27             Before using this module in your script(s) you need to acquire a token and a ID.
28              
29             use WWW::Freebox;
30            
31             my $fbx = WWW::Freebox->new("mafreebox.freebox.fr");
32            
33             my $app_id = "perl.helloworld";
34             my $app_name = "Perl script";
35             my $app_version = "1.0";
36             my $device_name = "John's computer";
37            
38             # You have to launch this method only once because a unique token and a unique ID are required per application.
39             my ($app_token, $track_id) = $fbx->authorize($app_id, $app_name, $app_version, $device_name);
40             # You need to store $app_token and $track_id (in a config file for example)
41             # You will have to grant access to your application (a message will be displayed on LCD screen of the Freebox Server)
42              
43             Once you've got the token and the ID you will be able to use the module as follow :
44            
45             use WWW::Freebox;
46            
47             my $fbx = WWW::Freebox->new("mafreebox.freebox.fr");
48            
49             $fbx->login("perl.helloworld", $app_token, $track_id);
50            
51             if($fbx->{permissions}{downloader}){
52             # Doing a request to FreeboxOS API
53             my $content = [download_dir => 'path', download_file => ['file.torrent']];
54             my $jsonResponse = $fbx->request("downloads/add", 1, $content);
55             }
56            
57             You can find more informations at L.
58            
59             =head1 METHODS
60              
61             =head2 new(freebox)
62              
63             my $fbx = WWW::Freebox->new("mafreebox.freebox.fr");
64            
65             =cut
66              
67             sub new {
68 0     0 1   my $class = shift;
69 0           my $self = {
70             freebox => shift
71             };
72            
73 0   0       my $content = decode_json(get("http://".$self->{freebox}."/api_version") or die("Can't access to http://".$self->{freebox}."/api_version.\n"));
74 0           $self->{uid} = $content->{"uid"};
75 0           $self->{device_name} = $content->{"device_name"};
76 0           $self->{device_name} =~ s/\\\//\//g;
77 0           $self->{api_version} = $content->{"api_version"};
78 0           $self->{api_base_url} = $content->{"api_base_url"};
79 0           $self->{api_base_url} =~ s/\\\//\//g;
80 0           $self->{device_type} = $content->{"device_type"};
81 0           $self->{device_type} =~ s/\\\//\//g;
82            
83 0           $self->{api_version} =~ m/^([0-9]+)/;
84 0           $self->{base_url} = "http://".$self->{freebox}."/api/v".$1."/";
85            
86 0           bless $self, $class;
87            
88 0           return $self;
89             }
90              
91             =head2 $fbx->authorize(app_id, app_name, app_version, device_name)
92              
93             my ($app_token, $track_id) = $fbx->authorize("perl.helloworld", "Perl script", "1.0", "John's computer");
94            
95             =cut
96              
97             sub authorize {
98 0     0 1   my $self = shift;
99 0           my %authorize = (
100             'app_id' => shift,
101             'app_name' => shift,
102             'app_version' => shift,
103             'device_name' => shift,
104             );
105 0           my $content = decode_json($self->request('login/authorize/', 1, encode_json(\%authorize)));
106 0 0         unless($content->{success}) {
107 0           die("Error: ".$content->{msg}."\n");
108             }
109 0           return ($content->{result}{app_token}, $content->{result}{track_id});
110             }
111              
112             =head2 $fbx->login($app_id, $app_token, $track_id)
113              
114             $fbx->login("perl.helloworld", $app_token, $track_id);
115            
116             =cut
117              
118             sub login {
119 0     0 1   my $self = shift;
120 0           $self->{app_id} = shift;
121 0           $self->{app_token} = shift;
122 0           $self->{track_id} = shift;
123            
124 0   0       my $content = decode_json(get($self->{base_url}."login/authorize/".$self->{track_id}) or die("Can't access to ".$self->{base_url}."login/authorize/".$self->{track_id}."\n"));
125 0 0         if($content->{'result'}{'status'} eq "pending") {
126 0           die("you have to grant access for this application (a message has been displayed on the LCD screen of the Freebox).\n");
127             }
128 0 0         unless($content->{'result'}{'status'} eq "granted") {
129 0           die("The freebox has returned the following status: ".$content->{'result'}{'status'}.'. You should maybe try to get another app token with the function authorize($app_id, $app_name, $app_version, $device_name)'."\n");
130             }
131 0           my $challenge = $content->{'result'}{'challenge'};
132 0           my $password_salt = $content->{'result'}{'password_salt'};
133 0           my $password = hmac_sha1_hex($challenge, $self->{app_token});
134 0           my %session_login = (
135             'app_id' => $self->{app_id},
136             'password' => $password,
137             );
138 0           my $result = decode_json($self->request('login/session/', 1, encode_json(\%session_login)));
139 0 0         if($result->{'success'}){
140 0           $self->{session_token} = $result->{'result'}{'session_token'};
141 0           $self->{permissions} = $result->{'result'}{'permissions'};
142 0           return 1;
143             }
144             else {
145 0           print "Error: ".$result->{'msg'}."\n";
146 0           return 0;
147             }
148             }
149              
150             =head2 $fbx->request($url, $method [, $content/@content])
151              
152             # Possible values for the second parameter (method):
153             # 0: GET
154             # 1: POST
155             # 2: PUT
156             # 3: DELETE
157            
158             my $jsonResponse = $fbx->request("downloads/", 0);
159            
160             my $content = '{"io_priority": "high","status": "stopped"}';
161             my $jsonResponse = $fbx->request("downloads/16", 2, $content);
162            
163             my @content = [download_dir => 'path', download_file => ['file.torrent']];
164             my $jsonResponse = $fbx->request("downloads/add", 1, @content);
165            
166             =cut
167              
168             sub request {
169 0     0 1   my $self = $_[0];
170 0           my $url = $self->{base_url}.$_[1];
171 0           my $method = $_[2];
172 0           my $req;
173             my $content_type;
174 0           my $content;
175            
176 0 0         if(defined $_[3]){
177 0           $content = $_[3];
178 0 0         if(ref($_[3]) eq "ARRAY") {
179 0           $content_type = "form-data";
180             }
181             else {
182 0           $content_type = "application/json";
183             }
184             }
185 0 0         if($method == 0){
    0          
    0          
    0          
186 0           $req = GET($url);
187             }
188             elsif($method == 1) {
189 0           $req = POST($url, Content_Type => $content_type, Content => $content);
190             }
191             elsif($method == 2) {
192 0           $req = PUT($url, Content_Type => $content_type, Content => $content);
193             }
194             elsif($method == 3) {
195 0           $req = DELETE($url);
196             }
197 0 0         if(defined $self->{session_token}) {
198 0           $req->header("X-Fbx-App-Auth" => $self->{session_token});
199             }
200            
201 0           my $lwp = LWP::UserAgent->new;
202 0           my $response = $lwp->request($req);
203 0           my $json = $response->decoded_content();
204 0           return $json;
205             }
206              
207             =head2 $fbx->logout()
208            
209             $fbx->logout();
210            
211             =cut
212              
213             sub logout {
214 0     0 1   my $self = shift;
215 0           $self->request("login/logout/", 1);
216 0           undef $self->{app_id};
217 0           undef $self->{app_token};
218 0           undef $self->{track_id};
219 0           undef $self->{session_token};
220 0           undef $self->{permissions};
221 0           return 1;
222             }
223              
224             =head2 $fbx->close()
225            
226             $fbx->close();
227            
228             =cut
229              
230             sub close {
231 0     0 1   my $self = shift;
232 0           undef $self;
233 0           return 1;
234             }
235              
236             =head1 AUTHOR
237              
238             Alexandre van Beurden, C<< >>
239              
240             =head1 BUGS
241              
242             Please report any bugs or feature requests to C, or through
243             the web interface at L.
244             You can also open an issue at L.
245              
246             =head1 SUPPORT
247              
248             You can find documentation for this module with the perldoc command.
249              
250             perldoc WWW::Freebox
251              
252              
253             You can also look for information at:
254              
255             =over 4
256              
257             =item * RT: CPAN's request tracker (report bugs here)
258              
259             L
260              
261             =item * AnnoCPAN: Annotated CPAN documentation
262              
263             L
264              
265             =item * CPAN Ratings
266              
267             L
268              
269             =item * Search CPAN
270              
271             L
272              
273             =back
274              
275             If you need help or you have any question about this module feel free to email me.
276              
277             =head1 LICENSE AND COPYRIGHT
278              
279             Copyright 2015 Alexandre van Beurden.
280              
281             This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
282              
283             =cut
284              
285             1; # End of WWW::Freebox