| 1 | package WebService::Jina::UA; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use URI; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use JSON::Lines; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use MIME::Base64 qw/encode_base64/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | use WebService::Jina::Response; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | has api_key => ( | |||||||||||||
| 12 | is => 'ro', | ||||||||||||||
| 13 | lazy => 1, | ||||||||||||||
| 14 | required => 1, | ||||||||||||||
| 15 | ); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | has ua => ( | ||||||||||||||
| 18 | is => 'ro', | ||||||||||||||
| 19 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.2
| |||||||||||||||
| 20 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||
| 21 | 1 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 0 | warn Dumper $_[0]->api_key; | |||||||||||||
| 23 | 0 | $ua->default_header('Authorization' => 'Bearer ' . $_[0]->api_key); | |||||||||||||
| 24 | 0 | $ua->default_header('User-Agent' => 'Mozilla/5.0'); | |||||||||||||
| 25 | 0 | return $ua; | |||||||||||||
| 26 | } | ||||||||||||||
| 27 | 1 | ); | |||||||||||||
| 28 | |||||||||||||||
| 29 | has json => ( | ||||||||||||||
| 30 | is => 'ro', | ||||||||||||||
| 31 | 0 | ▶ | default => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 32 | 0 | JSON::Lines->new; | |||||||||||||
| 33 | } | ||||||||||||||
| 34 | 1 | ); | |||||||||||||
| 35 | |||||||||||||||
| 36 | 0 | ▶ | sub get { | ||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 37 | shift->request( | ||||||||||||||
| 38 | 0 | type => 'GET', | |||||||||||||
| 39 | @_ | ||||||||||||||
| 40 | ); | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | sub post { | ||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 44 | shift->request( | ||||||||||||||
| 45 | 0 | type => 'POST', | |||||||||||||
| 46 | @_ | ||||||||||||||
| 47 | ); | ||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | sub delete { | ||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 51 | shift->request( | ||||||||||||||
| 52 | 0 | type => 'DELETE', | |||||||||||||
| 53 | @_ | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | } | ||||||||||||||
| 56 | |||||||||||||||
| 57 | 0 | ▶ | sub request { | ||||||||||||
|
Subroutine
request: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 58 | 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 | ▶ | if ($stream_cb) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 0 | ▶ | $self->ua->add_handler(response_data => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||
| 65 | 0 | my($response, $ua, $handler, $data) = @_; | |||||||||||||
| 66 | 0 | $data = $self->json->decode($data); | |||||||||||||
| 67 | 0 | for (@{$data}) { | |||||||||||||
| 68 | 0 | $stream_cb->(WebService::Jina::Response->new(%{$_})); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | 0 | return 1; | |||||||||||||
| 71 | 0 | }); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | 0 | ▶ | my %headers = %{ $params{headers} || {} }; | ||||||||||||
|
Condition0%0/2
$params{'headers'} || {}
MC/DC0%0/1
$params{'headers'} || {}
$params{'headers'}
Truth table0%0/2
A
A= $params{'headers'}
| |||||||||||||||
| 74 | 0 | ▶ | if ($params{type} eq 'GET') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 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 | 0 | ▶ | sub response { | ||||||||||||
|
Subroutine
response: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 96 | 0 | my ($self, $res) = @_; | |||||||||||||
| 97 | 0 | ▶ | if ($res->is_success) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 0 | ▶ | if ($res->content_type eq 'text/plain') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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(%{$_}) | |||||||||||||
| 104 | 0 | } @{$content}; | |||||||||||||
| 105 | 0 | ▶ | return scalar @res == 1 ? $res[0] : \@res; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | } | ||||||||||||||
| 107 | 0 | die $res->decoded_content; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 0 | ▶ | sub base64_images { | ||||||||||||
|
Subroutine
base64_images: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 111 | 0 | my ($self, $images) = @_; | |||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | my @out; | |||||||||||||
| 114 | 0 | for my $image (@{$images}) { | |||||||||||||
| 115 | 0 | open my $fh, '<', $image; | |||||||||||||
| 116 | 0 | my $content = do { local $/; <$fh> }; | |||||||||||||
| 117 | 0 | close $fh; | |||||||||||||
| 118 | 0 | push @out, encode_base64($content); | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | 0 | return \@out; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | 1 | 1; | |||||||||||||