Mattermost/V4/API/Resource.pm

filter: Statementstmt 84.1% 122 / 145 Branchbran 75.0% 18 / 24 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 86.2% 25 / 29 Podpod n/a totaltotal 82.9% 180 / 217 CC 19 SCAR 13.0
CC
19
Cov
83%
_as_response
8.0
_call
7.2
_validate
5.2
CRAP 3.7
« Mattermost/V4/API/Object/User.pm ↑ Summary Mattermost/V4/API/Object/Plugins.pm »
1 package WebService::Mattermost::V4::API::Resource;
2  
3 # ABSTRACT: Base class for API resources.
4  
5 12 ▶use List::MoreUtils 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Types::Standard qw(Bool HashRef Maybe Object Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use WebService::Mattermost::Helper::Alias 'view';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use WebService::Mattermost::V4::API::Object::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use WebService::Mattermost::V4::API::Object::Icon;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use WebService::Mattermost::V4::API::Object::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use WebService::Mattermost::V4::API::Object::Team;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use WebService::Mattermost::V4::API::Object::TeamMember;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use WebService::Mattermost::V4::API::Object::TeamStats;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use WebService::Mattermost::V4::API::Object::Plugins;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use WebService::Mattermost::V4::API::Object::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use WebService::Mattermost::V4::API::Object::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use WebService::Mattermost::V4::API::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use WebService::Mattermost::V4::API::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 12 with qw(
23 WebService::Mattermost::Role::Logger
24 WebService::Mattermost::Role::Returns
25 WebService::Mattermost::Role::UserAgent
26 WebService::Mattermost::V4::API::Role::RequireID
27 WebService::Mattermost::V4::API::Role::NewRelatedResource
28 );
29  
30 ################################################################################
31  
32 12 has api => (is => 'ro', isa => Object, required => 1);
33 12 has base_url => (is => 'ro', isa => Str, required => 1);
34 12 has resource => (is => 'ro', isa => Str, required => 1);
35 12 has auth_token => (is => 'rw', isa => Str, required => 1);
36  
37 12 has DELETE => (is => 'ro', isa => Str, default => 'DELETE');
38 12 has GET => (is => 'ro', isa => Str, default => 'GET');
39 1682 ▶has headers => (is => 'ro', isa => HashRef, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
40 12 has POST => (is => 'ro', isa => Str, default => 'POST');
41 12 has PUT => (is => 'ro', isa => Str, default => 'PUT');
42 12 has debug => (is => 'ro', isa => Bool, default => 0);
43  
44 12 has id => (is => 'rw', isa => Maybe[Str]);
45  
46 ################################################################################
47  
48 sub _delete {
49 0 ▶ my $self = shift;
Subroutine _delete: not called CC=1 SCAR=6.9
50 0 my $args = shift;
51  
52 0 $args->{method} = $self->DELETE;
53  
54 0 return $self->_call($args);
55 }
56  
57 sub _single_view_delete {
58 0 ▶ my $self = shift;
Subroutine _single_view_delete: not called CC=1 SCAR=6.9
59 0 my $args = shift;
60  
61 0 $args->{single} = 1;
62  
63 0 return $self->_delete($args);
64 }
65  
66 sub _get {
67 12 ▶ my $self = shift;
Subroutine _get: called CC=1 SCAR=0.0
68 12 my $args = shift;
69  
70 12 $args->{method} = $self->GET;
71  
72 12 return $self->_call($args);
73 }
74  
75 sub _single_view_get {
76 3 ▶ my $self = shift;
Subroutine _single_view_get: called CC=1 SCAR=0.0
77 3 my $args = shift;
78  
79 3 $args->{single} = 1;
80  
81 3 return $self->_get($args);
82 }
83  
84 sub _post {
85 3 ▶ my $self = shift;
Subroutine _post: called CC=1 SCAR=0.0
86 3 my $args = shift;
87  
88 3 $args->{method} = $self->POST;
89  
90 3 return $self->_call($args);
91 }
92  
93 sub _single_view_post {
94 0 ▶ my $self = shift;
Subroutine _single_view_post: not called CC=1 SCAR=6.9
95 0 my $args = shift;
96  
97 0 $args->{single} = 1;
98  
99 0 return $self->_post($args);
100 }
101  
102 sub _put {
103 1 ▶ my $self = shift;
Subroutine _put: called CC=1 SCAR=0.0
104 1 my $args = shift;
105  
106 1 $args->{method} = $self->PUT;
107  
108 1 return $self->_call($args);
109 }
110  
111 sub _single_view_put {
112 0 ▶ my $self = shift;
Subroutine _single_view_put: not called CC=1 SCAR=6.9
113 0 my $args = shift;
114  
115 0 $args->{method} = $self->PUT;
116  
117 0 return $self->_put($args);
118 }
119  
120 sub _call {
121 16 ▶ my $self = shift;
Subroutine _call: called CC=7 SCAR=19.7
122 16 my $args = shift;
123  
124 16 ▶ if ($args->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"required"}) 3 13 16
125 3 my $validation = $self->_validate($args->{parameters}, $args->{required});
126  
127 3 ▶ return $validation unless $validation->{valid};
Branch: 1 branch
BranchTrueFalseTotal
unless $validation->{"valid"} 0 3 3
128 }
129  
130 16 my %headers = ('Keep-Alive' => 1);
131  
132 16 ▶ if ($self->auth_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auth_token) 16 0 16
133 16 $headers{Authorization} = $self->bearer($self->auth_token);
134 }
135  
136 16 my $request = $self->_as_request($args);
137 16 my $method = lc $request->method;
138  
139 16 my $form_type;
140  
141 16 ▶ if (grep { $_ eq $request->method } ($self->PUT, $self->POST)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $request->method;} $self->PUT, $self->POST) { } 4 12 16
142 4 $form_type = 'json';
143 } else {
144 12 $form_type = 'form';
145 }
146  
147 16 ▶ $form_type = $args->{override_data_type} if $args->{override_data_type};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"override_data_type"} 1 15 16
148  
149 16 my $tx = $self->ua->$method(
150 $request->url => \%headers,
151 $form_type => $request->parameters,
152 );
153  
154 16 ▶ if (my $error = $tx->req->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $tx->req->error) 0 16 16
155 0 $self->logger->warn('No HTTP code was received from Mattermost. Is your server alive?');
156 0 $self->logger->warnf('The following may be useful: %s', $error->{message});
157 }
158  
159 16 return $self->_as_response($tx->res, $args);
160 }
161  
162 sub _as_request {
163 16 ▶ my $self = shift;
Subroutine _as_request: called CC=3 SCAR=11.0
164 16 my $args = shift;
165  
166 16 $args->{auth_token} = $self->auth_token;
167 16 $args->{base_url} = $self->base_url;
168 16 $args->{resource} = $self->resource;
169 16 $args->{debug} = $self->debug;
170  
171 16 ▶ $args->{endpoint} ||= '';
Condition100%2/2
$args->{"endpoint"} ||= ""
l!l
97
MC/DC100%1/1
$args->{"endpoint"} ||= ""
$args->{"endpoint"}
Truth table100%2/2
A
A= $args->{"endpoint"}
Aresult
11
00
172 16 ▶ $args->{parameters} ||= {};
Condition100%2/2
$args->{"parameters"} ||= {}
l!l
124
MC/DC100%1/1
$args->{"parameters"} ||= {}
$args->{"parameters"}
Truth table100%2/2
A
A= $args->{"parameters"}
Aresult
11
00
173  
174 16 return WebService::Mattermost::V4::API::Request->new($args);
175 }
176  
177 sub _as_response {
178 16 ▶ my $self = shift;
Subroutine _as_response: called CC=8 SCAR=20.8
179 16 my $res = shift;
180 16 my $args = shift;
181  
182 16 ▶ my $view_name = $self->can('view_name') && $self->view_name;
Condition66%2/3
$self->can('view_name') && $self->view_name
!ll&&!rl&&r
5011
MC/DC50%1/2
$self->can('view_name') && $self->view_name
$self->can('view_name') $self->view_name
Truth table66%2/3
A && B
A= $self->can('view_name')B= $self->view_name
ABresult
0X0
100
111
183  
184 16 ▶ if ($args->{view}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"view"}) 4 12 16
185 4 $view_name = $args->{view};
186 }
187  
188 16 ▶ if ($res->is_error && $self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_error and $self->debug) 1 15 16
Condition100%3/3
$res->is_error and $self->debug
!ll&&!rl&&r
1411
MC/DC100%2/2
$res->is_error and $self->debug
$res->is_error $self->debug
Truth table100%3/3
A and B
A= $res->is_errorB= $self->debug
ABresult
0X0
100
111
189 1 $self->logger->warnf('An API error occurred: %s', $res->message);
190 }
191  
192 return WebService::Mattermost::V4::API::Response->new({
193 auth_token => $self->auth_token,
194 base_url => $self->base_url,
195 code => $res->code || 0,
196 headers => $res->headers,
197 is_error => $res->is_error ? 1 : 0,
198 is_success => $res->is_success ? 1 : 0,
199 message => $res->message,
200 prev => $res,
201 raw_content => $res->body,
202 item_view => $view_name,
203 single_item => $args->{single},
204 16 ▶ });
Branch: 2 branches
BranchTrueFalseTotal
$res->is_error ? : 2 14 16
$res->is_success ? : 14 2 16
Condition50%1/2
$res->code || 0
l!l
160
MC/DC0%0/1
$res->code || 0
$res->code
Truth table50%1/2
A
A= $res->code
Aresult
11
00
205 }
206  
207 sub _validate {
208 3 ▶ my $self = shift;
Subroutine _validate: called CC=4 SCAR=16.5
209 3 my $args = shift;
210 3 my $required = shift;
211  
212 3 my %slice;
213  
214 # Grab a slice of the keys from given arguments
215 3 @slice{@{$required}} = @{$args}{@{$required}};
216  
217 # Return early, all's well
218 3 ▶ return { valid => 1 } if all { defined($_) } values %slice;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { defined $_; } , values %slice) 3 0 3
219  
220 0 my @missing;
221  
222 0 foreach my $kx (@{$required}) {
223 0 ▶ push @missing, $kx unless $args->{$kx};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$kx} 0 0 0
224 }
225  
226 return {
227 0 valid => 0,
228 missing => \@missing,
229 error => sprintf('Required parameters missing: %s', join(', ', @missing)),
230 };
231 }
232  
233 ################################################################################
234  
235 12 1;
236  
237 __END__
238  
239 =pod
240  
241 =encoding UTF-8
242  
243 =head1 NAME
244  
245 WebService::Mattermost::V4::API::Resource - Base class for API resources.
246  
247 =head1 VERSION
248  
249 version 0.28
250  
251 =head1 DESCRIPTION
252  
253 =head2 ATTRIBUTES
254  
255 =over 4
256  
257 =item C<auth_token>
258  
259 An auth token to use in the headers for every API call. Authentication is
260 required to use the Mattermost API.
261  
262 =item C<base_url>
263  
264 The API's base URL.
265  
266 =item C<resource>
267  
268 The name of the API resource, for example L<WebService::Mattermost::V4::API::Brand>'s
269 resource is 'brand'.
270  
271 =back
272  
273 =head1 AUTHOR
274  
275 Mike Jones <mike@netsplit.org.uk>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 This software is Copyright (c) 2020 by Mike Jones.
280  
281 This is free software, licensed under:
282  
283 The MIT (X11) License
284  
285 =cut
« Mattermost/V4/API/Object/User.pm ↑ Summary Mattermost/V4/API/Object/Plugins.pm »