I have a program that uses AnyEvent and I want to call Finance::MtGox, but do
so without blocking the event loop. I tried both LWP::Protocol::AnyEvent::http
and calling an internal method in Finance::MtGox to just construct the
HTTP::Request and use AnyEvent::HTTP to make the request, but both of these
result in a 400 Bad Request. I'm including the code so hopefully somebody can
figure out where I'm going wrong. Thanks!
Need to subclass Finance::MtGox so the POST goes to a test server.
use Data::Dump;
BEGIN {
package Local::Finance::MtGox;
use base 'Finance::MtGox';
sub _build_api_method_uri {
return URI->new("https://h.wrttn.me/post");
}
}
This works as expected, but it's blocking.
my $mtgox = Local::Finance::MtGox->new({ key => 'key', secret => 'secr
+et' });
$mtgox->{mech}->ssl_opts(verify_hostname => 0);
dd $mtgox->call_auth('getFunds');
This works and is non-blocking, but it is not very efficient if it is called
many times or the parent process is large.
use AnyEvent;
use AnyEvent::Util 'fork_call';
my $mtgox = Local::Finance::MtGox->new({ key => 'key', secret => 'secr
+et' });
$mtgox->{mech}->ssl_opts(verify_hostname => 0);
my $cv = AE::cv;
fork_call { $mtgox->call_auth('getFunds') } sub { dd @_; $cv->send };
$cv->wait;
This doesn't work, it returns a 400 Bad Request.
use Coro;
use LWP::Protocol::AnyEvent::http;
my $mtgox = Local::Finance::MtGox->new({ key => 'key', secret => 'secr
+et' });
my $coro = async {
say "before";
$mtgox->call_auth('getFunds');
};
dd $coro->join;
This also doesn't work and returns a 400 Bad Request.
use AnyEvent;
use AnyEvent::HTTP::Request;
use AnyEvent::HTTP::Response;
my $mtgox = Local::Finance::MtGox->new({ key => 'key', secret => 'secr
+et' });
my $cv = AE::cv;
my $req = AnyEvent::HTTP::Request->new(
# Returns an HTTP::Request
$mtgox->_build_api_method_request(POST => 'getFunds'),
{
cb => sub {
my $res = AnyEvent::HTTP::Response->new(@_)->to_http_messa
+ge;
dd $res;
$cv->send;
}
}
);
$req->send;
$cv->wait;