| 1 | package WebService::Pandora::Method; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use WebService::Pandora::Cryptor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | ### constructor ### | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | my $caller = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | my $class = ref( $caller ); | |||||||||||||||||||||||||||
| 21 | 1 | ▶ | $class = $caller if ( !$class ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | my $self = {'name' => undef, | |||||||||||||||||||||||||||
| 24 | 'partnerAuthToken' => undef, | ||||||||||||||||||||||||||||
| 25 | 'userAuthToken' => undef, | ||||||||||||||||||||||||||||
| 26 | 'partnerId' => undef, | ||||||||||||||||||||||||||||
| 27 | 'userId' => undef, | ||||||||||||||||||||||||||||
| 28 | 'syncTime' => undef, | ||||||||||||||||||||||||||||
| 29 | 'host' => undef, | ||||||||||||||||||||||||||||
| 30 | 'ssl' => 0, | ||||||||||||||||||||||||||||
| 31 | 'encrypt' => 1, | ||||||||||||||||||||||||||||
| 32 | 'cryptor' => undef, | ||||||||||||||||||||||||||||
| 33 | 'timeout' => 10, | ||||||||||||||||||||||||||||
| 34 | 'params' => {}, | ||||||||||||||||||||||||||||
| 35 | @_}; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 1 | bless( $self, $class ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # create and store user agent object | ||||||||||||||||||||||||||||
| 40 | 1 | $self->{'ua'} = LWP::UserAgent->new( timeout => $self->{'timeout'} ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # create and store json object | ||||||||||||||||||||||||||||
| 43 | 1 | $self->{'json'} = JSON->new(); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | return $self; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | ### getters/setters ### | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub error { | ||||||||||||||||||||||||||
|
Subroutine
error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | my ( $self, $error ) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | $self->{'error'} = $error if ( defined( $error ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | return $self->{'error'}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ### public methods ### | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: called CC=15 SCAR=52.3
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # make sure both name and host were given | ||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if ( !defined( $self->{'name'} ) || !defined( $self->{'host'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"name"} and defined $self->{"host"}
MC/DC0%0/2
defined $self->{"name"} and defined $self->{"host"}
defined $self->{"name"}
defined $self->{"host"}
Truth table33%1/3
A and B
A= defined $self->{"name"}B= defined $self->{"host"}
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | $self->error( 'Both the name and host must be provided to the constructor.' ); | |||||||||||||||||||||||||||
| 69 | 1 | return; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # craft the json data accordingly | ||||||||||||||||||||||||||||
| 73 | 0 | my $json_data = {}; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( defined( $self->{'userAuthToken'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | $json_data->{'userAuthToken'} = $self->{'userAuthToken'}; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( defined( $self->{'syncTime'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | $json_data->{'syncTime'} = int( $self->{'syncTime'} ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # merge the two required params with the additional user-supplied args | ||||||||||||||||||||||||||||
| 86 | 0 | $json_data = {%$json_data, %{$self->{'params'}}}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # encode it to json | ||||||||||||||||||||||||||||
| 89 | 0 | $json_data = $self->{'json'}->encode( $json_data ); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # encrypt it, if needed | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if ( $self->{'encrypt'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | $json_data = $self->{'cryptor'}->encrypt( $json_data ); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # detect error decrypting | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( !defined( $json_data ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $self->error( 'An error occurred encrypting our JSON data: ' . $self->{'cryptor'}->error() ); | |||||||||||||||||||||||||||
| 100 | 0 | return; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # http or https? | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my $protocol = ( $self->{'ssl'} ) ? 'https://' : 'http://'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # craft the full URL, protocol + host + path | ||||||||||||||||||||||||||||
| 108 | 0 | my $url = $protocol . $self->{'host'} . '/services/json/'; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # create URI object | ||||||||||||||||||||||||||||
| 111 | 0 | my $uri = URI->new( $url ); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # create all url params for POST request | ||||||||||||||||||||||||||||
| 114 | 0 | my $url_params = ['method' => $self->{'name'}]; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # set user_auth_token if provided | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( defined( $self->{'userAuthToken'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | push( @$url_params, 'auth_token' => $self->{'userAuthToken'} ); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # set partner_auth_token if provided and user_auth_token was not | ||||||||||||||||||||||||||||
| 123 | elsif ( defined( $self->{'partnerAuthToken'} ) ) { | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | push( @$url_params, 'auth_token' => $self->{'partnerAuthToken'} ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # set partner_id if provided | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ( defined( $self->{'partnerId'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | push( @$url_params, 'partner_id' => $self->{'partnerId'} ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # set user_id if provided | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ( defined( $self->{'userId'} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | push( @$url_params, 'user_id' => $self->{'userId'} ); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # add the params to the URI | ||||||||||||||||||||||||||||
| 141 | 0 | $uri->query_form( $url_params ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # create and store the POST request accordingly | ||||||||||||||||||||||||||||
| 144 | 0 | my $request = HTTP::Request->new( 'POST', $uri ); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # json data is the POST content | ||||||||||||||||||||||||||||
| 147 | 0 | $request->content( $json_data ); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # issue the HTTP request | ||||||||||||||||||||||||||||
| 150 | 0 | my $response = $self->{'ua'}->request( $request ); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # handle request error | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ( !$response->is_success() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | $self->error( $response->status_line() ); | |||||||||||||||||||||||||||
| 156 | 0 | return; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # decode the raw content of the response | ||||||||||||||||||||||||||||
| 160 | 0 | my $content = $response->decoded_content(); | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # decode the JSON content | ||||||||||||||||||||||||||||
| 163 | 0 | $json_data = $self->{'json'}->decode( $content ); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # handle pandora error | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ( $json_data->{'stat'} ne 'ok' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | $self->error( "$self->{'name'} error $json_data->{'code'}: $json_data->{'message'}" ); | |||||||||||||||||||||||||||
| 169 | 0 | return; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # return all result data, if any exists | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | return $json_data->{'result'} if ( defined( $json_data->{'result'} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # otherwise just return a true value to indicate success | ||||||||||||||||||||||||||||
| 176 | 0 | return 1; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 3 | 1; | |||||||||||||||||||||||||||