| 1 | package CORION::Apache::Tika::Connection::AEHTTP; | ||
| 2 | use AnyEvent::HTTP qw(http_request); | ||
| 3 | use Promises qw(deferred); | ||
| 4 | use Try::Tiny; | ||
| 5 | use Moo; | ||
| 6 | with 'CORION::Apache::Tika::Connection'; | ||
| 7 | |||
| 8 | use vars '$VERSION'; | ||
| 9 | $VERSION = '0.06'; | ||
| 10 | |||
| 11 | sub request { | ||
| 12 | my( $self, $method, $url, $content, @headers ) = @_; | ||
| 13 | # Should initialize | ||
| 14 | |||
| 15 | $method = uc $method; | ||
| 16 | |||
| 17 | my $content_size = length $content; | ||
| 18 | |||
| 19 | # 'text/plain' for the language | ||
| 20 | my %headers= ( | ||
| 21 | "Content-Length" => $content_size, | ||
| 22 | "Accept" => 'application/json,text/plain', | ||
| 23 | @headers | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $p = deferred; | ||
| 27 | http_request( | ||
| 28 | $method => $url, | ||
| 29 | headers => \%headers, | ||
| 30 | body => $content, | ||
| 31 | sub { | ||
| 32 | my ( $body, $headers ) = @_; | ||
| 33 | # The headers might be invalid! | ||
| 34 | try { | ||
| 35 | my ( $code, $response ) = $self->process_response( | ||
| 36 | undef, # request | ||
| 37 | delete $headers->{Status}, # code | ||
| 38 | delete $headers->{Reason}, # msg | ||
| 39 | $body, # body | ||
| 40 | $headers # headers | ||
| 41 | ); | ||
| 42 | |||
| 43 | $p->resolve( $code, $response ); | ||
| 44 | } | ||
| 45 | catch { | ||
| 46 | warn "Internal error: $_"; | ||
| 47 | $p->reject($_); | ||
| 48 | } | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | $p->promise | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | =head1 REPOSITORY | ||
| 57 | |||
| 58 | The public repository of this module is | ||
| 59 | L<https://github.com/Corion/apache-tika>. | ||
| 60 | |||
| 61 | =head1 SUPPORT | ||
| 62 | |||
| 63 | The public support forum of this module is | ||
| 64 | L<https://perlmonks.org/>. | ||
| 65 | |||
| 66 | =head1 BUG TRACKER | ||
| 67 | |||
| 68 | Please report bugs in this module via the RT CPAN bug queue at | ||
| 69 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=CORION-Apache-Tika> | ||
| 70 | or via mail to L<corion-apache-tika-Bugs@rt.cpan.org>. | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Max Maischein C<corion@cpan.org> | ||
| 75 | |||
| 76 | =head1 COPYRIGHT (c) | ||
| 77 | |||
| 78 | Copyright 2014-2016 by Max Maischein C<corion@cpan.org>. | ||
| 79 | |||
| 80 | =head1 LICENSE | ||
| 81 | |||
| 82 | This module is released under the same terms as Perl itself. | ||
| 83 | |||
| 84 | =cut |