File Coverage

blib/lib/PayProp/API/Public/Client/Role/Request.pm
Criterion Covered Total %
statement 37 37 100.0
branch 5 6 83.3
condition 14 14 100.0
subroutine 12 12 100.0
pod 4 4 100.0
total 72 73 98.6


line stmt bran cond sub pod time code
1             package PayProp::API::Public::Client::Role::Request;
2              
3 29     29   402543 use strict;
  29         81  
  29         1185  
4 29     29   163 use warnings;
  29         56  
  29         1463  
5              
6 29     29   1106 use Mouse::Role;
  29         2731  
  29         213  
7             with qw/ PayProp::API::Public::Client::Role::Attribute::UA /;
8              
9 29     29   27275 use Mojo::URL;
  29         770510  
  29         207  
10 29     29   17601 use PayProp::API::Public::Client::Exception::Connection;
  29         99  
  29         15105  
11              
12              
13             has url => (
14             is => 'rw',
15             isa => 'Str',
16             default => sub { die 'you must override default url value' }
17             );
18              
19             has ordered_path_params => (
20             is => 'rw',
21             isa => 'ArrayRef[Str]',
22             default => sub { [] },
23             );
24              
25              
26 5     5 1 79 sub put_req_p { shift->_handle_request( 'put_p', @_ ) }
27 19     19 1 350 sub get_req_p { shift->_handle_request( 'get_p', @_ ) }
28 17     17 1 182 sub post_req_p { shift->_handle_request( 'post_p', @_ ) }
29 3     3 1 34 sub delete_req_p { shift->_handle_request( 'delete_p', @_ ) }
30              
31             sub _handle_request {
32 45     45   1986 my ( $self, $http_verb, $args ) = @_;
33              
34 45   100     350 $args //= {};
35 45   100     460 my $params = $args->{params} // {};
36 45   100     233 my $content = $args->{content} // {};
37 45   100     224 my $headers = $args->{headers} // {};
38 45   100     250 my $path_params = $args->{path_params} // {};
39              
40             return
41             $self->ua->$http_verb(
42             $self->_build_url( $params, $path_params ),
43             $headers,
44             ( ref $content ? $content->%* : $content ),
45             )
46             ->catch( sub {
47 1     1   39244 my ( $error ) = @_;
48 1         26 PayProp::API::Public::Client::Exception::Connection->throw("$error");
49             } )
50 45 50       798 ;
51             }
52              
53             sub _build_url {
54 47     47   10233 my ( $self, $params, $path_params ) = @_;
55              
56 47   100     148 $params //= {};
57 47   100     488 $path_params //= {};
58              
59 47 100       1331 my $URL = Mojo::URL->new( $self->url . ( $path_params->%* ? '/' : '' ) ); # trailing slash preserves "route path"
60              
61 47 100       12636 $URL->path( join( '/', map { $path_params->{ $_ } } ( grep { exists $path_params->{ $_ } } $self->ordered_path_params->@* ) ) )
  41         285  
  41         152  
62             if $path_params->%*
63             ;
64              
65 47         6573 $self->ordered_path_params([]); # reset for next request
66              
67 47         235 return $URL
68             ->query( $params )
69             ->to_string
70             ;
71             }
72              
73             1;
74              
75             __END__