Client/Request.pm

filter: Statementstmt 57.8% 81 / 140 Branchbran 33.3% 16 / 48 Conditioncond 50.0% 12 / 24 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 87.5% 14 / 16 Podpod 66.6% 4 / 6 totaltotal 52.2% 129 / 247 CC 36 SCAR 28.9
CC
36
Cov
51%
build_request
40.3
token_request
20.0
send
18.8
CRAP 18.1
« Client/MoreComments.pm ↑ Summary Client/Thing.pm »
1 package Reddit::Client::Request;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use JSON qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use LWP::UserAgent qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Request qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use URI::Encode qw/uri_encode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use URI::Escape qw/uri_escape/; # next update, also line 122
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 require Reddit::Client;
15  
16 5 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 'user_agent',
18 'method',
19 'url',
20 'query',
21 'post_data',
22 'cookie',
23 'modhash',
24 'token',
25 'tokentype',
26 'request_errors',
27 'print_response',
28 'print_request',
29 'print_request_on_error',
30 'last_token',
31 5 );
32  
33 3 ▶sub new {
Subroutine new: called CC=12 SCAR=25.8
Pod documented
34 3 my ($class, %param) = @_;
35 3 my $self = fields::new($class);
36 3 ▶ $self->{user_agent} = $param{user_agent} || croak 'Expected "user_agent"';
Condition100%2/2
$param{'user_agent'} || croak('Expected "user_agent"')
l!l
21
MC/DC100%1/1
$param{'user_agent'} || croak('Expected "user_agent"')
$param{'user_agent'}
Truth table100%2/2
A
A= $param{'user_agent'}
Aresult
11
00
37 2 ▶ $self->{url} = $param{url} || croak 'Expected "url"';
Condition100%2/2
$param{'url'} || croak('Expected "url"')
l!l
11
MC/DC100%1/1
$param{'url'} || croak('Expected "url"')
$param{'url'}
Truth table100%2/2
A
A= $param{'url'}
Aresult
11
00
38 1 $self->{query} = $param{query};
39 1 $self->{post_data} = $param{post_data};
40 1 $self->{cookie} = $param{cookie};
41 1 $self->{modhash} = $param{modhash};
42 1 $self->{token} = $param{token};
43 1 $self->{tokentype} = $param{tokentype};
44 1 ▶ $self->{request_errors} = $param{request_errors} || 0;
Condition50%1/2
$param{'request_errors'} || 0
l!l
01
MC/DC0%0/1
$param{'request_errors'} || 0
$param{'request_errors'}
Truth table50%1/2
A
A= $param{'request_errors'}
Aresult
11
00
45 1 ▶ $self->{print_response} = $param{print_response} || 0;
Condition50%1/2
$param{'print_response'} || 0
l!l
01
MC/DC0%0/1
$param{'print_response'} || 0
$param{'print_response'}
Truth table50%1/2
A
A= $param{'print_response'}
Aresult
11
00
46 1 ▶ $self->{print_request} = $param{print_request} || 0;
Condition50%1/2
$param{'print_request'} || 0
l!l
01
MC/DC0%0/1
$param{'print_request'} || 0
$param{'print_request'}
Truth table50%1/2
A
A= $param{'print_request'}
Aresult
11
00
47 1 ▶ $self->{print_request_on_error} = $param{print_request_on_error} || 0;
Condition50%1/2
$param{'print_request_on_error'} || 0
l!l
01
MC/DC0%0/1
$param{'print_request_on_error'} || 0
$param{'print_request_on_error'}
Truth table50%1/2
A
A= $param{'print_request_on_error'}
Aresult
11
00
48 1 $self->{last_token} = $param{last_token};
49  
50 1 ▶ if (defined $self->{query}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"query"}) 1 0 1
51 1 ▶ ref $self->{query} eq 'HASH' || croak 'Expected HASH ref for "query"';
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"query"} eq "HASH" or croak("Expected HASH ref for \"query\"") 1 0 1
52 1 $self->{url} = sprintf('%s?%s', $self->{url}, build_query($self->{query}))
53 }
54  
55 1 ▶ if (defined $self->{post_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"post_data"}) 1 0 1
56 1 ▶ ref $self->{post_data} eq 'HASH' || croak 'Expected HASH ref for "post_data"';
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{"post_data"} eq "HASH" or croak("Expected HASH ref for \"post_data\"") 1 0 1
57 }
58  
59 1 ▶ $self->{method} = $param{method} || 'GET';
Condition50%1/2
$param{'method'} || 'GET'
l!l
10
MC/DC0%0/1
$param{'method'} || 'GET'
$param{'method'}
Truth table50%1/2
A
A= $param{'method'}
Aresult
11
00
60 1 $self->{method} = uc $self->{method};
61  
62 1 return $self;
63 }
64  
65 8 ▶sub build_query {
Subroutine build_query: called CC=2 SCAR=6.9
Pod documented
66 8 ▶ my $param = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $param = shift() or return "" 7 1 8
67 7 my $opt = { encode_reserved => 1 };
68 7 join '&', map {uri_encode($_, $opt) . '=' . uri_encode($param->{$_}, $opt)} sort keys %$param;
69 }
70  
71 3 ▶sub build_request {
Subroutine build_request: called CC=12 SCAR=37.0
Pod documented
72 3 my $self = shift;
73 3 my $request = HTTP::Request->new();
74  
75 3 $request->uri($self->{url});
76 3 ▶ $request->header("Authorization"=> "$self->{tokentype} $self->{token}") if $self->{tokentype} && $self->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tokentype"} and $self->{"token"} 0 3 3
Condition33%1/3
$self->{"tokentype"} and $self->{"token"}
!ll&&!rl&&r
300
MC/DC0%0/2
$self->{"tokentype"} and $self->{"token"}
$self->{"tokentype"} $self->{"token"}
Truth table33%1/3
A and B
A= $self->{"tokentype"}B= $self->{"token"}
ABresult
0X0
100
111
77  
78 3 ▶ if ($self->{method} eq 'POST') {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'method'} eq 'POST') { } 3 0 3
elsif ($self->{'method'} eq 'DELETE') { } 0 0 0
elsif ($self->{'method'} eq 'PUT') { } 0 0 0
79 3 ▶ my $post_data = $self->{post_data} || {};
Condition50%1/2
$self->{'post_data'} || {}
l!l
30
MC/DC0%0/1
$self->{'post_data'} || {}
$self->{'post_data'}
Truth table50%1/2
A
A= $self->{'post_data'}
Aresult
11
00
80 3 ▶ $post_data->{modhash} = $self->{modhash} if $self->{modhash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"modhash"} 3 0 3
81 3 ▶ $post_data->{uh} = $self->{modhash} if $self->{modhash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"modhash"} 3 0 3
82  
83 3 $request->method('POST');
84 3 $request->content_type('application/x-www-form-urlencoded');
85 3 $request->content(build_query($post_data));
86 } elsif ($self->{method} eq 'DELETE') {
87 0 $request->method('DELETE');
88 } elsif ($self->{method} eq 'PUT') {
89 0 ▶ my $post_data = $self->{post_data} || {};
Condition0%0/2
$self->{'post_data'} || {}
l!l
00
MC/DC0%0/1
$self->{'post_data'} || {}
$self->{'post_data'}
Truth table0%0/2
A
A= $self->{'post_data'}
Aresult
11
00
90 0 ▶ $post_data->{modhash} = $self->{modhash} if $self->{modhash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"modhash"} 0 0 0
91 0 ▶ $post_data->{uh} = $self->{modhash} if $self->{modhash};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"modhash"} 0 0 0
92  
93 0 $request->method('PUT');
94 0 $request->content_type('application/x-www-form-urlencoded');
95 0 $request->content(build_query($post_data));
96 } else {
97 0 $request->method('GET');
98 }
99  
100 3 return $request;
101 }
102  
103 2 ▶sub send {
Subroutine send: called CC=8 SCAR=29.3
Pod documented
104 2 my $self = shift;
105 2 my $request = $self->build_request;
106  
107 2 Reddit::Client::DEBUG('%4s request to %s', $self->{method}, $self->{url});
108  
109 2 my $ua = LWP::UserAgent->new(agent => $self->{user_agent}, env_proxy => 1);
110 2 my $res = $ua->request($request);
111  
112 #print Dumper($res); # slightly more redable because more compact
113 2 ▶ if ($self->{print_request}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'print_request'}) { } 0 2 2
elsif ($self->{'print_response'}) { } 0 2 2
114 0 print Dumper($request);
115 0 print Dumper($res);
116 } elsif ($self->{print_response}) {
117 0 print $res->content . "\n";
118 }
119  
120 # response is an HTTP::Response object, sent is HTTP::Request
121  
122 2 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 1 2
123 1 return $res->content;
124 } else {
125 # I don't know what the fuck any of this is
126 # print request unless we already printed it
127 1 ▶ if ($self->{print_request_on_error} and !$self->{print_request}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'print_request_on_error'} and not $self->{'print_request'}) { } 0 1 1
elsif ($self->{'request_errors'}) { } 0 1 1
Condition33%1/3
$self->{"print_request_on_error"} and not $self->{"print_request"}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"print_request_on_error"} and not $self->{"print_request"}
$self->{"print_request_on_error"} not $self->{"print_request"}
Truth table33%1/3
A and not(B)
A= $self->{"print_request_on_error"}B= not $self->{"print_request"}
ABresult
0X0
100
111
128 0 print Dumper($request);
129 0 print Dumper($res);
130 } elsif ($self->{request_errors}) {
131 0 my $json;
132 0 my $success = eval { $json = decode_json $res->{_content}; };
133  
134 # If Reddit returned valid json, add it to a hash and print it
135 0 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 0 0
136 0 my $err = {
137 error => 1,
138 code => $res->code,
139 status_line => $res->status_line,
140 data => $json,
141 };
142
143 0 my $rtn = encode_json $err;
144 0 die "$rtn\n";
145  
146 } else {
147 0 die "Request error: HTTP ".$res->status_line .", Content: $res->{_content}";
148 }
149 #die $res->{_content}."\n";
150 } else { # default: print status line and exit
151 #croak sprintf("Request error: HTTP %s last token: %s time: %s", $res->status_line, $self->{last_token}, time);
152 1 die sprintf("Request error: HTTP %s\n", $res->status_line);
153 }
154 }
155 }
156  
157 0 ▶sub token_request {
Subroutine token_request: not called CC=4 SCAR=30.0
Pod undocumented
158 0 my ($self, %param) = @_;
159  
160 0 my $url = "https://$param{client_id}:$param{secret}\@www.reddit.com/api/v1/access_token";
161  
162 0 my $ua = LWP::UserAgent->new(agent => $param{user_agent});
163 0 my $req = HTTP::Request->new(POST => $url);
164 0 $req->header('content-type' => 'application/x-www-form-urlencoded');
165  
166 #my $postdata = "grant_type=password&username=$username&password=$password";
167 0 my $postdata;
168
169 0 ▶ if ($param{auth_type} eq 'script') {
Branch: 2 branches
BranchTrueFalseTotal
if ($param{'auth_type'} eq 'script') { } 0 0 0
elsif ($param{'auth_type'} eq 'webapp') { } 0 0 0
170 0 $postdata = "grant_type=password&username=$param{username}&password=" . uri_escape($param{password});
171 } elsif ($param{auth_type} eq 'webapp') {
172 0 $postdata = "grant_type=refresh_token&refresh_token=".uri_escape($param{refresh_token});
173 0 } else { die "Request:token_request: invalid auth type"; }
174  
175 0 $req->content($postdata);
176  
177 0 my $res = $ua->request($req);
178  
179 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
180 0 return $res->decoded_content;
181 } else {
182 # this is sometimes called in static context
183 #if ($self->{request_errors}) {
184 # croak "Request error: HTTP ".$res->status_line .", Content: $res->{_content}";
185 #} else {
186 0 croak sprintf("Request error: HTTP %s", $res->status_line);
187 #}
188 #croak sprintf('Request error: HTTP %s', $res->status_line);
189 }
190 }
191  
192 0 ▶sub refresh_token_request {
Subroutine refresh_token_request: not called CC=3 SCAR=24.8
Pod undocumented
193 0 my ($self, %data) = @_;
194  
195 # create user agent
196 0 my $ua = new LWP::UserAgent( agent=> $data{ua} );
197 # create new request
198 0 my $request = new HTTP::Request();
199 # set the request method
200 0 $request->method("POST");
201 # set request url
202 0 my $url = "https://$data{client_id}:$data{secret}\@www.reddit.com/api/v1/access_token";
203 0 $request->uri($url);
204  
205 my $reqdata = {
206 grant_type => 'authorization_code',
207 code => $data{code},
208 redirect_uri=> $data{redirect_uri},
209 0 duration => 'permanent',
210 };
211  
212 0 $request->content_type('application/x-www-form-urlencoded');
213  
214 0 my $opt = { encode_reserved => 1 };
215 0 my $encoded = join '&', map { uri_encode($_, $opt) . '=' . uri_encode($reqdata->{$_}, $opt) } sort keys %$reqdata;
216  
217 0 $request->content($encoded);
218  
219 0 my $result = $ua->request($request);
220  
221 0 ▶ if ($data{print_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"print_request"}) 0 0 0
222 0 print Dumper($request);
223 0 print Dumper($result);
224 }
225  
226 0 ▶ if ($result->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_success) { } 0 0 0
227 0 my $j = decode_json $result->content;
228 0 my $tok = $j->{refresh_token};
229  
230 0 return $tok;
231 } else {
232 0 print "Request error: HTTP ".$result->status_line.", Content:\n$result->{_content}\n";
233 0 print "refresh_token_request: something went wrong. To aid in debugging, you can set 'print_request' to true when creating a new Reddit::Client object. This will print the entire content of the request and response.\n";
234  
235 0 die;
236 }
237  
238 }
239  
240 5 1;
241  
242 __END__
« Client/MoreComments.pm ↑ Summary Client/Thing.pm »