line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::StackExchange2::V2::Common; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
18
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
13
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
1
|
|
|
1
|
|
5
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
6
|
1
|
|
|
1
|
|
1193
|
use JSON qw(decode_json); |
|
1
|
|
|
|
|
16592
|
|
|
1
|
|
|
|
|
9
|
|
7
|
1
|
|
|
1
|
|
184
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
77
|
|
8
|
1
|
|
|
1
|
|
3631
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
100550
|
|
|
1
|
|
|
|
|
145
|
|
9
|
1
|
|
|
1
|
|
14
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
362
|
|
10
|
1
|
|
|
1
|
|
9
|
use constant BASE_URL => "https://api.stackexchange.com/2.1/"; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
4518
|
|
11
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
our @EXPORT = qw(query no_params one_param two_params); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = "0.05"; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub query { |
18
|
0
|
|
|
0
|
0
|
|
my $queryStrHash = pop @_; |
19
|
|
|
|
|
|
|
# print Dumper(@_); |
20
|
0
|
|
|
|
|
|
my $url = join("/",@_); |
21
|
0
|
|
|
|
|
|
my @params = (); |
22
|
0
|
|
|
|
|
|
while ( my ($key, $value) = each(%$queryStrHash) ) { |
23
|
0
|
|
|
|
|
|
push @params, $key."=".$value; |
24
|
|
|
|
|
|
|
} |
25
|
0
|
|
|
|
|
|
my $query = '?'.join '&',@params; |
26
|
0
|
|
|
|
|
|
my $finalUrl = BASE_URL.$url.$query; |
27
|
0
|
|
|
|
|
|
print $finalUrl; |
28
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
29
|
0
|
|
|
|
|
|
my $response = $ua->get($finalUrl); |
30
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
31
|
|
|
|
|
|
|
#StackExchange2 already returns error codes for |
32
|
|
|
|
|
|
|
#incorrect params and requests |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# if ($response->is_success) { |
35
|
|
|
|
|
|
|
# return decode_json($response->decoded_content); |
36
|
|
|
|
|
|
|
# } |
37
|
|
|
|
|
|
|
# else { |
38
|
|
|
|
|
|
|
# croak $response->status_line; |
39
|
|
|
|
|
|
|
# } |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
sub query_post { |
42
|
0
|
|
|
0
|
0
|
|
my $queryStrHash = pop @_; |
43
|
|
|
|
|
|
|
# print Dumper(%$queryStrHash); |
44
|
|
|
|
|
|
|
#for the purposes of POST we need all the excess query str stuff to go into POST form data |
45
|
0
|
|
|
|
|
|
my $url = join("/",@_); |
46
|
0
|
|
|
|
|
|
my $finalUrl = BASE_URL.$url; |
47
|
0
|
|
|
|
|
|
print $finalUrl; |
48
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
49
|
|
|
|
|
|
|
#apprantly the second param in the post has to be a hash inside an array |
50
|
0
|
|
|
|
|
|
my $response = $ua->post($finalUrl, [%$queryStrHash]); |
51
|
0
|
|
|
|
|
|
return decode_json($response->decoded_content); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
sub no_params { |
54
|
0
|
|
|
0
|
0
|
|
my $param = shift; |
55
|
0
|
|
|
|
|
|
my $config = shift; |
56
|
|
|
|
|
|
|
return sub { |
57
|
0
|
|
|
0
|
|
|
my $self = shift; |
58
|
0
|
|
|
|
|
|
my $queryStr = pop @_; |
59
|
0
|
0
|
|
|
|
|
if(defined($queryStr)) { |
60
|
|
|
|
|
|
|
# copy current method keys and vals |
61
|
0
|
|
|
|
|
|
while(my($key,$val) = each(%$queryStr)) |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
|
|
|
|
$self->{$key} = $val; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
0
|
0
|
0
|
|
|
|
if (defined($config) and $config->{no_site} == 1) { |
67
|
0
|
|
|
|
|
|
$self->{site} = ''; |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
|
return query($param, $self); |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub one_param { |
74
|
0
|
|
|
0
|
0
|
|
my $param1 = shift; |
75
|
0
|
|
|
|
|
|
my $param2 = shift; |
76
|
0
|
|
|
|
|
|
my $config = shift; |
77
|
|
|
|
|
|
|
return sub { |
78
|
0
|
|
|
0
|
|
|
my $self = shift; |
79
|
0
|
|
|
|
|
|
my $ids = shift; |
80
|
|
|
|
|
|
|
#you should check if this param is NOT null/empty |
81
|
0
|
|
|
|
|
|
my $q = shift; |
82
|
|
|
|
|
|
|
|
83
|
0
|
0
|
|
|
|
|
if(defined($q)) { |
84
|
|
|
|
|
|
|
# copy current method keys and vals |
85
|
0
|
|
|
|
|
|
while(my($key,$val) = each(%$q)) |
86
|
|
|
|
|
|
|
{ |
87
|
0
|
|
|
|
|
|
$self->{$key} = $val; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
0
|
|
|
|
|
|
my $ids_str = ""; |
91
|
0
|
0
|
|
|
|
|
if (ref($ids) eq 'ARRAY') { |
92
|
0
|
|
|
|
|
|
$ids_str = join(";",@$ids); |
93
|
|
|
|
|
|
|
} else { |
94
|
0
|
|
|
|
|
|
$ids_str = $ids.""; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
# print Dumper($q); |
97
|
0
|
0
|
|
|
|
|
if (defined($config)) { |
98
|
0
|
0
|
0
|
|
|
|
if(exists $config->{no_site} and $config->{no_site} == 1) { |
99
|
0
|
|
|
|
|
|
$self->{site} = ''; |
100
|
|
|
|
|
|
|
} |
101
|
0
|
0
|
0
|
|
|
|
if(exists $config->{post} and $config->{post} == 1) { |
102
|
|
|
|
|
|
|
#this is a little icky, but should do for now. |
103
|
0
|
|
|
|
|
|
print Dumper($self); |
104
|
0
|
0
|
|
|
|
|
if (not defined($param2)) { |
105
|
0
|
|
|
|
|
|
return query_post($param1, $ids_str , $self); |
106
|
|
|
|
|
|
|
} else { |
107
|
0
|
|
|
|
|
|
return query_post($param1, $ids_str, $param2, $self); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
0
|
0
|
|
|
|
|
if (not defined($param2)) { |
112
|
0
|
|
|
|
|
|
return query($param1, $ids_str , $self); |
113
|
|
|
|
|
|
|
} else { |
114
|
0
|
|
|
|
|
|
return query($param1, $ids_str, $param2, $self); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
0
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
sub two_params { |
119
|
0
|
|
|
0
|
0
|
|
my $param1 = shift; |
120
|
0
|
|
|
|
|
|
my $param2 = shift; |
121
|
0
|
|
|
|
|
|
my $param3 = shift; |
122
|
|
|
|
|
|
|
return sub { |
123
|
0
|
|
|
0
|
|
|
my $self = shift; |
124
|
0
|
|
|
|
|
|
my $ids = shift; |
125
|
0
|
|
|
|
|
|
my $ids_2 = shift; |
126
|
0
|
|
|
|
|
|
my $q = shift; |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
if(defined($q)) { |
129
|
|
|
|
|
|
|
# copy current method keys and vals |
130
|
0
|
|
|
|
|
|
while(my($key,$val) = each(%$q)) |
131
|
|
|
|
|
|
|
{ |
132
|
0
|
|
|
|
|
|
$self->{$key} = $val; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
} |
135
|
0
|
|
|
|
|
|
my $ids_str = ""; |
136
|
0
|
0
|
|
|
|
|
if (ref($ids) eq 'ARRAY') { |
137
|
0
|
|
|
|
|
|
$ids_str = join(";",@$ids); |
138
|
|
|
|
|
|
|
} else { |
139
|
0
|
|
|
|
|
|
$ids_str = $ids.""; |
140
|
|
|
|
|
|
|
} |
141
|
0
|
|
|
|
|
|
my $ids_str_2 = ""; |
142
|
0
|
0
|
|
|
|
|
if (ref($ids_2) eq 'ARRAY') { |
143
|
0
|
|
|
|
|
|
$ids_str_2 = join(";",@$ids_2); |
144
|
|
|
|
|
|
|
} else { |
145
|
0
|
|
|
|
|
|
$ids_str_2 = $ids_2.""; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
# print Dumper($q); |
148
|
|
|
|
|
|
|
# if (defined($config) and $config->{no_site} == 1) { |
149
|
|
|
|
|
|
|
# $self->{site} = ''; |
150
|
|
|
|
|
|
|
# } |
151
|
0
|
0
|
|
|
|
|
if (not defined($param3)) { |
152
|
0
|
|
|
|
|
|
return query($param1, $ids_str, $param2, $ids_str_2, $self); |
153
|
|
|
|
|
|
|
} else { |
154
|
0
|
|
|
|
|
|
return query($param1, $ids_str, $param2, $ids_str_2, $param3, $self); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
0
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
1; # End of Net::StackExchange2::V2::Common |
159
|
|
|
|
|
|
|
__END__ |