| 1 | package UserAgent::Any::Impl::HttpPromise; | ||
| 2 | |||
| 3 | use 5.036; | ||
| 4 | |||
| 5 | use HTTP::Promise; | ||
| 6 | use Moo; | ||
| 7 | use Promise::Me; | ||
| 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 $p = $self->{ua}->$method( | ||
| 19 | $url, | ||
| 20 | %{params_to_hash(@{$params})}, | ||
| 21 | (defined ${$content} ? (Content => ${$content}) : ()) | ||
| 22 | )->then(sub { return $_[0] }); | ||
| 23 | my @r = await($p); | ||
| 24 | return new_response(@r); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub call_cb { | ||
| 28 | my ($self, $method, $url, $params, $content) = &get_call_args; | ||
| 29 | return sub ($cb) { | ||
| 30 | $self->{ua}->$method( | ||
| 31 | $url, | ||
| 32 | %{params_to_hash(@{$params})}, | ||
| 33 | (defined ${$content} ? (Content => ${$content}) : ()) | ||
| 34 | )->then(sub { $cb->(new_response($_[0])) }); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | sub call_p { | ||
| 40 | my ($self, $method, $url, $params, $content) = &get_call_args; | ||
| 41 | return $self->{ua}->$method( | ||
| 42 | $url, | ||
| 43 | %{params_to_hash(@{$params})}, | ||
| 44 | (defined ${$content} ? (Content => ${$content}) : ()) | ||
| 45 | )->then(sub { new_response($_[0]) }); | ||
| 46 | } | ||
| 47 | |||
| 48 | BEGIN { generate_methods() } | ||
| 49 | |||
| 50 | 1; | ||
| 51 | |||
| 52 | __END__ |