API/Client/Furl.pm

filter: Statementstmt 91.2% 94 / 103 Branchbran 55.5% 10 / 18 Conditioncond 46.1% 6 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 10 totaltotal 76.0% 130 / 171 CC 16 SCAR 12.3
CC
16
Cov
82%
contents_download
12.0
__decode_response_body
4.7
new
4.2
CRAP 3.4
« API/Builder/FlexMessage.pm ↑ Summary API/Client.pm »
1 package LINE::Bot::API::Client::Furl;
2 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 51 ▶use Carp qw/ carp croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 51 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 51 ▶use Furl::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 51 ▶use Furl::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 51 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 51 ▶use LINE::Bot::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 51 ▶use LINE::Bot::API::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 51 our @CARP_NOT = qw( LINE::Bot::API::Client::Furl LINE::Bot::API::Client LINE::Bot::API);
15 51 my $JSON = JSON::XS->new->utf8;
16  
17 102 ▶sub __decode_response_body {
Subroutine __decode_response_body: called CC=4 SCAR=15.5
18 102 my ($response_body) = @_;
19  
20 102 ▶ unless ($response_body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response_body) 0 102 102
21 0 croak 'LINE Messaging API error: response body is empty.';
22 }
23  
24 102 my ($ret, $error);
25 eval {
26 102 $ret = $JSON->decode($response_body);
27 102 1;
28 102 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $ret = $JSON->decode($response_body); 1 } } or do { $error = $@ } 102 0 102
29 0 $error = $@;
30 };
31  
32 102 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 102 102
33 0 croak 'LINE Messaging API error: ' . $error . '. response_body=' . $response_body;
34 }
35  
36 102 return $ret;
37 }
38  
39 52 ▶sub new {
Subroutine new: called CC=4 SCAR=14.5
Pod undocumented
40 52 my($class, %args) = @_;
41  
42 52 ▶ $args{http_client} ||= +{};
Condition50%1/2
$args{"http_client"} ||= {}
l!l
052
MC/DC0%0/1
$args{"http_client"} ||= {}
$args{"http_client"}
Truth table50%1/2
A
A= $args{"http_client"}
Aresult
11
00
43 52 ▶ $args{http_client}{agent} ||= "LINE::Bot::API/$LINE::Bot::API::VERSION";
Condition50%1/2
$args{"http_client"}{"agent"} ||= "LINE::Bot::API/$LINE::Bot::API::VERSION"
l!l
052
MC/DC0%0/1
$args{"http_client"}{"agent"} ||= "LINE::Bot::API/$LINE::Bot::API::VERSION"
$args{"http_client"}{"agent"}
Truth table50%1/2
A
A= $args{"http_client"}{"agent"}
Aresult
11
00
44 52 ▶ $args{http_client}{timeout} ||= 3;
Condition50%1/2
$args{"http_client"}{"timeout"} ||= 3
l!l
052
MC/DC0%0/1
$args{"http_client"}{"timeout"} ||= 3
$args{"http_client"}{"timeout"}
Truth table50%1/2
A
A= $args{"http_client"}{"timeout"}
Aresult
11
00
45 bless {
46 channel_secret => $args{channel_secret},
47 channel_access_token => $args{channel_access_token},
48 furl => Furl::HTTP->new(
49 52 %{ $args{http_client} }
50 ),
51 }, $class;
52 }
53  
54 99 ▶sub credentials {
Subroutine credentials: called CC=1 SCAR=0.0
Pod undocumented
55 99 my $self = shift;
56 (
57 99 'Authorization', "Bearer $self->{channel_access_token}"
58 );
59 }
60  
61 32 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
62 32 my($self, $url) = @_;
63  
64 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->get(
65 32 $url,
66 [
67 $self->credentials,
68 ],
69 );
70  
71 32 my $ret = __decode_response_body($res_content);
72 32 $ret->{http_status} = $res_status;
73 32 $ret;
74 }
75  
76 1 ▶sub get_content {
Subroutine get_content: called CC=1 SCAR=0.0
Pod undocumented
77 1 my($self, $url) = @_;
78  
79 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->get(
80 1 $url,
81 [
82 $self->credentials,
83 ],
84 );
85  
86 1 $res_content;
87 }
88  
89 59 ▶sub post {
Subroutine post: called CC=3 SCAR=11.0
Pod undocumented
90 59 my($self, $url, $headers, $data);
91  
92 59 ▶ if (@_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3) { } 28 31 59
elsif (@_ == 4) { } 31 0 31
93 28 ($self, $url, $data) = @_;
94 28 $headers = [];
95 } elsif (@_ == 4) {
96 31 ($self, $url, $headers, $data) = @_;
97 }
98  
99 59 my $json = $JSON->encode($data);
100 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
101 59 $url,
102 [
103 @$headers,
104 $self->credentials,
105 'Content-Type' => 'application/json',
106 'Content-Length' => length($json),
107 ],
108 $json,
109 );
110  
111 59 my $ret = __decode_response_body($res_content);
112 59 $ret->{http_status} = $res_status;
113 59 $ret->{http_headers} = Furl::Headers->new($res_headers);
114 59 $ret;
115 }
116  
117 4 ▶sub post_form {
Subroutine post_form: called CC=4 SCAR=13.9
Pod undocumented
118 4 my ($self, $url, $headers, $data) = @_;
119  
120 4 ▶ if (@_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3) { } 3 1 4
elsif (@_ == 4) { } 1 0 1
121 3 ($self, $url, $data) = @_;
122 } elsif (@_ == 4) {
123 1 ($self, $url, $headers, $data) = @_;
124 }
125 4 ▶ $headers //= [];
Condition100%2/2
$headers //= []
l!l
31
MC/DC100%1/1
$headers //= []
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
126  
127 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
128 4 $url,
129 [ @$headers, 'Content-Type' => 'application/x-www-form-urlencoded' ],
130 $data,
131 );
132  
133 4 my $ret = __decode_response_body($res_content);
134 4 $ret->{http_status} = $res_status;
135 4 $ret;
136 }
137  
138 1 ▶sub post_image {
Subroutine post_image: called CC=3 SCAR=11.1
Pod undocumented
139 1 my ($self, $url, $headers, $filePath) = @_;
140  
141 1 ▶ $headers //= [];
Condition50%1/2
$headers //= []
l!l
10
MC/DC0%0/1
$headers //= []
$headers
Truth table50%1/2
A
A= $headers
Aresult
11
00
142  
143 1 ▶ open my $fh, '<', $filePath
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filePath or croak("Failed to open file.") 1 0 1
144 or croak 'Failed to open file.';
145  
146 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
147 1 $url,
148 [
149 @$headers,
150 $self->credentials,
151 ],
152 $fh,
153 );
154  
155 1 close $fh;
156  
157 1 my $ret = __decode_response_body($res_content);
158 1 $ret->{http_status} = $res_status;
159 1 $ret;
160 }
161  
162 2 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod undocumented
163 2 my($self, $url, $data) = @_;
164  
165 2 my $json = $JSON->encode($data);
166 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->put(
167 2 $url,
168 [
169 $self->credentials,
170 'Content-Type' => 'application/json',
171 'Content-Length' => length($json),
172 ],
173 $json,
174 );
175  
176 2 my $ret = __decode_response_body($res_content);
177 2 $ret->{http_status} = $res_status;
178 2 $ret->{http_headers} = Furl::Headers->new($res_headers);
179 2 $ret;
180 }
181  
182 4 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
183 4 my($self, $url) = @_;
184  
185 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->delete(
186 4 $url,
187 [
188 $self->credentials,
189 ],
190 );
191  
192 4 my $ret = __decode_response_body($res_content);
193 4 $ret->{http_status} = $res_status;
194 4 $ret;
195 }
196  
197 0 ▶sub contents_download {
Subroutine contents_download: not called CC=3 SCAR=24.8
Pod undocumented
198 0 my($self, $url, %options) = @_;
199  
200 0 ▶ my $fh = CORE::delete($options{fh}) || File::Temp->new(%options);
Condition0%0/3
delete $options{'fh'} || 'File::Temp'->new(%options)
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $options{'fh'} || 'File::Temp'->new(%options)
delete $options{'fh'} 'File::Temp'->new(%options)
Truth table0%0/3
A || B
A= delete $options{'fh'}B= 'File::Temp'->new(%options)
ABresult
1X1
011
000
201  
202 my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->request(
203 0 method => 'GET',
204 url => $url,
205 write_file => $fh,
206 headers => [
207 $self->credentials,
208 ],
209 );
210 0 ▶ unless ($res_status eq '200') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res_status eq 200) 0 0 0
211 0 carp "LINE Messaging API contents_download error: $res_status $url\n\tcontent=$res_content";
212 }
213  
214 +{
215 0 http_status => $res_status,
216 fh => $fh,
217 headers => $res_headers,
218 };
219 }
220  
221 51 1;
« API/Builder/FlexMessage.pm ↑ Summary API/Client.pm »