1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package Net::Riak::Role::REST;
# ABSTRACT: role for REST operations
use URI;
use Moose::Role;
use MooseX::Types::Moose 'Bool';
use Net::Riak::Types qw/HTTPResponse HTTPRequest/;
use Data::Dump 'pp';
with qw/Net::Riak::Role::REST::Bucket
Net::Riak::Role::REST::Object
Net::Riak::Role::REST::Link
Net::Riak::Role::REST::MapReduce
Net::Riak::Role::REST::Search
/;
has http_request => (
is => 'rw',
isa => HTTPRequest,
);
has http_response => (
is => 'rw',
isa => HTTPResponse,
handles => {
is_success => 'is_success',
status => 'code',
}
);
has disable_return_body => (
is => 'rw',
isa => Bool,
default => 0
);
sub _build_path {
my ($self, $path) = @_;
$path = join('/', @$path);
}
sub _build_uri {
my ($self, $path, $params) = @_;
my $uri = URI->new($self->get_host);
$uri->path($self->_build_path($path));
$uri->query_form(%$params);
$uri;
}
# constructs a HTTP::Request
sub new_request {
my ($self, $method, $path, $params) = @_;
my $uri = $self->_build_uri($path, $params);
return HTTP::Request->new($method => $uri);
}
# makes a HTTP::Request returns and stores a HTTP::Response
sub send_request {
my ($self, $req) = @_;
$self->http_request($req);
my $r = $self->useragent->request($req);
$self->http_response($r);
if ($ENV{RIAK_VERBOSE}) {
print STDERR pp($r);
}
return $r;
}
sub is_alive {
my $self = shift;
my $request = $self->new_request('HEAD', ['ping']);
my $response = $self->send_request($request);
$self->is_success ? return 1 : return 0;
}
sub all_buckets {
my $self = shift;
my $request = $self->new_request('GET', [$self->prefix], {buckets => 'true'});
my $response = $self->send_request($request);
die "Failed to fetch buckets.. are you running riak 0.14+?"
unless $response->is_success;
my $resp = JSON::decode_json($response->content);
return ref ($resp->{buckets}) eq 'ARRAY' ? @{$resp->{buckets}} : ();
}
sub server_info { die "->server_info not supported by the REST interface" }
sub stats {
my $self = shift;
my $request = $self->new_request('GET', ["stats"]);
my $response = $self->send_request($request);
return JSON::decode_json($response->content);
}
1;
|