| 1 | package Net::Async::Authentik::Role::HTTP; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: Sending requests to authentik through Net::Async::HTTP | ||||||||||
| 4 | |||||||||||
| 5 | 8 | ▶ | use Future; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 8 | ▶ | use Net::Async::Authentik::Error::API; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 8 | ▶ | use Net::Async::Authentik::Error::Network; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 8 | ▶ | use Net::Async::Authentik::Error::Validation; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 8 | ▶ | use Scalar::Util qw( blessed ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 8 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 8 | our $VERSION = '0.001'; | |||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | 48 | ▶ | sub api_error_class { 'Net::Async::Authentik::Error::API' } | ||||||||
|
Subroutine
api_error_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 16 | 4 | ▶ | sub network_error_class { 'Net::Async::Authentik::Error::Network' } | ||||||||
|
Subroutine
network_error_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 17 | 116 | ▶ | sub validation_error_class { 'Net::Async::Authentik::Error::Validation' } | ||||||||
|
Subroutine
validation_error_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | 49 | ▶ | sub fail_validation { | ||||||||
|
Subroutine
fail_validation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 49 | my ( $self, $message ) = @_; | |||||||||
| 22 | 49 | return Future->fail( $self->validation_error_class->new( message => $message ) ); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | 140 | ▶ | sub pairs_or_fail { | ||||||||
|
Subroutine
pairs_or_fail: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 27 | 140 | my ( $self, $positional, @args ) = @_; | |||||||||
| 28 | # An odd-sized list where a key belongs is a mistake in the caller, and on | ||||||||||
| 29 | # Future::AsyncAwait 0.71 a very expensive one: a suspended async sub whose | ||||||||||
| 30 | # pad holds a hash built from such a list corrupts the heap when the future | ||||||||||
| 31 | # is dropped - "double free or corruption", no exception, no stack. Nine | ||||||||||
| 32 | # lines reproduce it without this distribution; see the plan under "Nach | ||||||||||
| 33 | # dem Bau geklaert". So the shape is checked before the hash is built. | ||||||||||
| 34 | 140 | ▶ | return ( 1, { @args[ $positional .. $#args ] } ) if ( @args - $positional ) % 2 == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 9 | return ( 0, undef ); | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 313 | ▶ | sub send_request_f { | ||||||||
|
Subroutine
send_request_f: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 40 | 313 | my ( $self, $method, $url, %arg ) = @_; | |||||||||
| 41 | # building it can die too: a body the JSON codec cannot encode. Nothing in | ||||||||||
| 42 | # this distribution throws, so that becomes a failed future as well. | ||||||||||
| 43 | 313 | my $request = eval { $self->build_request( $method, $url, %arg ) }; | |||||||||
| 44 | 313 | ▶ | return Future->fail( $self->validation_error_class->new( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | message => $method.' '.$url.': the request could not be built ('.( $@ =~ s/ at \S+ line \d+.*//sr ).')' ) ) | ||||||||||
| 46 | unless $request; | ||||||||||
| 47 | # Net::Async::HTTP dies instead of failing when it is in no loop | ||||||||||
| 48 | 311 | my $sent = eval { $self->http->do_request( request => $request ) }; | |||||||||
| 49 | 311 | ▶ | return Future->fail( $self->network_error_class->new( message => $method.' '.$url.': could not send (' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | .( $@ =~ s/ at \S+ line \d+.*//sr ).'); was the Net::Async::Authentik added to a loop?' ) ) unless $sent; | ||||||||||
| 51 | 1 | ▶ | return $sent->else( sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 52 | 1 | my ( $message ) = @_; | |||||||||
| 53 | 1 | ▶ | return Future->fail($message) if blessed $message; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | # a refused connection arrives as one string, a timeout as ('Timed out', | ||||||||||
| 55 | # 'timeout'); neither is an object the way LWP's internal response was | ||||||||||
| 56 | 1 | return Future->fail( $self->network_error_class->new( message => $method.' '.$url.': '.$message ) ); | |||||||||
| 57 | 307 | ▶ | } )->then( sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 58 | 307 | my ( $response ) = @_; | |||||||||
| 59 | 307 | my $result = eval { $self->read_response( $response, $method, $url, %arg ) }; | |||||||||
| 60 | 307 | ▶ | return $result ? Future->done($result) : Future->fail($@); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 308 | } ); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | |||||||||||
| 65 | 8 | 1; | |||||||||
| 66 | |||||||||||
| 67 | __END__ | ||||||||||