summary refs log tree commit diff
path: root/lib/AnyEvent/Riak/Role/HTTPUtils.pm
blob: 1963d884f5d4e80adc703f469ddd2cb226b688d4 (plain) (blame)
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
package AnyEvent::Riak::Role::HTTPUtils;

use Moose::Role;

use AnyEvent;
use AnyEvent::HTTP;
use URI;

use MIME::Base64;

has client_id   => (
    is  => 'rw',
    isa => 'Str',
    default =>
      sub { "perl_anyevent_riak" . encode_base64(int(rand(10737411824)), '') }
);

sub _build_uri {
    my ($self, $host, $path, $options) = @_;
    my $uri = URI->new($host);
    $uri->path(join("/", @$path));
    $uri->query_form($self->_build_query($options));
    warn $uri->as_string;
    return $uri->as_string;
}

sub _build_headers {
    my ($self, $options) = @_;
    my $headers = delete $options->{headers} || {};

    warn $self->client_id;
    $headers->{'X-Riak-ClientId'} = $self->client_id;
    $headers->{'Content-Type'}    = 'application/json'
      unless exists $headers->{'Content-Type'};
    return $headers;
}

sub _build_query {
    my ($self, $options) = @_;
    my $valid_options = [qw/props keys returnbody/];
    my $query;
    foreach (@$valid_options) {
        $query->{$_} = $options->{$_} if exists $options->{$_};
    }
    $query;
}

1;