| 1 | package Data::Riak::Async::HTTP; | ||
| 2 | { | ||
| 3 | $Data::Riak::Async::HTTP::VERSION = '2.0'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | use AnyEvent::HTTP; | ||
| 8 | use HTTP::Headers; | ||
| 9 | use HTTP::Request; | ||
| 10 | use HTTP::Response; | ||
| 11 | use Data::Riak::HTTP::Request; | ||
| 12 | use Data::Riak::HTTP::Response; | ||
| 13 | use Data::Riak::HTTP::ExceptionHandler::Default; | ||
| 14 | use namespace::autoclean; | ||
| 15 | |||
| 16 | has host => ( | ||
| 17 | is => 'ro', | ||
| 18 | isa => 'Str', | ||
| 19 | required => 1, | ||
| 20 | ); | ||
| 21 | |||
| 22 | has port => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Int', | ||
| 25 | required => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | has timeout => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Int', | ||
| 31 | default => 15, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has request_arguments => ( | ||
| 35 | traits => ['Hash'], | ||
| 36 | isa => 'HashRef', | ||
| 37 | default => sub { +{} }, | ||
| 38 | handles => { | ||
| 39 | request_arguments => 'elements', | ||
| 40 | }, | ||
| 41 | ); | ||
| 42 | |||
| 43 | sub send { | ||
| 44 | my ($self, $request, $cb, $error_cb) = @_; | ||
| 45 | |||
| 46 | my $http_request = $self->create_request($request); | ||
| 47 | |||
| 48 | $self->_send($http_request, sub { | ||
| 49 | my ($http_response) = @_; | ||
| 50 | |||
| 51 | my $e = $self->exception_handler->try_build_exception( | ||
| 52 | $request, $http_request, $http_response, | ||
| 53 | ); | ||
| 54 | return $error_cb->($e) if $e; | ||
| 55 | |||
| 56 | $cb->($http_response); | ||
| 57 | }, $error_cb); | ||
| 58 | |||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _send { | ||
| 63 | my ($self, $request, $cb, $error_cb) = @_; | ||
| 64 | |||
| 65 | my $uri = URI->new( sprintf('%s%s', $self->base_uri, $request->uri) ); | ||
| 66 | |||
| 67 | if ($request->has_query) { | ||
| 68 | $uri->query_form($request->query); | ||
| 69 | } | ||
| 70 | |||
| 71 | my $headers = HTTP::Headers->new( | ||
| 72 | 'X-Riak-ClientId' => $self->client_id, | ||
| 73 | ($request->method eq 'GET' ? ('Accept' => $request->accept) : ()), | ||
| 74 | ($request->method eq 'POST' || $request->method eq 'PUT' ? ('Content-Type' => $request->content_type) : ()), | ||
| 75 | %{ $request->headers }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | if(my $links = $request->links) { | ||
| 79 | $headers->header('Link' => $request->links); | ||
| 80 | } | ||
| 81 | |||
| 82 | if(my $indexes = $request->indexes) { | ||
| 83 | foreach my $index (@{$indexes}) { | ||
| 84 | my $field = $index->{field}; | ||
| 85 | my $values = $index->{values}; | ||
| 86 | $headers->header(":X-Riak-Index-$field" => $values); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | my $http_request = HTTP::Request->new( | ||
| 91 | $request->method => $uri->as_string, | ||
| 92 | $headers, | ||
| 93 | $request->data, | ||
| 94 | ); | ||
| 95 | |||
| 96 | $self->_send_via_anyevent_http($http_request, $cb, $error_cb); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _send_via_anyevent_http { | ||
| 100 | my ($self, $http_request, $cb, $error_cb) = @_; | ||
| 101 | |||
| 102 | my %plain_headers = ( | ||
| 103 | (map { | ||
| 104 | (my $normalised = $_) =~ s/^://; | ||
| 105 | # In scalar context ->header will join multiple values for a field | ||
| 106 | # together with a comma and do the appropriate escaping. | ||
| 107 | ($normalised => scalar $http_request->headers->header($_)); | ||
| 108 | } $http_request->headers->header_field_names), | ||
| 109 | Referer => undef, | ||
| 110 | ); | ||
| 111 | |||
| 112 | http_request $http_request->method, $http_request->uri, | ||
| 113 | $self->request_arguments, | ||
| 114 | timeout => $self->timeout, | ||
| 115 | headers => \%plain_headers, | ||
| 116 | body => $http_request->content, | ||
| 117 | sub { | ||
| 118 | my ($body, $hdr) = @_; | ||
| 119 | |||
| 120 | my $http_response = HTTP::Response->new( | ||
| 121 | delete $hdr->{Status}, delete $hdr->{Reason}, | ||
| 122 | HTTP::Headers->new(%{ $hdr }), $body, | ||
| 123 | ); | ||
| 124 | $http_response->request($http_request); | ||
| 125 | |||
| 126 | my $response = Data::Riak::HTTP::Response->new({ | ||
| 127 | http_response => $http_response | ||
| 128 | }); | ||
| 129 | |||
| 130 | $cb->($response); | ||
| 131 | }; | ||
| 132 | |||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | with 'Data::Riak::Transport::HTTP'; | ||
| 137 | |||
| 138 | __PACKAGE__->meta->make_immutable; | ||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ |