blib/lib/WWW/Freebox.pm

filter: Statementstmt 27.7% 28 / 101 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.0% 9 / 15 Podpod 100% 6 / 6 totaltotal 28.6% 43 / 150 CC 14 SCAR 33.1
CC
14
Cov
22%
request
72.0
login
30.0
new
6.0
CRAP 27.2
↑ Summary
1 package WWW::Freebox;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Digest::HMAC_SHA1 qw(hmac_sha1_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 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<http://dev.freebox.fr/sdk/os/>.
58
59 =head1 METHODS
60  
61 =head2 new(freebox)
62  
63 my $fbx = WWW::Freebox->new("mafreebox.freebox.fr");
64
65 =cut
66  
67 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
68 0 my $class = shift;
69 0 my $self = {
70 freebox => shift
71 };
72
73 0 ▶ my $content = decode_json(get("http://".$self->{freebox}."/api_version") or die("Can't access to http://".$self->{freebox}."/api_version.\n"));
Condition0%0/2
&get('http://' . $self->{'freebox'} . '/api_version') || die(q[Can't access to http://] . $self->{'freebox'} . "/api_version.\n")
l!l
00
MC/DC0%0/1
&get('http://' . $self->{'freebox'} . '/api_version') || die(q[Can't access to http://] . $self->{'freebox'} . "/api_version.\n")
&get('http://' . $self->{'freebox'} . '/api_version')
Truth table0%0/2
A
A= &get('http://' . $self->{'freebox'} . '/api_version')
Aresult
11
00
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 0 ▶sub authorize {
Subroutine authorize: not called CC=2 SCAR=17.9
Pod documented
98 0 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 ▶ unless($content->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content->{"success"}) 0 0 0
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 0 ▶sub login {
Subroutine login: not called CC=5 SCAR=34.0
Pod documented
119 0 my $self = shift;
120 0 $self->{app_id} = shift;
121 0 $self->{app_token} = shift;
122 0 $self->{track_id} = shift;
123
124 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"));
Condition0%0/2
&get($self->{'base_url'} . 'login/authorize/' . $self->{'track_id'}) || die(q[Can't access to ] . $self->{'base_url'} . 'login/authorize/' . $self->{'track_id'} . "\n")
l!l
00
MC/DC0%0/1
&get($self->{'base_url'} . 'login/authorize/' . $self->{'track_id'}) || die(q[Can't access to ] . $self->{'base_url'} . 'login/authorize/' . $self->{'track_id'} . "\n")
&get($self->{'base_url'} . 'login/authorize/' . $self->{'track_id'})
Truth table0%0/2
A
A= &get($self->{'base_url'} . 'login/authorize/' . $self->{'track_id'})
Aresult
11
00
125 0 ▶ if($content->{'result'}{'status'} eq "pending") {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->{"result"}{"status"} eq "pending") 0 0 0
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 ▶ unless($content->{'result'}{'status'} eq "granted") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content->{"result"}{"status"} eq "granted") 0 0 0
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 my %session_login = (
135 'app_id' => $self->{app_id},
136 0 'password' => $password,
137 );
138 0 my $result = decode_json($self->request('login/session/', 1, encode_json(\%session_login)));
139 0 ▶ if($result->{'success'}){
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'success'}) { } 0 0 0
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 0 ▶sub request {
Subroutine request: not called CC=8 SCAR=42.8
Pod documented
169 0 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 ▶ if(defined $_[3]){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]) 0 0 0
177 0 $content = $_[3];
178 0 ▶ if(ref($_[3]) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[3] eq 'ARRAY') { } 0 0 0
179 0 $content_type = "form-data";
180 }
181 else {
182 0 $content_type = "application/json";
183 }
184 }
185 0 ▶ if($method == 0){
Branch: 4 branches
BranchTrueFalseTotal
if ($method == 0) { } 0 0 0
elsif ($method == 1) { } 0 0 0
elsif ($method == 2) { } 0 0 0
elsif ($method == 3) { } 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 ▶ if(defined $self->{session_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"session_token"}) 0 0 0
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 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
214 0 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 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
231 0 my $self = shift;
232 0 undef $self;
233 0 return 1;
234 }
235  
236 =head1 AUTHOR
237  
238 Alexandre van Beurden, C<< <alexandre.vanbeurden.dev(at)gmail.com> >>
239  
240 =head1 BUGS
241  
242 Please report any bugs or feature requests to C<bug-www-freebox at rt.cpan.org>, or through
243 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Freebox>.
244 You can also open an issue at L<https://github.com/KiLlOrBe/PM-Freebox>.
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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Freebox>
260  
261 =item * AnnoCPAN: Annotated CPAN documentation
262  
263 L<http://annocpan.org/dist/WWW-Freebox>
264  
265 =item * CPAN Ratings
266  
267 L<http://cpanratings.perl.org/d/WWW-Freebox>
268  
269 =item * Search CPAN
270  
271 L<http://search.cpan.org/dist/WWW-Freebox/>
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 1; # End of WWW::Freebox
↑ Summary