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