WebApi/Files.pm

filter: Statementstmt 72.2% 78 / 108 Branchbran 31.5% 12 / 38 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 7 totaltotal 55.7% 107 / 192 CC 25 SCAR 32.4
CC
25
Cov
58%
_check_response
90.0
send_file_to_external_url
11.9
upload_v2
9.6
CRAP 25.5
↑ Summary WebApi/Client.pm »
1 package WebService::Slack::WebApi::Files;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature qw/state/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use parent 'WebService::Slack::WebApi::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use WebService::Slack::WebApi::Generator (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 delete => {
12 file => 'Str',
13 },
14 info => {
15 file => 'Str',
16 count => { isa => 'Int', optional => 1 },
17 page => { isa => 'Int', optional => 1 },
18 },
19 list => {
20 channel => { isa => 'Str', optional => 1 },
21 count => { isa => 'Int', optional => 1 },
22 page => { isa => 'Int', optional => 1 },
23 ts_from => { isa => 'Str', optional => 1 },
24 ts_to => { isa => 'Str', optional => 1 },
25 types => { isa => 'Str', optional => 1 },
26 user => { isa => 'Str', optional => 1 },
27 },
28 3 );
29  
30 8 ▶sub revoke_public_url {
Subroutine revoke_public_url: called CC=1 SCAR=0.0
Pod undocumented
31 8 state $rule = Data::Validator->new(
32 file => 'Str',
33 )->with('Method', 'AllowExtra');
34 8 my ($self, $args, %extra) = $rule->validate(@_);
35  
36 8 return $self->request('revokePublicURL', {%$args, %extra});
37 }
38  
39 8 ▶sub shared_public_url {
Subroutine shared_public_url: called CC=1 SCAR=0.0
Pod undocumented
40 8 state $rule = Data::Validator->new(
41 file => 'Str',
42 )->with('Method', 'AllowExtra');
43 8 my ($self, $args, %extra) = $rule->validate(@_);
44  
45 8 return $self->request('sharedPublicURL', {%$args, %extra});
46 }
47  
48 16 ▶sub get_upload_url_external {
Subroutine get_upload_url_external: called CC=1 SCAR=0.0
Pod undocumented
49 16 state $rule = Data::Validator->new(
50 filename => 'Str',
51 length => 'Int',
52 )->with('Method', 'AllowExtra');
53 16 my ($self, $args, %extra) = $rule->validate(@_);
54  
55 16 return $self->request('getUploadURLExternal', {%$args, %extra});
56 }
57  
58 8 ▶sub send_file_to_external_url {
Subroutine send_file_to_external_url: called CC=7 SCAR=24.8
Pod undocumented
59 8 my ($self, $url, $params) = @_;
60  
61 8 my %headers;
62 8 ▶ if( $self->client->token && $params->{'http_auth'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->client->token and $params->{"http_auth"}) 0 8 8
Condition33%1/3
$self->client->token and $params->{"http_auth"}
!ll&&!rl&&r
080
MC/DC0%0/2
$self->client->token and $params->{"http_auth"}
$self->client->token $params->{"http_auth"}
Truth table33%1/3
A and B
A= $self->client->tokenB= $params->{"http_auth"}
ABresult
0X0
100
111
63 0 my $msg = 'Illegal parameters. You have defined \'token\' but the '
64 . ' method you are using defines its own HTTP Authorization header.';
65 0 WebService::Slack::WebApi::Exception::IllegalParameters->throw(
66 message => $msg,
67 );
68 }
69 8 ▶ if (exists $params->{file_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{'file_type'}) { } 8 0 8
70 8 $headers{'Content-type'} = $params->{file_type};
71 } else {
72 0 $headers{'Content-type'} = 'application/octet-stream';
73 }
74  
75 8 ▶ if( $self->client->token ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->client->token) { } 8 0 8
elsif ($params->{'http_auth'}) { } 0 0 0
76 8 $headers{'Authorization'} = 'Bearer ' . $self->client->token;
77 } elsif( $params->{'http_auth'} ) {
78 0 $headers{'Authorization'} = $params->{'http_auth'};
79 }
80
81 my %options = (
82 headers => \%headers,
83 content => $params->{file},
84 8 );
85
86 8 my $response = $self->client->ua->request(
87 'POST',
88 $url,
89 \%options
90 );
91 8 ▶ return if $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 8 0 8
92  
93 0 WebService::Slack::WebApi::Exception::FailureResponse->throw(
94 message => 'file upload failed.',
95 response => $response,
96 );
97 }
98  
99 17 ▶sub complete_upload_external {
Subroutine complete_upload_external: called CC=2 SCAR=7.4
Pod undocumented
100 17 state $rule = Data::Validator->new(
101 files => 'ArrayRef[HashRef]',
102 channels => { isa => 'ArrayRef[Str]', optional => 1 },
103 channel_id => { isa => 'Str', optional => 1 },
104 initial_comment => { isa => 'Str', optional => 1 },
105 thread_ts => { isa => 'Str', optional => 1 },
106 )->with('Method', 'AllowExtra');
107 17 my ($self, $args, %extra) = $rule->validate(@_);
108  
109 0 ▶ $args->{channels} = join ',', @{$args->{channels}} if exists $args->{channels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"channels"} 0 17 17
110  
111 17 return $self->request_json('completeUploadExternal', {%$args, %extra});
112 }
113  
114 # FIXME: maybe be broken... https://github.com/mihyaeru21/p5-WebService-Slack-WebApi/issues/15
115 # Will STOP Working by Slack on 2025-03-11
116 8 ▶sub upload {
Subroutine upload: called CC=3 SCAR=11.2
Pod undocumented
117 8 state $rule = Data::Validator->new(
118 channels => { isa => 'ArrayRef[Str]', optional => 1 },
119 content => { isa => 'Str', optional => 1 },
120 file => { isa => 'Str', optional => 1 },
121 filename => { isa => 'Str', optional => 1 },
122 filetype => { isa => 'Str', optional => 1 },
123 initial_comment => { isa => 'Str', optional => 1 },
124 title => { isa => 'Str', optional => 1 },
125 )->with('Method', 'AllowExtra');
126 8 my ($self, $args, %extra) = $rule->validate(@_);
127  
128 8 ▶ $args->{file} = [$args->{file}] if exists $args->{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"file"} 8 0 8
129 8 ▶ $args->{channels} = join ',', @{$args->{channels}} if exists $args->{channels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"channels"} 8 0 8
130  
131 8 return $self->request('upload', {%$args, %extra});
132 }
133  
134 =item $ _check_response(response =>, to =>, from =>, text =>, channel_not_found =>)
135  
136 Internal method.
137  
138 Checks the response from slack for the ok value and looks for error and warning values.
139  
140 It will die if it's an error or ok not found.
141  
142 The to, from and optional text values are included in the output to help troubleshoot.
143  
144 If the error is 'channel_not_found' we call the 'channel_not_found' provided subroutine so the
145 caller can handle sending to their custom error slack channel, etc.
146
147 Returns the following status code:
148 0 - okay
149 1 - warning found
150 2 - channel_not_found encountered and message was sent to the error channel successfully
151  
152 =cut
153 sub _check_response
154 0 ▶{
Subroutine _check_response: not called CC=9 SCAR=45.0
155 0 my $self = shift;
156 0 my %args = ( @_ );
157 0 my $response = $args{response};
158 0 my $to = $args{to};
159 0 my $from = $args{from};
160 0 my $text = $args{text}; # can be undef.
161 0 my $channel_not_found = $args{channel_not_found};
162
163 0 ▶ die "ERROR: channel_not_found must be a CODE ref!" if (!defined $channel_not_found || (defined $channel_not_found && ref($channel_not_found) ne "CODE"));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $channel_not_found or defined $channel_not_found and ref $channel_not_found ne "CODE" 0 0 0
Condition0%0/6
not defined $channel_not_found or defined $channel_not_found and ref $channel_not_found ne "CODE"
l!l&&r!l&&!r
000
defined $channel_not_found and ref $channel_not_found ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $channel_not_found or defined $channel_not_found and ref $channel_not_found ne "CODE"
not defined $channel_not_found defined $channel_not_found ref $channel_not_found ne "CODE"
Truth table0%0/4
not(A) or B and C
A= not defined $channel_not_foundB= defined $channel_not_foundC= ref $channel_not_found ne "CODE"
ABCresult
1XX1
0111
00X0
0100
164
165 0 ▶ if (! exists $response->{ok})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $response->{'ok'}) { } 0 0 0
166 {
167 0 die "ERROR: _check_response(): ok field not found in slack response hash! to='$to', from='$from'\n" . Dumper($response);
168 }
169 else
170 {
171 0 ▶ if ($response->{ok})
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'ok'}) { } 0 0 0
172 {
173 0 ▶ if (exists $response->{warning})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{'warning'}) { } 0 0 0
174 {
175 0 return 1;
176 }
177 else
178 {
179 0 return 0;
180 }
181 }
182 else
183 {
184 0 ▶ if ($response->{error} eq 'channel_not_found')
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'error'} eq 'channel_not_found') { } 0 0 0
185 {
186 0 my $code = $channel_not_found->(
187 $self,
188 response => $response,
189 to => $to,
190 from => $from,
191 text => $text
192 );
193
194 0 ▶ return 2 if ($code == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $code == 0 0 0 0
195 0 return $code; # default to pass through the error returned from $channel_not_found
196 }
197 else
198 {
199 0 die "ERROR: _check_response(): ok is false and error='$response->{error}'! to='$to', from='$from'\n" . Dumper($response);
200 }
201 }
202 }
203 }
204  
205 # This is a helper method that wraps get_upload_url_external(), post_file_to_external_url()
206 # and complete_upload_external() calls for the caller. They have to provide everything necessary
207 # though.
208 # Returns an array with $code and $response.
209 # $code = 0, 1 or 2 to indicate if it succeeded or 'channel_not_found' handler was called.
210 # $response = last response message returned in the process
211 8 ▶sub upload_v2 {
Subroutine upload_v2: called CC=8 SCAR=22.6
Pod undocumented
212 8 my $self = shift;
213 8 my $args = { error_handler => \&_check_response, @_ };
214 8 my $channel = $args->{channel}; # slack channel name
215 8 my $channel_id = $args->{channel_id}; # internal slack channel id
216 8 my $file_contents = $args->{file_contents}; # contents of the file to upload
217 8 my $file_type = $args->{file_type}; # type of the file
218 8 my $file_length = $args->{file_length}; # length of the file in bytes
219 8 my $filename = $args->{filename}; # what you want the file to be named in Slack
220 8 my $from = $args->{from}; # who we say this message is from
221 8 my $message = $args->{message}; # the message you want associated with the file in slack.
222 8 my $callingObj = $args->{callingObj}; # instance of the caller that $error_handler and
223 # $channel_not_found need passed in as $self.
224 8 my $error_handler = $args->{error_handler}; # sub you should call to validate the returned info
225 #is valid. Takes the following parameters:
226 # response =>, to, from, message, channel_not_found
227 # and returns 0 if ok, 1 otherwise. Can die if a fatal error is encountered.
228 8 my $channel_not_found = $args->{channel_not_found};
229 8 my $code; # tracks the error_handler result.
230
231 8 ▶ if (!defined $error_handler) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $error_handler) 0 8 8
232 0 die "upload_v2(): error_handler must be defined!";
233 }
234 8 ▶ if (defined $error_handler && ref($error_handler) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error_handler and ref $error_handler ne "CODE") 0 8 8
Condition33%1/3
defined $error_handler and ref $error_handler ne "CODE"
!ll&&!rl&&r
080
MC/DC0%0/2
defined $error_handler and ref $error_handler ne "CODE"
defined $error_handler ref $error_handler ne "CODE"
Truth table33%1/3
A and B
A= defined $error_handlerB= ref $error_handler ne "CODE"
ABresult
0X0
100
111
235 0 die "upload_v2(): error_handler is not a CODE ref! You provided: " . ref($error_handler);
236 }
237
238 8 ▶ if (!defined $channel_not_found) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $channel_not_found) 0 8 8
239 0 die "upload_v2(): channel_not_found must be defined!";
240 }
241 8 ▶ if (defined $channel_not_found && ref($channel_not_found) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $channel_not_found and ref $channel_not_found ne "CODE") 0 8 8
Condition33%1/3
defined $channel_not_found and ref $channel_not_found ne "CODE"
!ll&&!rl&&r
080
MC/DC0%0/2
defined $channel_not_found and ref $channel_not_found ne "CODE"
defined $channel_not_found ref $channel_not_found ne "CODE"
Truth table33%1/3
A and B
A= defined $channel_not_foundB= ref $channel_not_found ne "CODE"
ABresult
0X0
100
111
242 0 die "upload_v2(): channel_not_found is not a CODE ref! You provided: " . ref($channel_not_found);
243 }
244  
245 8 my $external_url_response = $self->get_upload_url_external(
246 filename => $filename,
247 length => $file_length
248 );
249 8 $code = $error_handler->(
250 $callingObj,
251 response => $external_url_response,
252 to => $channel,
253 from => $from,
254 text => $message,
255 channel_not_found => $channel_not_found,
256 );
257
258 8 ▶ if ($code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 0) 8 0 8
259 # we got a valid response and can proceed.
260 8 my $url = $external_url_response->{upload_url};
261 8 my $file_id = $external_url_response->{file_id};
262
263 8 $self->send_file_to_external_url($url,
264 {
265 file => $file_contents,
266 file_type => $file_type
267 }
268 );
269
270 8 my $complete_upload_response = $self->complete_upload_external(
271 files => [
272 {
273 id => $file_id,
274 title => $filename
275 },
276 ],
277 channel_id => $channel_id,
278 initial_comment => $message,
279 );
280 8 $code = $error_handler->(
281 $callingObj,
282 response => $complete_upload_response,
283 to => $channel,
284 from => $from,
285 text => $message,
286 channel_not_found => $channel_not_found,
287 );
288
289 8 return ($code, $complete_upload_response);
290 }
291
292 0 return ($code, $external_url_response);
293 }
294  
295 3 1;
296  
↑ Summary WebApi/Client.pm »