| 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=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use Encode qw/encode_utf8/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 23 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 23 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 23 | with 'WWW::FBX::Role::API::APIv3'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 23 | with 'WWW::FBX::Role::Auth'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 23 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 23 | our $VERSION = "0.23"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 23 | has base_url => ( isa => 'Str', is => 'ro', default => 'http://mafreebox.free.fr' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | has lwp_args => ( isa => 'HashRef', is => 'ro', default => sub { {} } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 23 | has [ qw/app_id app_name app_version device_name/ ] => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | isa => 'Str', is => 'ro', required => 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 23 | has ua => ( isa => 'LWP::UserAgent', is => 'rw', lazy => 1, builder => '_build_ua' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 23 | has uar => ( isa => 'HashRef', is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 23 | has uarh => ( isa => 'HTTP::Response', is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 23 | has debug => ( isa => 'Bool', is => 'rw', default => 0, trigger => \&_set_debug ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 23 | has noauth => ( isa => 'Bool', is => 'ro', default => 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | has _json_handler => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | default => sub { JSON->new->allow_nonref }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 23 | handles => { from_json => 'decode' }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub _set_debug { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_debug: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | my ( $self, $debug, $odebug) = @_ ; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( defined $odebug and $odebug != $debug or $debug ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $odebug and $odebug != $debug or $debug
defined $odebug and $odebug != $debug
MC/DC0%0/3
defined $odebug and $odebug != $debug or $debug
defined $odebug
$odebug != $debug
$debug
Truth table0%0/5
A and B or C
A= defined $odebugB= $odebug != $debugC= $debug
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $self->ua->add_handler("request_send", sub { print ">" x 25, "\n"; shift->dump; return }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $self->ua->add_handler("response_done", sub { print "<" x 25, "\n"; shift->dump; return }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | $self->ua->remove_handler("request_send"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $self->ua->remove_handler("response_done"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _build_ua { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_ua: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $ua = LWP::UserAgent->new(%{$self->lwp_args}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return $ua; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub _json_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_json_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my ($self, $http_method, $uri, $args, $content_type ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $msg = $self->_prepare_request($http_method, $uri, $args, $content_type); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $res = $self->_send_request($msg); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | #Store response content | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $self->uar( $self->_parse_result ($res, $args ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | #And HTTP response RAW | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $self->uarh( $res ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | return $self->uar->{result}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub _prepare_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare_request: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my ($self, $http_method, $uri, $args, $content_type ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $msg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if( $http_method eq 'PUT' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $msg = PUT( $uri, Content => encode_json $args ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | elsif ( $http_method =~ /^(?:GET|DELETE)$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $uri->query($self->_query_string_for($args)) if keys %$args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $msg = HTTP::Request->new($http_method, $uri); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | elsif ( $http_method eq 'POST' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if( !$content_type or $content_type eq 'application/json' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
not $content_type or $content_type eq "application/json"
MC/DC0%0/2
not $content_type or $content_type eq "application/json"
not $content_type
$content_type eq "application/json"
Truth table0%0/3
not(A) or B
A= not $content_typeB= $content_type eq "application/json"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $msg = POST( $uri, Content_Type => 'application/json', Content => encode_json $args ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | elsif ( $content_type eq "form-data" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $msg = POST($uri, Content_Type => 'form-data', Content => [ map { ref $_ ? $_ : encode_utf8 $_ } %$args ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $msg = POST($uri, Content => $args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | croak "unexpected HTTP method: $http_method"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | return $msg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub _query_string_for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_query_string_for: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | while ( my ($k, $v) = each %$args ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | push @pairs, join '=', $k, $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return join '&', @pairs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | sub _send_request { shift->ua->request(shift) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_send_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub _parse_result { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_result: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | my ($self, $res, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $content = $res->content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $j_obj = length $content ? try { $self->from_json($content) } : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | #Die if message contains an API error (even on HTTP 200) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ( ref $j_obj && reftype $j_obj eq 'HASH' && (exists $j_obj->{error_code} || exists $j_obj->{msg} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
ref $j_obj and builtin::reftype($j_obj) eq "HASH" and exists $j_obj->{"error_code"} || exists $j_obj->{"msg"}
ref $j_obj and builtin::reftype($j_obj) eq "HASH"
exists $j_obj->{"error_code"} or exists $j_obj->{"msg"}
MC/DC0%0/4
ref $j_obj and builtin::reftype($j_obj) eq "HASH" and exists $j_obj->{"error_code"} || exists $j_obj->{"msg"}
ref $j_obj
builtin::reftype($j_obj) eq "HASH"
exists $j_obj->{"error_code"}
exists $j_obj->{"msg"}
Truth table0%0/5
A and B and C or D
A= ref $j_objB= builtin::reftype($j_obj) eq "HASH"C= exists $j_obj->{"error_code"}D= exists $j_obj->{"msg"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | die WWW::FBX::Error->new(fbx_error => $j_obj, http_response => $res); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | #If no API error and HTTP is 200 and answer is json | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return $j_obj if $res->is_success && defined $j_obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->is_success and defined $j_obj
MC/DC0%0/2
$res->is_success and defined $j_obj
$res->is_success
defined $j_obj
Truth table0%0/3
A and B
A= $res->is_successB= defined $j_obj
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | #API Download file does not return JSON!! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | #If answer is 200 and not json, return unchanged (but still pack it in an HashRef for uar type check..) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | return { result => { filename => $res->filename, content => $content } } if $res->filename and $res->is_success; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->filename and $res->is_success
MC/DC0%0/2
$res->filename and $res->is_success
$res->filename
$res->is_success
Truth table0%0/3
A and B
A= $res->filenameB= $res->is_success
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | #Else die on HTTP failures, which might contain a json response or not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my $error = WWW::FBX::Error->new(http_response => $res); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $error->fbx_error($j_obj) if ref $j_obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | die $error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 23 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||