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;

In reply to Calling LWP/Mech from AnyEvent by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.