line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::MyAffiliates; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
17804
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
74
|
|
4
|
3
|
|
|
3
|
|
11
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
100
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
12
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
207
|
|
8
|
3
|
|
|
3
|
|
1655
|
use Mojo::UserAgent; |
|
3
|
|
|
|
|
692389
|
|
|
3
|
|
|
|
|
24
|
|
9
|
3
|
|
|
3
|
|
109
|
use Mojo::Util qw(b64_encode url_escape); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
181
|
|
10
|
3
|
|
|
3
|
|
2352
|
use XML::Simple 'XMLin'; |
|
3
|
|
|
|
|
19106
|
|
|
3
|
|
|
|
|
18
|
|
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
226
|
use vars qw/$errstr/; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
1956
|
|
13
|
0
|
|
|
0
|
1
|
0
|
sub errstr { return $errstr } |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { ## no critic (ArgUnpacking) |
16
|
1
|
|
|
1
|
1
|
12
|
my $class = shift; |
17
|
1
|
50
|
|
|
|
11
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
2
|
for (qw/user pass host/) { |
20
|
3
|
50
|
|
|
|
24
|
$args{$_} || croak "Param $_ is required."; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# fix host with schema |
24
|
1
|
50
|
|
|
|
8
|
$args{host} = 'http://' . $args{host} unless $args{host} =~ m{^https?\://}; |
25
|
1
|
|
|
|
|
3
|
$args{host} =~ s{/$}{}; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
50
|
|
|
10
|
$args{timeout} ||= 30; # for ua timeout |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
3
|
return bless \%args, $class; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub __ua { |
33
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
0
|
return $self->{ua} if exists $self->{ua}; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
0
|
my $ua = Mojo::UserAgent->new; |
38
|
0
|
|
|
|
|
0
|
$ua->max_redirects(3); |
39
|
0
|
|
|
|
|
0
|
$ua->inactivity_timeout($self->{timeout}); |
40
|
0
|
|
|
|
|
0
|
$ua->proxy->detect; # env proxy |
41
|
0
|
|
|
|
|
0
|
$ua->max_connections(100); |
42
|
0
|
|
|
|
|
0
|
$self->{ua} = $ua; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
return $ua; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+1%3A+Users+Feed |
48
|
|
|
|
|
|
|
sub get_users { ## no critic (ArgUnpacking) |
49
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
50
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
51
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=1'); |
52
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
53
|
0
|
|
|
|
|
0
|
return $self->request($url->to_string); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub get_user { |
57
|
0
|
|
|
0
|
1
|
0
|
my ($self, $id) = @_; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
0
|
$id or croak "id is required."; |
60
|
0
|
0
|
|
|
|
0
|
my $user = $self->get_users(USER_ID => $id) or return; |
61
|
0
|
|
|
|
|
0
|
return $user->{USER}; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+4%3A+Decode+Token |
65
|
|
|
|
|
|
|
sub decode_token { |
66
|
1
|
|
|
1
|
1
|
715
|
my $self = shift; |
67
|
1
|
50
|
|
|
|
25
|
my @tokens = @_ or croak 'Must pass at least one token.'; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
0
|
return $self->request('/feeds.php?FEED_ID=4&TOKENS=' . url_escape(join(',', @tokens))); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+5%3A+Encode+Token |
73
|
|
|
|
|
|
|
sub encode_token { ## no critic (ArgUnpacking) |
74
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
75
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
0
|
$args{USER_ID} or croak "USER_ID is required."; |
78
|
0
|
0
|
|
|
|
0
|
$args{SETUP_ID} or croak "SETUP_ID is required."; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=5'); |
81
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
82
|
0
|
|
|
|
|
0
|
return $self->request($url->to_string); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
## https://myaffiliates.atlassian.net/wiki/display/PUB/Feed+6%3A+User+Transactions+Feed |
86
|
|
|
|
|
|
|
sub get_user_transactions { ## no critic (ArgUnpacking) |
87
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
88
|
0
|
0
|
|
|
|
0
|
my %args = @_ % 2 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
89
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
0
|
$args{FROM_DATE} or croak 'FROM_DATE is reqired.'; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new('/feeds.php?FEED_ID=6'); |
93
|
0
|
0
|
|
|
|
0
|
$url->query(\%args) if %args; |
94
|
0
|
|
|
|
|
0
|
return $self->request($url->to_string); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub request { |
98
|
0
|
|
|
0
|
1
|
0
|
my ($self, $url, $method, %params) = @_; |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
0
|
|
|
0
|
$method ||= 'GET'; |
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
my $ua = $self->__ua; |
103
|
0
|
|
|
|
|
0
|
my $header = {Authorization => 'Basic ' . b64_encode($self->{user} . ':' . $self->{pass}, '')}; |
104
|
0
|
0
|
|
|
|
0
|
my @extra = %params ? (form => \%params) : (); |
105
|
0
|
|
|
|
|
0
|
my $tx = $ua->build_tx($method => $self->{host} . $url => $header => @extra); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
$tx = $ua->start($tx); |
108
|
|
|
|
|
|
|
# use Data::Dumper; print STDERR Dumper(\$tx); |
109
|
0
|
0
|
0
|
|
|
0
|
if ($tx->res->headers->content_type and $tx->res->headers->content_type =~ 'text/xml') { |
110
|
0
|
|
|
|
|
0
|
return XMLin($tx->res->body); |
111
|
|
|
|
|
|
|
} |
112
|
0
|
0
|
|
|
|
0
|
if (!$tx->success) { |
113
|
0
|
|
|
|
|
0
|
$errstr = $tx->error->{message}; |
114
|
0
|
|
|
|
|
0
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
0
|
$errstr = "Unknown Response."; |
118
|
0
|
|
|
|
|
0
|
return; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
## un-documented helper |
122
|
|
|
|
|
|
|
sub get_affiliate_id_from_token { |
123
|
1
|
|
|
1
|
1
|
606
|
my ($self, $token) = @_; |
124
|
|
|
|
|
|
|
|
125
|
1
|
50
|
|
|
|
12
|
$token or croak 'Must pass a token to get_affiliate_id_from_token.'; |
126
|
|
|
|
|
|
|
|
127
|
0
|
0
|
|
|
|
|
my $token_info = $self->decode_token($token) or return; |
128
|
0
|
|
|
|
|
|
return $token_info->{TOKEN}->{USER_ID}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |
132
|
|
|
|
|
|
|
__END__ |