File Coverage

blib/lib/WebService/Mattermost/V4/API/Request.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 5 5 100.0
pod n/a
total 37 39 94.8


line stmt bran cond sub pod time code
1             package WebService::Mattermost::V4::API::Request;
2              
3             # ABSTRACT: A request to be sent to the Mattermost API.
4              
5 7     7   2422 use Mojo::URL;
  7         577877  
  7         61  
6 7     7   286 use Mojo::Util 'url_escape';
  7         16  
  7         318  
7 7     7   42 use Moo;
  7         15  
  7         44  
8 7     7   2795 use Types::Standard qw(Any ArrayRef Bool Enum InstanceOf Str);
  7         16  
  7         58  
9              
10             with 'WebService::Mattermost::Role::Logger';
11              
12             ################################################################################
13              
14             has base_url => (is => 'ro', isa => Str, required => 1);
15             has endpoint => (is => 'ro', isa => Str, required => 1);
16             has method => (is => 'ro', isa => Enum [ qw(DELETE GET POST PUT) ], required => 1);
17             has resource => (is => 'ro', isa => Str, required => 1);
18              
19             # Some endpoints require parameters as a HashRef, some as an ArrayRef
20             has debug => (is => 'ro', isa => Bool, default => 0);
21             has ids => (is => 'ro', isa => ArrayRef, default => sub { [] });
22             has parameters => (is => 'ro', isa => Any, default => sub { {} });
23              
24             has url => (is => 'ro', isa => InstanceOf['Mojo::URL'], lazy => 1, builder => 1);
25              
26             ################################################################################
27              
28             sub _build_url {
29 10     10   466     my $self = shift;
30              
31 10         44     my $base_url = $self->base_url;
32 10         31     my $resource = $self->resource;
33 10         37     my $endpoint = $self->endpoint;
34              
35 10 50       57     $base_url .= '/' if $base_url !~ /\/$/;
36 10 100 66     70     $resource .= '/' if $self->endpoint ne '' && $resource !~ /\/$/;
37              
38 10         25     my @ids = map { url_escape($_) } @{$self->ids};
  4         20  
  10         46  
39              
40 10         78     $endpoint = sprintf($endpoint, @ids);
41              
42 10         43     my $url = sprintf('%s%s%s', $base_url, $resource, $endpoint);
43              
44 10 100       67     $self->logger->debug($url) if $self->debug;
45              
46 10         590     return Mojo::URL->new($url);
47             }
48              
49             ################################################################################
50              
51             1;
52              
53             __END__
54            
55             =pod
56            
57             =encoding UTF-8
58            
59             =head1 NAME
60            
61             WebService::Mattermost::V4::API::Request - A request to be sent to the Mattermost API.
62            
63             =head1 VERSION
64            
65             version 0.26
66            
67             =head1 DESCRIPTION
68            
69             A request to be sent to the Mattermost API.
70            
71             =head2 USAGE
72            
73             See L<WebService::Mattermost::V4::API::Resource::_call()>.
74            
75             =head2 ATTRIBUTES
76            
77             =over 4
78            
79             =item C<base_url>
80            
81             =item C<endpoint>
82            
83             =item C<method>
84            
85             HTTP method.
86            
87             =item C<resource>
88            
89             The API endpoint's namespace.
90            
91             =item C<parameters>
92            
93             Data to be sent to the API.
94            
95             =item C<ids>
96            
97             IDs to replace into the URL with C<sprintf>.
98            
99             =item C<url>
100            
101             =back
102            
103             =head1 AUTHOR
104            
105             Mike Jones <mike@netsplit.org.uk>
106            
107             =head1 COPYRIGHT AND LICENSE
108            
109             This software is Copyright (c) 2020 by Mike Jones.
110            
111             This is free software, licensed under:
112            
113             The MIT (X11) License
114            
115             =cut
116