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   336 use strict;
  45         100  
  45         1323  
3 45     45   242 use warnings;
  45         94  
  45         1232  
4              
5 45     45   226 use Carp qw/ carp croak /;
  45         85  
  45         2337  
6 45     45   33689 use File::Temp;
  45         738868  
  45         3596  
7 45     45   437 use Furl::HTTP;
  45         111  
  45         1774  
8 45     45   281 use Furl::Headers;
  45         102  
  45         988  
9 45     45   469 use JSON::XS;
  45         108  
  45         1935  
10              
11 45     45   6722 use LINE::Bot::API;
  45         111  
  45         1107  
12 45     45   274 use LINE::Bot::API::Client;
  45         95  
  45         50434  
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   229 my ($response_body) = @_;
19              
20 88 50       295 unless ($response_body) {
21 0         0 croak 'LINE Messaging API error: response body is empty.';
22             }
23              
24 88         167 my ($ret, $error);
25             eval {
26 88         525 $ret = $JSON->decode($response_body);
27 88         323 1;
28 88 50       161 } or do {
29 0         0 $error = $@;
30             };
31              
32 88 50       254 if ($error) {
33 0         0 croak 'LINE Messaging API error: ' . $error . '. response_body=' . $response_body;
34             }
35              
36 88         220 return $ret;
37             }
38              
39             sub new {
40 46     46 0 210 my($class, %args) = @_;
41              
42 46   50     414 $args{http_client} ||= +{};
43 46   33     380 $args{http_client}{agent} ||= "LINE::Bot::API/$LINE::Bot::API::VERSION";
44 46   50     277 $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         111 %{ $args{http_client} }
  46         412  
50             ),
51             }, $class;
52             }
53              
54             sub credentials {
55 85     85 0 202 my $self = shift;
56             (
57 85         605 'Authorization', "Bearer $self->{channel_access_token}"
58             );
59             }
60              
61             sub get {
62 26     26 0 68 my($self, $url) = @_;
63              
64             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->get(
65 26         109 $url,
66             [
67             $self->credentials,
68             ],
69             );
70              
71 26         60158 my $ret = __decode_response_body($res_content);
72 26         55 $ret->{http_status} = $res_status;
73 26         116 $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         6 $url,
81             [
82             $self->credentials,
83             ],
84             );
85              
86 1         1758 $res_content;
87             }
88              
89             sub post {
90 52     52 0 120 my($self, $url, $headers, $data);
91              
92 52 100       231 if (@_ == 3) {
    50          
93 22         60 ($self, $url, $data) = @_;
94 22         46 $headers = [];
95             } elsif (@_ == 4) {
96 30         81 ($self, $url, $headers, $data) = @_;
97             }
98              
99 52         640 my $json = $JSON->encode($data);
100             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
101 52         241 $url,
102             [
103             @$headers,
104             $self->credentials,
105             'Content-Type' => 'application/json',
106             'Content-Length' => length($json),
107             ],
108             $json,
109             );
110              
111 52         284783 my $ret = __decode_response_body($res_content);
112 52         120 $ret->{http_status} = $res_status;
113 52         302 $ret->{http_headers} = Furl::Headers->new($res_headers);
114 52         1697 $ret;
115             }
116              
117             sub post_form {
118 4     4 0 10 my ($self, $url, $headers, $data) = @_;
119              
120 4 100       13 if (@_ == 3) {
    50          
121 3         8 ($self, $url, $data) = @_;
122             } elsif (@_ == 4) {
123 1         3 ($self, $url, $headers, $data) = @_;
124             }
125 4   100     14 $headers //= [];
126              
127             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->post(
128 4         19 $url,
129             [ @$headers, 'Content-Type' => 'application/x-www-form-urlencoded' ],
130             $data,
131             );
132              
133 4         7405 my $ret = __decode_response_body($res_content);
134 4         8 $ret->{http_status} = $res_status;
135 4         16 $ret;
136             }
137              
138             sub post_image {
139 1     1 0 3 my ($self, $url, $headers, $filePath) = @_;
140              
141 1   50     3 $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         2951 close $fh;
156              
157 1         5 my $ret = __decode_response_body($res_content);
158 1         2 $ret->{http_status} = $res_status;
159 1         8 $ret;
160             }
161              
162             sub put {
163 1     1 0 2 my($self, $url, $data) = @_;
164              
165 1         7 my $json = $JSON->encode($data);
166             my($res_minor_version, $res_status, $res_msg, $res_headers, $res_content) = $self->{furl}->put(
167 1         4 $url,
168             [
169             $self->credentials,
170             'Content-Type' => 'application/json',
171             'Content-Length' => length($json),
172             ],
173             $json,
174             );
175              
176 1         2838 my $ret = __decode_response_body($res_content);
177 1         2 $ret->{http_status} = $res_status;
178 1         6 $ret->{http_headers} = Furl::Headers->new($res_headers);
179 1         30 $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         14 $url,
187             [
188             $self->credentials,
189             ],
190             );
191              
192 4         6584 my $ret = __decode_response_body($res_content);
193 4         10 $ret->{http_status} = $res_status;
194 4         17 $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;