File Coverage

blib/lib/Search/Typesense/Role/Request.pm
Criterion Covered Total %
statement 42 42 100.0
branch 11 12 91.6
condition 5 6 83.3
subroutine 10 10 100.0
pod n/a
total 68 70 97.1


line stmt bran cond sub pod time code
1             package Search::Typesense::Role::Request;
2              
3             # ABSTRACT: No user-serviceable parts inside.
4              
5 4     4   18882 use v5.16.0;
  4         18  
6 4     4   24 use Carp 'croak';
  4         11  
  4         228  
7 4     4   28 use Moo::Role;
  4         9  
  4         26  
8 4         26 use Search::Typesense::Types qw(
9             Enum
10             InstanceOf
11             compile
12 4     4   2486 );
  4         11  
13              
14             our $VERSION = '0.08';
15              
16             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             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             sub _url {
33 31     31   138644 my ( $self, $path ) = @_;
34 31         743 return $self->_url_base->clone->path( '/' . join( '/' => @$path ) );
35             }
36              
37             sub _GET {
38 17     17   76 my ( $self, %arg_for ) = @_;
39 17         74 return $self->_handle_request( \%arg_for );
40             }
41              
42             sub _DELETE {
43 3     3   13 my ( $self, %arg_for ) = @_;
44 3         11 return $self->_handle_request( \%arg_for );
45             }
46              
47             sub _POST {
48 7     7   35 my ( $self, %arg_for ) = @_;
49 7         18 my $body = $arg_for{body};
50 7 100       29 my @args = ref $body ? ( json => $body ) : $body;
51 7         25 return $self->_handle_request( \%arg_for, \@args );
52             }
53              
54             sub _PATCH {
55 1     1   6 my ( $self, %arg_for ) = @_;
56 1         4 my $body = $arg_for{body};
57 1 50       6 my @args = ref $body ? ( json => $body ) : $body;
58 1         5 return $self->_handle_request( \%arg_for, \@args );
59             }
60              
61             sub _handle_request {
62 28     28   73 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         98 my ( undef, undef, undef, $method ) = caller(1);
70 28         887 $method =~ s/^.*::_//;
71 28         273 state $check = compile( Enum [qw/get delete post patch/] );
72 28         68560 ($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 100       361 my @args = $args ? @$args : ();
78             my $url
79 28   100     117 = $self->_url( $arg_for->{path} )->query( $arg_for->{query} || {} );
80 28         4240 my $tx = $self->_ua->$method( $url, @args );
81 28         247759 my $res = $tx->res;
82              
83             # If the response is not succesful, return nothing if it's a 404.
84             # Otherwise, croak()
85 28 100       191 unless ( $res->is_success ) {
86 4 100 100     65 return if ( $res->code // 0 ) == 404;
87 3   50     39 my $message = $res->message // '';
88              
89 3         46 my $body = $res->body;
90 3         245 my $method = $tx->req->method;
91 3         29 my $url = $tx->req->url;
92 3         36 croak("'$method $url' failed: $message. $body");
93             }
94              
95 24 100       614 return $arg_for->{return_transaction} ? $tx : $tx->res->json;
96             }
97              
98             1;
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Search::Typesense::Role::Request - No user-serviceable parts inside.
109              
110             =head1 VERSION
111              
112             version 0.08
113              
114             =head1 AUTHOR
115              
116             Curtis "Ovid" Poe <ovid@allaroundtheworld.fr>
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2021 by Curtis "Ovid" Poe.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut