Tatsumaki/HTTPClient.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 74 CC 1 SCAR 6.9
CC
1
Cov
0%
get
2.0
head
2.0
post
2.0
CRAP 2.0
1 package Tatsumaki::HTTPClient;
2 use strict;
3 use AnyEvent::HTTP ();
4 use HTTP::Request::Common ();
5 use HTTP::Request;
6 use HTTP::Response;
7 use Tatsumaki;
8 use Any::Moose;
9  
10 has timeout => (is => 'rw', isa => 'Int', default => sub { 30 });
11 has agent => (is => 'rw', isa => 'Str', default => sub { join "/", __PACKAGE__, $Tatsumaki::VERSION });
12  
13 sub get { _request(GET => @_) }
14 sub head { _request(HEAD => @_) }
15 sub post { _request(POST => @_) }
16 sub put { _request(PUT => @_) }
17 sub delete { _request(DELETE => @_) }
18  
19 sub _request {
20 my $cb = pop;
21 my $method = shift;
22 my $self = shift;
23 no strict 'refs';
24 my $req = &{"HTTP::Request::Common::$method"}(@_);
25 $self->request($req, $cb);
26 }
27  
28 sub request {
29 my($self, $request, $cb) = @_;
30  
31 my $headers = $request->headers;
32 $headers->{'user-agent'} = $self->agent;
33  
34 my %options = (
35 timeout => $self->timeout,
36 headers => $headers,
37 body => $request->content,
38 );
39  
40 AnyEvent::HTTP::http_request $request->method, $request->uri, %options, sub {
41 my($body, $header) = @_;
42 my $res = HTTP::Response->new($header->{Status}, $header->{Reason}, [ %$header ], $body);
43 $cb->($res);
44 };
45 }
46  
47 no Any::Moose;
48 __PACKAGE__->meta->make_immutable;
49  
50 1;