Last active
August 29, 2015 13:57
-
-
Save ishiduca/9484996 to your computer and use it in GitHub Desktop.
HTTPClient based Tatusmaki::HTTPClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Moco::Moco::HTTPClient; | |
use Moo; | |
use MooX::late; | |
use Moco::Moco; | |
use AnyEvent::HTTP; | |
extends qw(Tatsumaki::HTTPClient); | |
has agent => ( is => 'rw', isa => 'Str', default => sub { join '/', __PACKAGE__ , $Moco::Moco::VERSION }); | |
has jar => ( is => 'rw', isa => 'HashRef', default => sub { +{version => 1} }); | |
has recurse => ( is => 'rw', isa => 'Num', default => $AnyEvent::HTTP::MAX_RECURSE); | |
sub request { | |
my($self, $request, $cb) = @_; | |
my $headers = $request->headers; | |
$headers->{'user-agent'} = $self->agent; | |
delete $headers->{'content-length'}; # AnyEven::HTTP::request が面倒うみてくれるので | |
my $jar = $self->jar; | |
my %options = ( | |
timeout => $self->timeout, | |
headers => $headers, | |
cookie_jar => $jar, | |
recurse => $self->recuse, | |
body => $request->content, | |
); | |
AnyEvent::HTTP::http_request $request->method, $request->uri, %options, sub { | |
my($body, $header) = @_; | |
my $res = HTTP::Response->new($header->{Status}, $header->{Reason}, [ %$header ], $body); | |
$self->jar( $jar ); | |
$cb->($res); | |
}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
何かしらのデータをポストした際に、サーバーからリダイレクトを支持された場合、recurse をデフォルトにしておくと最初のヘッダ情報もリダイレクトURLに送っちゃってるっぽいので、recurse => 0 にして手動でリダイレクト先にリクエストを送らないといけないっぽい