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