File Coverage

blib/lib/WWW/Kickstarter/HttpClient/Lwp.pm
Criterion Covered Total %
statement 24 46 52.1
branch 0 8 0.0
condition n/a
subroutine 8 10 80.0
pod 0 2 0.0
total 32 66 48.4


line stmt bran cond sub pod time code
1              
2             package WWW::Kickstarter::HttpClient::Lwp;
3              
4 1     1   4285 use strict;
  1         2  
  1         27  
5 1     1   4 use warnings;
  1         32  
  1         49  
6 1     1   19 no autovivification;
  1         3  
  1         7  
7              
8              
9 1     1   439 use HTTP::Headers qw( );
  1         6825  
  1         30  
10 1     1   400 use HTTP::Request::Common qw( GET POST );
  1         10521  
  1         69  
11 1     1   304 use LWP::Protocol::https qw( );
  1         117403  
  1         33  
12 1     1   392 use LWP::UserAgent qw( );
  1         9666  
  1         25  
13 1     1   7 use WWW::Kickstarter::Error qw( my_croak );
  1         2  
  1         292  
14              
15              
16             sub new {
17 0     0 0   my ($class, %opts) = @_;
18              
19 0           my $agent = delete($opts{agent});
20              
21 0 0         if (my @unrecognized = keys(%opts)) {
22 0           my_croak(400, "Unrecognized parameters @unrecognized");
23             }
24              
25 0           my $http_client = LWP::UserAgent->new(
26             default_headers => HTTP::Headers->new(
27             Accept => 'application/json; charset=utf-8',
28             ),
29             env_proxy => 1,
30             );
31 0 0         $http_client->agent($agent) if $agent;
32              
33 0           my $self = bless({}, $class);
34 0           $self->{http_client} = $http_client;
35 0           return $self;
36             }
37              
38              
39             sub request {
40 0     0 0   my ($self, $method, $url, $req_content) = @_;
41              
42 0           my $http_request;
43 0 0         if ($method eq 'GET' ) {
    0          
44 0           $http_request = GET($url);
45             }
46             elsif ($method eq 'POST') {
47 0           $http_request = POST($url,
48             Content_Length => length($req_content),
49             Content_Type => 'application/x-www-form-urlencoded',
50             Content => $req_content,
51             );
52             }
53             else {
54 0           my_croak(400, "Unexpected argument");
55             }
56              
57 0           my $http_response = $self->{http_client}->request($http_request);
58              
59 0           my $status_code = $http_response->code();
60 0           my $status_line = $http_response->status_line();
61 0           my $content_type = $http_response->content_type(); # Lowercase with "parameters" filtered out.
62 0           my $content_encoding = $http_response->content_type_charset();
63 0           my $content = $http_response->decoded_content( charset => 'none' );
64              
65 0           return ( $status_code, $status_line, $content_type, $content_encoding, $content );
66             }
67              
68              
69             1;
70              
71              
72             __END__