| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebService::Jina::UA; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
530
|
use Moo; |
|
|
1
|
|
|
|
|
6387
|
|
|
|
1
|
|
|
|
|
6
|
|
|
4
|
1
|
|
|
1
|
|
1896
|
use URI; |
|
|
1
|
|
|
|
|
7799
|
|
|
|
1
|
|
|
|
|
26
|
|
|
5
|
1
|
|
|
1
|
|
699
|
use LWP::UserAgent; |
|
|
1
|
|
|
|
|
37717
|
|
|
|
1
|
|
|
|
|
32
|
|
|
6
|
1
|
|
|
1
|
|
451
|
use JSON::Lines; |
|
|
1
|
|
|
|
|
20055
|
|
|
|
1
|
|
|
|
|
9
|
|
|
7
|
1
|
|
|
1
|
|
527
|
use MIME::Base64 qw/encode_base64/; |
|
|
1
|
|
|
|
|
680
|
|
|
|
1
|
|
|
|
|
57
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
370
|
use WebService::Jina::Response; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
81
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has api_key => ( |
|
12
|
|
|
|
|
|
|
is => 'ro', |
|
13
|
|
|
|
|
|
|
lazy => 1, |
|
14
|
|
|
|
|
|
|
required => 1, |
|
15
|
|
|
|
|
|
|
); |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has ua => ( |
|
18
|
|
|
|
|
|
|
is => 'ro', |
|
19
|
|
|
|
|
|
|
default => sub { |
|
20
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
|
21
|
1
|
|
|
1
|
|
519
|
use Data::Dumper; |
|
|
1
|
|
|
|
|
7264
|
|
|
|
1
|
|
|
|
|
541
|
|
|
22
|
|
|
|
|
|
|
warn Dumper $_[0]->api_key; |
|
23
|
|
|
|
|
|
|
$ua->default_header('Authorization' => 'Bearer ' . $_[0]->api_key); |
|
24
|
|
|
|
|
|
|
$ua->default_header('User-Agent' => 'Mozilla/5.0'); |
|
25
|
|
|
|
|
|
|
return $ua; |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has json => ( |
|
30
|
|
|
|
|
|
|
is => 'ro', |
|
31
|
|
|
|
|
|
|
default => sub { |
|
32
|
|
|
|
|
|
|
JSON::Lines->new; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub get { |
|
37
|
|
|
|
|
|
|
shift->request( |
|
38
|
0
|
|
|
0
|
0
|
|
type => 'GET', |
|
39
|
|
|
|
|
|
|
@_ |
|
40
|
|
|
|
|
|
|
); |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub post { |
|
44
|
|
|
|
|
|
|
shift->request( |
|
45
|
0
|
|
|
0
|
0
|
|
type => 'POST', |
|
46
|
|
|
|
|
|
|
@_ |
|
47
|
|
|
|
|
|
|
); |
|
48
|
|
|
|
|
|
|
} |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub delete { |
|
51
|
|
|
|
|
|
|
shift->request( |
|
52
|
0
|
|
|
0
|
0
|
|
type => 'DELETE', |
|
53
|
|
|
|
|
|
|
@_ |
|
54
|
|
|
|
|
|
|
); |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub request { |
|
58
|
0
|
|
|
0
|
0
|
|
my ($self, %params) = @_; |
|
59
|
0
|
|
|
|
|
|
my $url = URI->new($params{url}); |
|
60
|
0
|
|
|
|
|
|
my $res; |
|
61
|
0
|
|
|
|
|
|
my $stream_cb = delete $params{data}{stream_cb}; |
|
62
|
0
|
|
|
|
|
|
$self->ua->remove_handler(); |
|
63
|
0
|
0
|
|
|
|
|
if ($stream_cb) { |
|
64
|
|
|
|
|
|
|
$self->ua->add_handler(response_data => sub { |
|
65
|
0
|
|
|
0
|
|
|
my($response, $ua, $handler, $data) = @_; |
|
66
|
0
|
|
|
|
|
|
$data = $self->json->decode($data); |
|
67
|
0
|
|
|
|
|
|
for (@{$data}) { |
|
|
0
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
$stream_cb->(WebService::Jina::Response->new(%{$_})); |
|
|
0
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
} |
|
70
|
0
|
|
|
|
|
|
return 1; |
|
71
|
0
|
|
|
|
|
|
}); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
0
|
0
|
|
|
|
|
my %headers = %{ $params{headers} || {} }; |
|
|
0
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if ($params{type} eq 'GET') { |
|
|
|
0
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
$url->query_form($params{data}); |
|
76
|
0
|
|
|
|
|
|
$res = $self->ua->get($url, %headers); |
|
77
|
|
|
|
|
|
|
} elsif ($params{type} eq 'DELETE') { |
|
78
|
|
|
|
|
|
|
$res = $self->ua->delete( |
|
79
|
|
|
|
|
|
|
$url, |
|
80
|
|
|
|
|
|
|
%headers, |
|
81
|
0
|
|
|
|
|
|
content => $self->json->encode([$params{data}]), |
|
82
|
|
|
|
|
|
|
'Content-Type' => 'application/json' |
|
83
|
|
|
|
|
|
|
); |
|
84
|
|
|
|
|
|
|
} else { |
|
85
|
|
|
|
|
|
|
$res = $self->ua->post( |
|
86
|
|
|
|
|
|
|
$url, |
|
87
|
|
|
|
|
|
|
%headers, |
|
88
|
0
|
|
|
|
|
|
content => $self->json->encode([$params{data}]), |
|
89
|
|
|
|
|
|
|
'Content-Type' => 'application/json' |
|
90
|
|
|
|
|
|
|
); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
0
|
|
|
|
|
|
return $self->response($res); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub response { |
|
96
|
0
|
|
|
0
|
0
|
|
my ($self, $res) = @_; |
|
97
|
0
|
0
|
|
|
|
|
if ($res->is_success) { |
|
98
|
0
|
0
|
|
|
|
|
if ($res->content_type eq 'text/plain') { |
|
99
|
0
|
|
|
|
|
|
return $res->decoded_content; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
0
|
|
|
|
|
|
my $content = $self->json->decode($res->decoded_content); |
|
102
|
|
|
|
|
|
|
my @res = map { |
|
103
|
0
|
|
|
|
|
|
WebService::Jina::Response->new(%{$_}) |
|
|
0
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
} @{$content}; |
|
|
0
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
return scalar @res == 1 ? $res[0] : \@res; |
|
106
|
|
|
|
|
|
|
} |
|
107
|
0
|
|
|
|
|
|
die $res->decoded_content; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub base64_images { |
|
111
|
0
|
|
|
0
|
0
|
|
my ($self, $images) = @_; |
|
112
|
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my @out; |
|
114
|
0
|
|
|
|
|
|
for my $image (@{$images}) { |
|
|
0
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
open my $fh, '<', $image; |
|
116
|
0
|
|
|
|
|
|
my $content = do { local $/; <$fh> }; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
close $fh; |
|
118
|
0
|
|
|
|
|
|
push @out, encode_base64($content); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
0
|
|
|
|
|
|
return \@out; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |