| 1 | package Mojo::Pua; | ||
| 2 | 3 | ▶ | use Mojo::Base 'Mojo::UserAgent'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use Evo 'Evo::Export; -Promise::Mojo *; Carp croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '0.010'; # VERSION | ||
| 6 | |||
| 7 | # LIB version | ||
| 8 | |||
| 9 | use constant PUA => __PACKAGE__->new(); | ||
| 10 | export 'PUA'; | ||
| 11 | |||
| 12 | # OO version | ||
| 13 | sub start ($self, $tx, $cb_empty = undef) { | ||
| 14 | |||
| 15 | croak "Got callback but this class returns a Promise" if $cb_empty; | ||
| 16 | my $d = deferred(); | ||
| 17 | |||
| 18 | my $pcb = sub ($ua, $tx) { | ||
| 19 | return $d->resolve($tx) if $tx->res->code; | ||
| 20 | $d->reject($tx->error->{message}); | ||
| 21 | }; | ||
| 22 | |||
| 23 | $self->SUPER::start($tx, $pcb); | ||
| 24 | |||
| 25 | $d->promise; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub want_code($want_code) : prototype($) : Export { | ||
| 29 | my (undef, $file, $line) = caller(); | ||
| 30 | return sub($tx) { | ||
| 31 | my $res = $tx->res; | ||
| 32 | return $res if $res->code == $want_code; | ||
| 33 | die | ||
| 34 | "Wanted [$want_code], got [${\$res->code}] ${\$res->message} at $file line $line\n"; | ||
| 35 | }; | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | # ABSTRACT: HTTP Client + Evo::Promise | ||
| 41 | |||
| 42 | __END__ | ||