line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Pandora::Method; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
20716
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
72
|
|
4
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
73
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
1485
|
use WebService::Pandora::Cryptor; |
|
3
|
|
|
|
|
28
|
|
|
3
|
|
|
|
|
95
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
2574
|
use URI; |
|
3
|
|
|
|
|
17280
|
|
|
3
|
|
|
|
|
83
|
|
9
|
3
|
|
|
3
|
|
3162
|
use JSON; |
|
3
|
|
|
|
|
37703
|
|
|
3
|
|
|
|
|
24
|
|
10
|
3
|
|
|
3
|
|
2585
|
use HTTP::Request; |
|
3
|
|
|
|
|
50307
|
|
|
3
|
|
|
|
|
97
|
|
11
|
3
|
|
|
3
|
|
3032
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
70589
|
|
|
3
|
|
|
|
|
104
|
|
12
|
3
|
|
|
3
|
|
26
|
use Data::Dumper; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2075
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
### constructor ### |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub new { |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
1
|
10
|
my $caller = shift; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
3
|
my $class = ref( $caller ); |
21
|
1
|
50
|
|
|
|
4
|
$class = $caller if ( !$class ); |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
|
|
14
|
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
|
|
|
|
|
3
|
bless( $self, $class ); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# create and store user agent object |
40
|
1
|
|
|
|
|
12
|
$self->{'ua'} = LWP::UserAgent->new( timeout => $self->{'timeout'} ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# create and store json object |
43
|
1
|
|
|
|
|
3043
|
$self->{'json'} = JSON->new(); |
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
|
|
4
|
return $self; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
### getters/setters ### |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub error { |
51
|
|
|
|
|
|
|
|
52
|
2
|
|
|
2
|
1
|
371
|
my ( $self, $error ) = @_; |
53
|
|
|
|
|
|
|
|
54
|
2
|
100
|
|
|
|
6
|
$self->{'error'} = $error if ( defined( $error ) ); |
55
|
|
|
|
|
|
|
|
56
|
2
|
|
|
|
|
7
|
return $self->{'error'}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
### public methods ### |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub execute { |
62
|
|
|
|
|
|
|
|
63
|
1
|
|
|
1
|
1
|
5
|
my ( $self ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# make sure both name and host were given |
66
|
1
|
50
|
33
|
|
|
7
|
if ( !defined( $self->{'name'} ) || !defined( $self->{'host'} ) ) { |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
4
|
$self->error( 'Both the name and host must be provided to the constructor.' ); |
69
|
1
|
|
|
|
|
2
|
return; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# craft the json data accordingly |
73
|
0
|
|
|
|
|
|
my $json_data = {}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
if ( defined( $self->{'userAuthToken'} ) ) { |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$json_data->{'userAuthToken'} = $self->{'userAuthToken'}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
0
|
|
|
|
|
if ( defined( $self->{'syncTime'} ) ) { |
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'}}}; |
|
0
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# encode it to json |
89
|
0
|
|
|
|
|
|
$json_data = $self->{'json'}->encode( $json_data ); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# encrypt it, if needed |
92
|
0
|
0
|
|
|
|
|
if ( $self->{'encrypt'} ) { |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
$json_data = $self->{'cryptor'}->encrypt( $json_data ); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# detect error decrypting |
97
|
0
|
0
|
|
|
|
|
if ( !defined( $json_data ) ) { |
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
|
0
|
|
|
|
|
my $protocol = ( $self->{'ssl'} ) ? 'https://' : 'http://'; |
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
|
0
|
|
|
|
|
if ( defined( $self->{'userAuthToken'} ) ) { |
|
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
if ( defined( $self->{'partnerId'} ) ) { |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
push( @$url_params, 'partner_id' => $self->{'partnerId'} ); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# set user_id if provided |
135
|
0
|
0
|
|
|
|
|
if ( defined( $self->{'userId'} ) ) { |
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
|
0
|
|
|
|
|
if ( !$response->is_success() ) { |
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
|
0
|
|
|
|
|
if ( $json_data->{'stat'} ne 'ok' ) { |
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
|
0
|
|
|
|
|
return $json_data->{'result'} if ( defined( $json_data->{'result'} ) ); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# otherwise just return a true value to indicate success |
176
|
0
|
|
|
|
|
|
return 1; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
1; |