File Coverage

blib/lib/LINE/Bot/API/Client/Furl.pm
Criterion Covered Total %
statement 91 100 91.0
branch 10 18 55.5
condition 6 14 42.8
subroutine 19 20 95.0
pod 0 10 0.0
total 126 162 77.7


line stmt bran cond sub pod time code
1             package LINE::Bot::API::Client::Furl;
2 49     49   374 use strict;
  49         110  
  49         1743  
3 49     49   261 use warnings;
  49         105  
  49         1556  
4              
5 49     49   256 use Carp qw/ carp croak /;
  49         153  
  49         2918  
6 49     49   38083 use File::Temp;
  49         854436  
  49         4117  
7 49     49   472 use Furl::HTTP;
  49         120  
  49         2122  
8 49     49   318 use Furl::Headers;
  49         117  
  49         1142  
9 49     49   529 use JSON::XS;
  49         120  
  49         2276  
10              
11 49     49   7316 use LINE::Bot::API;
  49         127  
  49         1212  
12 49     49   325 use LINE::Bot::API::Client;
  49         125  
  49         57309  
13              
14             our @CARP_NOT = qw( LINE::Bot::API::Client::Furl LINE::Bot::API::Client LINE::Bot::API);
15             my $JSON = JSON::XS->new->utf8;
16              
17             sub __decode_response_body {
18 92     92   276 my ($response_body) = @_;
19              
20 92 50       376 unless ($response_body) {
21 0         0 croak 'LINE Messaging API error: response body is empty.';
22             }
23              
24 92         203 my ($ret, $error);
25             eval {
26 92         762 $ret = $JSON->decode($response_body);
27 92         353 1;
28 92 50       200 } or do {
29 0         0 $error = $@;
30             };
31              
32 92 50       299 if ($error) {
33 0         0 croak 'LINE Messaging API error: ' . $error . '. response_body=' . $response_body;
34             }
35              
36 92         244 return $ret;
37             }
38              
39             sub new {
40 50     50 0 255 my($class, %args) = @_;
41              
42 50   50     493 $args{http_client} ||= +{};
43 50   33     421 $args{http_client}{agent} ||= "LINE::Bot::API/$LINE::Bot::API::VERSION";
44 50   50     313 $args{http_client}{timeout} ||= 3;
45             bless {
46             channel_secret => $args{channel_secret},
47             channel_access_token => $args{channel_access_token},
48             furl => Furl::HTTP->new(
49 50         126 %{ $args{http_client} }
  50         484  
50             ),
51             }, $class;
52             }
53              
54             sub credentials {
55 89     89 0 206 my $self = shift;
56             (
57 89         739 'Authorization', "Bearer $self->{channel_access_token}"
58             );
59             }
60              
61             sub get {
62 28     28 0 96 my($self, $url) = @_;
63              
64             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->get(
65 28         133 $url,
66             [
67             $self->credentials,
68             ],
69             );
70              
71 28         65329 my $ret = __decode_response_body($res_content);
72 28         74 $ret->{http_status} = $res_status;
73 28         138 $ret;
74             }
75              
76             sub get_content {
77 1     1 0 2 my($self, $url) = @_;
78              
79             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->get(
80 1         4 $url,
81             [
82             $self->credentials,
83             ],
84             );
85              
86 1         1712 $res_content;
87             }
88              
89             sub post {
90 53     53 0 157 my($self, $url, $headers, $data);
91              
92 53 100       257 if (@_ == 3) {
    50          
93 22         61 ($self, $url, $data) = @_;
94 22         58 $headers = [];
95             } elsif (@_ == 4) {
96 31         96 ($self, $url, $headers, $data) = @_;
97             }
98              
99 53         753 my $json = $JSON->encode($data);
100             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
101 53         325 $url,
102             [
103             @$headers,
104             $self->credentials,
105             'Content-Type' => 'application/json',
106             'Content-Length' => length($json),
107             ],
108             $json,
109             );
110              
111 53         276778 my $ret = __decode_response_body($res_content);
112 53         145 $ret->{http_status} = $res_status;
113 53         945 $ret->{http_headers} = Furl::Headers->new($res_headers);
114 53         1935 $ret;
115             }
116              
117             sub post_form {
118 4     4 0 27 my ($self, $url, $headers, $data) = @_;
119              
120 4 100       21 if (@_ == 3) {
    50          
121 3         11 ($self, $url, $data) = @_;
122             } elsif (@_ == 4) {
123 1         4 ($self, $url, $headers, $data) = @_;
124             }
125 4   100     20 $headers //= [];
126              
127             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
128 4         42 $url,
129             [ @$headers, 'Content-Type' => 'application/x-www-form-urlencoded' ],
130             $data,
131             );
132              
133 4         8197 my $ret = __decode_response_body($res_content);
134 4         12 $ret->{http_status} = $res_status;
135 4         22 $ret;
136             }
137              
138             sub post_image {
139 1     1 0 3 my ($self, $url, $headers, $filePath) = @_;
140              
141 1   50     4 $headers //= [];
142              
143 1 50       48 open my $fh, '<', $filePath
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         9 $url,
148             [
149             @$headers,
150             $self->credentials,
151             ],
152             $fh,
153             );
154              
155 1         2720 close $fh;
156              
157 1         5 my $ret = __decode_response_body($res_content);
158 1         3 $ret->{http_status} = $res_status;
159 1         6 $ret;
160             }
161              
162             sub put {
163 2     2 0 5 my($self, $url, $data) = @_;
164              
165 2         29 my $json = $JSON->encode($data);
166             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->put(
167 2         18 $url,
168             [
169             $self->credentials,
170             'Content-Type' => 'application/json',
171             'Content-Length' => length($json),
172             ],
173             $json,
174             );
175              
176 2         11564 my $ret = __decode_response_body($res_content);
177 2         4 $ret->{http_status} = $res_status;
178 2         18 $ret->{http_headers} = Furl::Headers->new($res_headers);
179 2         85 $ret;
180             }
181              
182             sub delete {
183 4     4 0 12 my($self, $url) = @_;
184              
185             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->delete(
186 4         16 $url,
187             [
188             $self->credentials,
189             ],
190             );
191              
192 4         6629 my $ret = __decode_response_body($res_content);
193 4         9 $ret->{http_status} = $res_status;
194 4         20 $ret;
195             }
196              
197             sub contents_download {
198 0     0 0   my($self, $url, %options) = @_;
199              
200 0   0       my $fh = CORE::delete($options{fh}) || File::Temp->new(%options);
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 0         unless ($res_status eq '200') {
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             1;