File Coverage

blib/lib/Data/Riak/Fast/HTTP.pm
Criterion Covered Total %
statement 57 65 87.6
branch 7 14 50.0
condition n/a
subroutine 15 15 100.0
pod 2 3 66.6
total 81 97 83.5


line stmt bran cond sub pod time code
1             package Data::Riak::Fast::HTTP;
2             # ABSTRACT: An interface to a Riak server, using its HTTP (REST) interface
3              
4 22     22   16431 use Mouse;
  22         661598  
  22         135  
5              
6 22     22   38358 use Furl;
  22         823021  
  22         713  
7 22     22   23020 use Net::DNS::Lite;
  22         207309  
  22         1309  
8 22     22   21308 use Cache::LRU;
  22         16075  
  22         674  
9 22     22   21699 use HTTP::Headers;
  22         200734  
  22         1042  
10 22     22   33761 use HTTP::Response;
  22         451726  
  22         919  
11 22     22   33887 use HTTP::Request;
  22         18633  
  22         710  
12              
13 22     22   10032 use Data::Riak::Fast;
  22         81  
  22         624  
14 22     22   14310 use Data::Riak::Fast::HTTP::Request;
  22         73  
  22         683  
15 22     22   13238 use Data::Riak::Fast::HTTP::Response;
  22         67  
  22         18272  
16              
17             =head2 host
18              
19             The host the Riak server is on. Can be set via the environment variable
20             DATA_RIAK_HTTP_HOST, and defaults to 127.0.0.1.
21              
22             =cut
23              
24             has host => (
25             is => 'ro',
26             isa => 'Str',
27             default => sub {
28             $ENV{'DATA_RIAK_HTTP_HOST'} || '127.0.0.1';
29             }
30             );
31              
32             =head2 port
33              
34             The port of the host that the riak server is on. Can be set via the environment
35             variable DATA_RIAK_HTTP_PORT, and defaults to 8098.
36              
37             =cut
38              
39             has port => (
40             is => 'ro',
41             isa => 'Int',
42             default => sub {
43             $ENV{'DATA_RIAK_HTTP_PORT'} || '8098';
44             }
45             );
46              
47             =head2 timeout
48              
49             The maximum value (in seconds) that a request can go before timing out. Can be set
50             via the environment variable DATA_RIAK_HTTP_TIMEOUT, and defaults to 15.
51              
52             =cut
53              
54             has timeout => (
55             is => 'ro',
56             isa => 'Num',
57             default => sub {
58             $ENV{'DATA_RIAK_HTTP_TIMEOUT'} || '15';
59             }
60             );
61              
62             =head2 user_agent
63              
64             This is the instance of L we use to talk to Riak.
65              
66             =cut
67              
68             =head1 METHOD
69             =head2 base_uri
70              
71             The base URI for the Riak server.
72              
73             =cut
74              
75             sub base_uri {
76 17     17 0 40 my $self = shift;
77 17         364 return sprintf('http://%s:%s/', $self->host, $self->port);
78             }
79              
80             =head2 ping
81              
82             Tests to see if the specified Riak server is answering. Returns 0 for no, 1 for yes.
83              
84             =cut
85              
86             sub ping {
87 17     17 1 42 my $self = shift;
88 17         137 my ($response,) = $self->send({ method => 'GET', uri => 'ping' });
89 17 50       295 return 0 unless($response->code eq '200');
90 0         0 return 1;
91             }
92              
93             =head2 send ($request)
94              
95             Send a Data::Riak::Fast::HTTP::Request to the server. If you pass in a hashref, it will
96             create the Request object for you on the fly.
97              
98             =cut
99              
100             sub send {
101 17     17 1 44 my ($self, $request) = @_;
102 17 50       147 unless(blessed $request) {
103 17         214 $request = Data::Riak::Fast::HTTP::Request->new($request);
104             }
105 17         970 my ($response, $url) = $self->_send($request);
106 17         405 return $response, $url;
107             }
108              
109             sub _send {
110 17     17   46 my ($self, $request) = @_;
111              
112 17         75 my $uri = URI->new( sprintf('%s%s', $self->base_uri, $request->uri) );
113              
114 17 50       159419 if ($request->has_query) {
115 0         0 $uri->query_form($request->query);
116             }
117              
118 17         49 my @headers;
119 17 50       299 push @headers, 'Accept' => $request->accept if $request->method eq 'GET';
120 17 50       118 push @headers, 'Content-Type' => $request->content_type if $request->method =~ /^(POST|PUT)$/;
121 17 50       3130 if(my $links = $request->links) {
122 0         0 push @headers, 'Link' => $request->links;
123             }
124              
125 17 50       547 if(my $indexes = $request->indexes) {
126 0         0 foreach my $index (@{$indexes}) {
  0         0  
127 0         0 my $field = $index->{field};
128 0         0 my $values = $index->{values};
129 0         0 push @headers, ":X-Riak-Index-$field" => $values;
130             }
131             }
132              
133 17         206 $Net::DNS::Lite::CACHE = Cache::LRU->new(
134             size => 256,
135             );
136              
137 17         541 my $furl = Furl::HTTP->new(
138             agent => "Data::Riak::Fast/$Data::Riak::Fast::VERSION",
139             timeout => $self->timeout,
140             inet_aton => \&Net::DNS::Lite::inet_aton,
141             );
142 17         1286 my ( $mv, $code, $msg, $headers, $content ) = $furl->request(
143             method => $request->method,
144             url => $uri->as_string,
145             headers => \@headers,
146             content => $request->data,
147             );
148 17         19584 my $http_response = HTTP::Response->new($code, $msg, $headers, $content);
149              
150 17         3658 my $response = Data::Riak::Fast::HTTP::Response->new({
151             http_response => $http_response
152             });
153              
154 17         1856 return $response, $uri;
155             }
156              
157             =begin :postlude
158              
159             =head1 ACKNOWLEDGEMENTS
160              
161              
162             =end :postlude
163              
164             =cut
165              
166             __PACKAGE__->meta->make_immutable;
167 22     22   165 no Mouse;
  22         44  
  22         135  
168              
169             1;