| 1 | package CORION::Apache::Tika::Connection::LWP; | ||
| 2 | use LWP::UserAgent; | ||
| 3 | use LWP::ConnCache; | ||
| 4 | use Promises qw(deferred); | ||
| 5 | use Try::Tiny; | ||
| 6 | use Moo; | ||
| 7 | with 'CORION::Apache::Tika::Connection'; | ||
| 8 | |||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '0.06'; | ||
| 11 | |||
| 12 | has ua => ( | ||
| 13 | is => 'ro', | ||
| 14 | #isa => 'Str', | ||
| 15 | default => sub { my $ua= LWP::UserAgent->new(); $ua->conn_cache( LWP::ConnCache->new ); $ua }, | ||
| 16 | ); | ||
| 17 | |||
| 18 | sub request { | ||
| 19 | my( $self, $method, $url, $content, @headers ) = @_; | ||
| 20 | # Should initialize | ||
| 21 | |||
| 22 | my $content_size = length $content; | ||
| 23 | |||
| 24 | # 'text/plain' for the language | ||
| 25 | unshift @headers, "Content-Length" => $content_size; | ||
| 26 | my %headers= (($content | ||
| 27 | ? ('Content' => $content) | ||
| 28 | : ()), | ||
| 29 | @headers); | ||
| 30 | my $res = $self->ua->$method( $url, %headers); | ||
| 31 | |||
| 32 | my $p = deferred; | ||
| 33 | my ( $code, $response ) = $self->process_response( | ||
| 34 | $res->request, # request | ||
| 35 | $res->code, # code | ||
| 36 | $res->message, # msg | ||
| 37 | $res->decoded_content, # body | ||
| 38 | $res->headers # headers | ||
| 39 | ); | ||
| 40 | $p->resolve( $code, $response ); | ||
| 41 | |||
| 42 | $p->promise | ||
| 43 | } | ||
| 44 | |||
| 45 | 1; |