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 45     45   316 use strict;
  45         112  
  45         1376  
3 45     45   250 use warnings;
  45         109  
  45         1284  
4              
5 45     45   254 use Carp qw/ carp croak /;
  45         97  
  45         2483  
6 45     45   32289 use File::Temp;
  45         749895  
  45         3640  
7 45     45   418 use Furl::HTTP;
  45         116  
  45         1913  
8 45     45   286 use Furl::Headers;
  45         105  
  45         1031  
9 45     45   500 use JSON::XS;
  45         114  
  45         2088  
10              
11 45     45   6655 use LINE::Bot::API;
  45         134  
  45         1109  
12 45     45   253 use LINE::Bot::API::Client;
  45         103  
  45         52031  
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 88     88   265 my ($response_body) = @_;
19              
20 88 50       302 unless ($response_body) {
21 0         0 croak 'LINE Messaging API error: response body is empty.';
22             }
23              
24 88         184 my ($ret, $error);
25             eval {
26 88         561 $ret = $JSON->decode($response_body);
27 88         285 1;
28 88 50       166 } or do {
29 0         0 $error = $@;
30             };
31              
32 88 50       237 if ($error) {
33 0         0 croak 'LINE Messaging API error: ' . $error . '. response_body=' . $response_body;
34             }
35              
36 88         186 return $ret;
37             }
38              
39             sub new {
40 46     46 0 224 my($class, %args) = @_;
41              
42 46   50     428 $args{http_client} ||= +{};
43 46   33     383 $args{http_client}{agent} ||= "LINE::Bot::API/$LINE::Bot::API::VERSION";
44 46   50     284 $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 46         106 %{ $args{http_client} }
  46         427  
50             ),
51             }, $class;
52             }
53              
54             sub credentials {
55 85     85 0 164 my $self = shift;
56             (
57 85         611 'Authorization', "Bearer $self->{channel_access_token}"
58             );
59             }
60              
61             sub get {
62 26     26 0 81 my($self, $url) = @_;
63              
64             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->get(
65 26         114 $url,
66             [
67             $self->credentials,
68             ],
69             );
70              
71 26         51638 my $ret = __decode_response_body($res_content);
72 26         62 $ret->{http_status} = $res_status;
73 26         119 $ret;
74             }
75              
76             sub get_content {
77 1     1 0 3 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         1984 $res_content;
87             }
88              
89             sub post {
90 52     52 0 130 my($self, $url, $headers, $data);
91              
92 52 100       216 if (@_ == 3) {
    50          
93 22         59 ($self, $url, $data) = @_;
94 22         58 $headers = [];
95             } elsif (@_ == 4) {
96 30         87 ($self, $url, $headers, $data) = @_;
97             }
98              
99 52         643 my $json = $JSON->encode($data);
100             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
101 52         232 $url,
102             [
103             @$headers,
104             $self->credentials,
105             'Content-Type' => 'application/json',
106             'Content-Length' => length($json),
107             ],
108             $json,
109             );
110              
111 52         285287 my $ret = __decode_response_body($res_content);
112 52         129 $ret->{http_status} = $res_status;
113 52         310 $ret->{http_headers} = Furl::Headers->new($res_headers);
114 52         1848 $ret;
115             }
116              
117             sub post_form {
118 4     4 0 12 my ($self, $url, $headers, $data) = @_;
119              
120 4 100       13 if (@_ == 3) {
    50          
121 3         7 ($self, $url, $data) = @_;
122             } elsif (@_ == 4) {
123 1         4 ($self, $url, $headers, $data) = @_;
124             }
125 4   100     15 $headers //= [];
126              
127             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
128 4         25 $url,
129             [ @$headers, 'Content-Type' => 'application/x-www-form-urlencoded' ],
130             $data,
131             );
132              
133 4         7589 my $ret = __decode_response_body($res_content);
134 4         9 $ret->{http_status} = $res_status;
135 4         14 $ret;
136             }
137              
138             sub post_image {
139 1     1 0 4 my ($self, $url, $headers, $filePath) = @_;
140              
141 1   50     12 $headers //= [];
142              
143 1 50       56 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         2694 close $fh;
156              
157 1         6 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 1     1 0 3 my($self, $url, $data) = @_;
164              
165 1         29 my $json = $JSON->encode($data);
166             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->put(
167 1         6 $url,
168             [
169             $self->credentials,
170             'Content-Type' => 'application/json',
171             'Content-Length' => length($json),
172             ],
173             $json,
174             );
175              
176 1         3877 my $ret = __decode_response_body($res_content);
177 1         2 $ret->{http_status} = $res_status;
178 1         9 $ret->{http_headers} = Furl::Headers->new($res_headers);
179 1         37 $ret;
180             }
181              
182             sub delete {
183 4     4 0 19 my($self, $url) = @_;
184              
185             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->delete(
186 4         19 $url,
187             [
188             $self->credentials,
189             ],
190             );
191              
192 4         6104 my $ret = __decode_response_body($res_content);
193 4         10 $ret->{http_status} = $res_status;
194 4         18 $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;