| 1 | package UserAgent::Any::Impl::AnyEventUserAgent; | ||
| 2 | |||
| 3 | use 5.036; | ||
| 4 | |||
| 5 | use AnyEvent; | ||
| 6 | use Promise::XS; | ||
| 7 | use Moo; | ||
| 8 | use UserAgent::Any::Impl::Helper ':all'; | ||
| 9 | |||
| 10 | use namespace::clean; | ||
| 11 | |||
| 12 | with 'UserAgent::Any::Impl'; | ||
| 13 | |||
| 14 | our $VERSION = 0.01; | ||
| 15 | |||
| 16 | sub call { | ||
| 17 | my ($self, $method, $url, $params, $content) = &get_call_args; | ||
| 18 | my $cv = AnyEvent->condvar; | ||
| 19 | my $r; | ||
| 20 | $self->{ua}->$method( | ||
| 21 | $url, | ||
| 22 | %{params_to_hash(@{$params})}, | ||
| 23 | (defined ${$content} ? (Content => ${$content}) : ()), | ||
| 24 | sub ($res) { $r = $res; $cv->send }); | ||
| 25 | $cv->recv; | ||
| 26 | return new_response($r); | ||
| 27 | } | ||
| 28 | |||
| 29 | sub call_cb { | ||
| 30 | my ($self, $method, $url, $params, $content) = &get_call_args; | ||
| 31 | return sub ($cb) { | ||
| 32 | $self->{ua}->$method( | ||
| 33 | $url, | ||
| 34 | %{params_to_hash(@{$params})}, | ||
| 35 | (defined ${$content} ? (Content => ${$content}) : ()), | ||
| 36 | sub ($res) { $cb->(new_response($res)) }); | ||
| 37 | return; | ||
| 38 | }; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub call_p { | ||
| 42 | my ($self, $method, $url, $params, $content) = &get_call_args; | ||
| 43 | my $p = Promise::XS::deferred(); | ||
| 44 | $self->{ua}->$method( | ||
| 45 | $url, | ||
| 46 | %{params_to_hash(@{$params})}, | ||
| 47 | (defined ${$content} ? (Content => ${$content}) : ()), | ||
| 48 | sub ($res) { $p->resolve(new_response($res)) }); | ||
| 49 | return $p->promise(); | ||
| 50 | } | ||
| 51 | |||
| 52 | BEGIN { generate_methods() } | ||
| 53 | |||
| 54 | 1; | ||
| 55 | |||
| 56 | __END__ |