Data/Riak/HTTP.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Data/Riak/Bucket.pm ↑ Summary Data/Riak/Link.pm »
1 package Data::Riak::HTTP;
2 {
3 $Data::Riak::HTTP::VERSION = '2.0';
4 }
5 # ABSTRACT: An interface to a Riak server, using its HTTP (REST) interface
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Carp 'cluck';
12  
13 use LWP::UserAgent;
14 use LWP::ConnCache;
15 use HTTP::Headers;
16 use HTTP::Response;
17 use HTTP::Request;
18  
19 use Data::Riak::HTTP::Request;
20 use Data::Riak::HTTP::Response;
21 use Data::Riak::HTTP::ExceptionHandler::Default;
22  
23 use namespace::autoclean;
24  
25  
26 {
27 my ($warned_host_env, $warned_host_default);
28  
29 has host => (
30 is => 'ro',
31 isa => 'Str',
32 default => sub {
33 if (exists $ENV{DATA_RIAK_HTTP_HOST}) {
34 cluck 'Environment variable DATA_RIAK_HTTP_HOST is deprecated'
35 unless $warned_host_env;
36  
37 return $ENV{DATA_RIAK_HTTP_HOST};
38 }
39  
40 cluck 'host defaulting to localhost is deprecated'
41 unless $warned_host_default;
42  
43 return '127.0.0.1';
44 }
45 );
46 }
47  
48  
49 {
50 my ($warned_port_env, $warned_port_default);
51  
52 has port => (
53 is => 'ro',
54 isa => 'Int',
55 default => sub {
56 if (exists $ENV{DATA_RIAK_HTTP_PORT}) {
57 cluck 'Environment variable DATA_RIAK_HTTP_PORT is deprecated'
58 unless $warned_port_env;
59  
60 return $ENV{DATA_RIAK_HTTP_PORT};
61 }
62  
63 cluck 'port defaulting to 8098 is deprecated'
64 unless $warned_port_default;
65  
66 return '8098';
67 }
68 );
69 }
70  
71  
72 {
73 my $warned_timeout_env;
74  
75 has timeout => (
76 is => 'ro',
77 isa => 'Int',
78 default => sub {
79 if (exists $ENV{DATA_RIAK_HTTP_TIMEOUT}) {
80 cluck 'Environment variable DATA_RIAK_HTTP_TIMEOUT is deprecated'
81 unless $warned_timeout_env;
82  
83 return $ENV{DATA_RIAK_HTTP_TIMEOUT};
84 }
85  
86 return '15';
87 }
88 );
89 };
90  
91  
92 our $CONN_CACHE;
93  
94 has user_agent => (
95 is => 'ro',
96 isa => 'LWP::UserAgent',
97 lazy => 1,
98 default => sub {
99 my $self = shift;
100  
101 # NOTE:
102 # Much of the following was copied from
103 # Net::Riak (franck cuny++ && robin edwards++)
104 # - SL
105  
106 # The Links header Riak returns (esp. for buckets) can get really long,
107 # so disable limits LWP puts on the length of response lines
108 # (default = 8192)
109 my %opts = @LWP::Protocol::http::EXTRA_SOCK_OPTS;
110 $opts{MaxLineLength} = 0;
111 @LWP::Protocol::http::EXTRA_SOCK_OPTS = %opts;
112  
113 my $ua = LWP::UserAgent->new(
114 timeout => $self->timeout,
115 keep_alive => 1,
116 );
117  
118 $CONN_CACHE ||= LWP::ConnCache->new;
119  
120 $ua->conn_cache( $CONN_CACHE );
121  
122 $ua;
123 }
124 );
125  
126  
127 sub send {
128 my ($self, $request) = @_;
129  
130 my $http_request = $self->create_request($request);
131 my $http_response = $self->_send($http_request);
132  
133 $self->exception_handler->try_handle_exception(
134 $request, $http_request, $http_response,
135 );
136  
137 return $http_response;
138 }
139  
140 sub _send {
141 my ($self, $request) = @_;
142  
143 my $uri = URI->new( sprintf('%s%s', $self->base_uri, $request->uri) );
144  
145 if ($request->has_query) {
146 $uri->query_form($request->query);
147 }
148  
149 my $headers = HTTP::Headers->new(
150 'X-Riak-ClientId' => $self->client_id,
151 ($request->method eq 'GET' ? ('Accept' => $request->accept) : ()),
152 ($request->method eq 'POST' || $request->method eq 'PUT' ? ('Content-Type' => $request->content_type) : ()),
153 %{ $request->headers },
154 );
155  
156 if(my $links = $request->links) {
157 $headers->header('Link' => $request->links);
158 }
159  
160 if(my $indexes = $request->indexes) {
161 foreach my $index (@{$indexes}) {
162 my $field = $index->{field};
163 my $values = $index->{values};
164 $headers->header(":X-Riak-Index-$field" => $values);
165 }
166 }
167  
168 my $http_request = HTTP::Request->new(
169 $request->method => $uri->as_string,
170 $headers,
171 $request->data
172 );
173  
174 my $http_response = $self->user_agent->request($http_request);
175  
176 my $response = Data::Riak::HTTP::Response->new({
177 http_response => $http_response
178 });
179  
180 return $response;
181 }
182  
183 with 'Data::Riak::Transport::HTTP';
184  
185  
186 __PACKAGE__->meta->make_immutable;
187  
188 1;
189  
190 __END__
« Data/Riak/Bucket.pm ↑ Summary Data/Riak/Link.pm »