| 1 | package Search::Typesense::Role::Request; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: No user-serviceable parts inside. | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | ▶ | use v5.16.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use Carp 'croak'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 4 | ▶ | use Search::Typesense::Types qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | Enum | ||||||||||||||||||||
| 10 | InstanceOf | ||||||||||||||||||||
| 11 | compile | ||||||||||||||||||||
| 12 | 4 | ); | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 4 | our $VERSION = '0.08'; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 4 | has _ua => ( | |||||||||||||||||||
| 17 | is => 'lazy', | ||||||||||||||||||||
| 18 | isa => InstanceOf ['Mojo::UserAgent'], | ||||||||||||||||||||
| 19 | weak_ref => 1, | ||||||||||||||||||||
| 20 | init_arg => 'user_agent', | ||||||||||||||||||||
| 21 | required => 1, | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 4 | has _url_base => ( | |||||||||||||||||||
| 25 | is => 'lazy', | ||||||||||||||||||||
| 26 | isa => InstanceOf ['Mojo::URL'], | ||||||||||||||||||||
| 27 | weak_ref => 1, | ||||||||||||||||||||
| 28 | init_arg => 'url', | ||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 31 | ▶ | sub _url { | ||||||||||||||||||
|
Subroutine
_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 31 | my ( $self, $path ) = @_; | |||||||||||||||||||
| 34 | 31 | return $self->_url_base->clone->path( '/' . join( '/' => @$path ) ); | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 17 | ▶ | sub _GET { | ||||||||||||||||||
|
Subroutine
_GET: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 17 | my ( $self, %arg_for ) = @_; | |||||||||||||||||||
| 39 | 17 | return $self->_handle_request( \%arg_for ); | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | ▶ | sub _DELETE { | ||||||||||||||||||
|
Subroutine
_DELETE: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | 3 | my ( $self, %arg_for ) = @_; | |||||||||||||||||||
| 44 | 3 | return $self->_handle_request( \%arg_for ); | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 7 | ▶ | sub _POST { | ||||||||||||||||||
|
Subroutine
_POST: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 48 | 7 | my ( $self, %arg_for ) = @_; | |||||||||||||||||||
| 49 | 7 | my $body = $arg_for{body}; | |||||||||||||||||||
| 50 | 7 | ▶ | my @args = ref $body ? ( json => $body ) : $body; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 7 | return $self->_handle_request( \%arg_for, \@args ); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 1 | ▶ | sub _PATCH { | ||||||||||||||||||
|
Subroutine
_PATCH: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 55 | 1 | my ( $self, %arg_for ) = @_; | |||||||||||||||||||
| 56 | 1 | my $body = $arg_for{body}; | |||||||||||||||||||
| 57 | 1 | ▶ | my @args = ref $body ? ( json => $body ) : $body; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 1 | return $self->_handle_request( \%arg_for, \@args ); | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 28 | ▶ | sub _handle_request { | ||||||||||||||||||
|
Subroutine
_handle_request: called CC=8 SCAR=20.8
| |||||||||||||||||||||
| 62 | 28 | my ( $self, $arg_for, $args ) = @_; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # We must only be called by methods like _GET, _POST, _DELETE, and so on. | ||||||||||||||||||||
| 65 | # We strip the package name and leading underscore | ||||||||||||||||||||
| 66 | # (Search::Typesense::_GET becomes GET) and then we call lc() on what's | ||||||||||||||||||||
| 67 | # left. That becomes our HTTP verb and the $check verifies that this is an | ||||||||||||||||||||
| 68 | # allowed verb. | ||||||||||||||||||||
| 69 | 28 | my ( undef, undef, undef, $method ) = caller(1); | |||||||||||||||||||
| 70 | 28 | $method =~ s/^.*::_//; | |||||||||||||||||||
| 71 | 28 | state $check = compile( Enum [qw/get delete post patch/] ); | |||||||||||||||||||
| 72 | 28 | ($method) = $check->( lc $method ); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | # make the actual request, passing a query string, if any, and passing any | ||||||||||||||||||||
| 75 | # args, if any (those can become part of a query string for GET, or part | ||||||||||||||||||||
| 76 | # of the body for other HTTP verbs | ||||||||||||||||||||
| 77 | 28 | ▶ | my @args = $args ? @$args : (); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | my $url | ||||||||||||||||||||
| 79 | 28 | ▶ | = $self->_url( $arg_for->{path} )->query( $arg_for->{query} || {} ); | ||||||||||||||||||
|
Condition100%2/2
$arg_for->{'query'} || {}
MC/DC100%1/1
$arg_for->{'query'} || {}
$arg_for->{'query'}
Truth table100%2/2
A
A= $arg_for->{'query'}
| |||||||||||||||||||||
| 80 | 28 | my $tx = $self->_ua->$method( $url, @args ); | |||||||||||||||||||
| 81 | 28 | my $res = $tx->res; | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | # If the response is not succesful, return nothing if it's a 404. | ||||||||||||||||||||
| 84 | # Otherwise, croak() | ||||||||||||||||||||
| 85 | 28 | ▶ | unless ( $res->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 4 | ▶ | return if ( $res->code // 0 ) == 404; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$res->code // 0
MC/DC100%1/1
$res->code // 0
$res->code
Truth table100%2/2
A
A= $res->code
| |||||||||||||||||||||
| 87 | 3 | ▶ | my $message = $res->message // ''; | ||||||||||||||||||
|
Condition50%1/2
$res->message // ""
MC/DC0%0/1
$res->message // ""
$res->message
Truth table50%1/2
A
A= $res->message
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 3 | my $body = $res->body; | |||||||||||||||||||
| 90 | 3 | my $method = $tx->req->method; | |||||||||||||||||||
| 91 | 3 | my $url = $tx->req->url; | |||||||||||||||||||
| 92 | 3 | croak("'$method $url' failed: $message. $body"); | |||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 24 | ▶ | return $arg_for->{return_transaction} ? $tx : $tx->res->json; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 4 | 1; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||