blib/lib/WWW/FBX.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/WWW/FBX/API.pm ↑ Summary Build »
1 package WWW::FBX;
2 22 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 22 ▶use Carp::Clan qw/^(?:WWW::FBX|Moose|Class::MOP)/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use WWW::FBX::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Encode qw/encode_utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 with 'WWW::FBX::Role::API::APIv3';
14 with 'WWW::FBX::Role::Auth';
15
16 22 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 our $VERSION="0.06";
19  
20 has lwp_args => ( isa => 'HashRef', is => 'ro', default => sub { {} } );
21 has ua => ( isa => 'LWP::UserAgent', is => 'rw', lazy => 1, builder => '_build_ua' );
22 has uar => ( isa => 'HashRef', is => 'rw' );
23 has [ qw/app_id app_name app_version device_name/ ] => (
24 isa => 'Str', is => 'ro', required => 1 );
25  
26 has _json_handler => (
27 is => 'rw',
28 default => sub { JSON->new->allow_nonref },
29 handles => { from_json => 'decode' },
30 );
31
32 sub _build_ua {
33 my $self = shift;
34
35 my $ua = LWP::UserAgent->new(%{$self->lwp_args});
36
37 return $ua;
38 }
39
40 sub _json_request {
41 my ($self, $http_method, $uri, $args, $content_type ) = @_;
42
43 my $msg = $self->_prepare_request($http_method, $uri, $args, $content_type);
44 my $res = $self->_send_request($msg);
45  
46 #Store response
47 $self->uar( $self->_parse_result ($res, $args ) );
48  
49 return $self->uar;
50 }
51
52 sub _prepare_request {
53 my ($self, $http_method, $uri, $args, $content_type ) = @_;
54
55 my $msg;
56
57 if( $http_method eq 'PUT' ) {
58 $msg = PUT( $uri, Content => encode_json $args );
59 }
60 elsif ( $http_method =~ /^(?:GET|DELETE)$/ ) {
61 $uri->query($self->_query_string_for($args)) if keys %$args;
62 $msg = HTTP::Request->new($http_method, $uri);
63 }
64 elsif ( $http_method eq 'POST' ) {
65 if( !$content_type or $content_type eq 'application/json' ) {
66 $msg = POST( $uri, Content_Type => 'application/json', Content => encode_json $args );
67 }
68 elsif ( $content_type eq "form-data" ) {
69 print "$http_method : $uri\n";
70 $msg = POST($uri, Content_Type => 'form-data', Content => [ map { ref $_ ? $_ : encode_utf8 $_ } %$args ]);
71 }
72 else {
73 $msg = POST($uri, Content => $args);
74 }
75 }
76 else {
77 croak "unexpected HTTP method: $http_method";
78 }
79
80 return $msg;
81 }
82
83 sub _query_string_for {
84 my ( $self, $args ) = @_;
85  
86 my @pairs;
87 while ( my ($k, $v) = each %$args ) {
88 push @pairs, join '=', $k, $v;
89 }
90  
91 return join '&', @pairs;
92 }
93  
94 sub _send_request { shift->ua->request(shift) }
95
96 sub _parse_result {
97 my ($self, $res, $args) = @_;
98
99 my $content = $res->content;
100
101 my $j_obj = length $content ? try { $self->from_json($content) } : {};
102  
103 #Die if message contains an API error (even on HTTP 200)
104 if ( ref $j_obj && reftype $j_obj eq 'HASH' && (exists $j_obj->{error_code} || exists $j_obj->{msg} ) ) {
105 die WWW::FBX::Error->new(fbx_error => $j_obj, http_response => $res);
106 }
107  
108 #If no API error and HTTP is 200 and answer is json
109 return $j_obj if $res->is_success && defined $j_obj;
110  
111 #API Download file does not return JSON!!
112 #If answer is 200 and not json, return unchanged (but still pack it in an HashRef for uar type check..)
113 return {content => $content} if $res->is_success;
114  
115 #Else die on HTTP failures, which might contain a json response or not
116 my $error = WWW::FBX::Error->new(http_response => $res);
117 $error->fbx_error($j_obj) if ref $j_obj;
118
119 die $error;
120 }
121  
122 __PACKAGE__->meta->make_immutable;
123  
124 1;
125 __END__
126  
127 =for html <a href="https://travis-ci.org/architek/WWW-FBX"><img src="https://travis-ci.org/architek/WWW-FBX.svg?branch=master"></a>
128  
129 =encoding utf-8
130  
131 =head1 NAME
132  
133 WWW::FBX - A perl interface to the Freebox v6 Rest API
134  
135 =head1 FREEBOX SDK API 3.0
136  
137 This version provides the API 3.0 support through the APIv3 role but other version can be provided by creating a new role.
138  
139 =head1 AUTHENTICATION
140  
141 Authentication is provided through the Auth role but other authentication mechanism can be provided by creating a new role.
142  
143 =head1 SYNOPSIS
144  
145 use WWW::FBX;
146 use Scalar::Util 'blessed';
147  
148 my $res;
149 eval {
150 my $fbx = WWW::FBX->new(
151 app_id => "APP ID",
152 app_name => "APP NAME",
153 app_version => "1.0",
154 device_name => "debian",
155 track_id => "48",
156 app_token => "2/g43EZYD8AO7tbnwwhmMxMuELtTCyQrV1goMgaepHWGrqWlloWmMRszCuiN2ftp",
157 );
158 print "You are now authenticated with track_id ", $fbx->track_id, " and app_token ", $fbx->app_token, "\n";
159 print "App permissions are:\n";
160 while ( my( $key, $value ) = each %{ $fbx->uar->{result}{permissions} } ) {
161 print "\t $key\n" if $value;
162 }
163  
164 $res = $fbx->connection;
165 print "Your ", $res->{result}{media}, " internet connection state is ", $res->{result}{state}, "\n";
166 $fbx->set_ftp_config( {enabled => \1} );
167 $fbx->reset_freeplug( {suff=>"F4:CA:E5:DE:AD:BE/reset/"} );
168 };
169  
170 if ( my $err = $@ ) {
171 die $@ unless blessed $err && $err->isa('WWW::FBX::Error');
172
173 warn "HTTP Response Code: ", $err->code, "\n",
174 "HTTP Message......: ", $err->message, "\n",
175 "API Error.........: ", $err->error, "\n",
176 "Error Code........: ", $err->fbx_error_code, "\n",
177 }
178  
179 =head1 DESCRIPTION
180  
181 This module provides a perl interface to the L<Freebox|https://en.wikipedia.org/wiki/Freebox#V6_generation.2C_Freebox_Revolution> v6 APIs. See L<http://dev.freebox.fr/sdk/os/> for a full description of the APIs.
182  
183 =head1 METHODS AND ARGUMENTS
184  
185 Mandatory constructor parameters are app_id, app_name, app_version, device_name.
186  
187 When track_id and app_token are also provided, they will be used to authenticate.
188 Otherwise, new track_id and app_token will be given by the freebox. These can be then used for later access.
189 Note that adding the settings permission is only possible through the web interface (Paramètres de la Freebox -> Gestion des accès -> Applications)
190  
191 The constructor takes care of detecting the API version and authentication.
192  
193 Return values are perl hashes.
194  
195 The same response is also available through the uar method.
196  
197 This distribution is heavily inspired from L<Net::Twitter>.
198  
199 =head1 LICENSE
200  
201 Copyright (C) Laurent Kislaire.
202  
203 This library is free software; you can redistribute it and/or modify
204 it under the same terms as Perl itself.
205  
206 =head1 AUTHOR
207  
208 Laurent Kislaire E<lt>teebeenator@gmail.comE<gt>
209  
210 =cut
211  
« blib/lib/WWW/FBX/API.pm ↑ Summary Build »