File Coverage

blib/lib/WebService/Ollama/UA.pm
Criterion Covered Total %
statement 26 69 37.6
branch 0 10 0.0
condition n/a
subroutine 9 16 56.2
pod 4 6 66.6
total 39 101 38.6


line stmt bran cond sub pod time code
1             package WebService::Ollama::UA;
2              
3 3     3   64 use 5.006;
  3         12  
4 3     3   17 use strict;
  3         7  
  3         106  
5 3     3   21 use warnings;
  3         6  
  3         268  
6              
7             our $VERSION = '0.08';
8              
9 3     3   21 use Moo;
  3         7  
  3         20  
10 3     3   4044 use LWP::UserAgent;
  3         224068  
  3         149  
11 3     3   1972 use JSON::Lines;
  3         94322  
  3         40  
12 3     3   2193 use MIME::Base64 qw/encode_base64/;
  3         2555  
  3         348  
13 3     3   28 use URI;
  3         7  
  3         117  
14              
15 3     3   1726 use WebService::Ollama::Response;
  3         19  
  3         4237  
16              
17             has base_url => (
18             is => 'ro',
19             );
20              
21             has ua => (
22             is => 'ro',
23             default => sub {
24             LWP::UserAgent->new();
25             }
26             );
27              
28             has json => (
29             is => 'ro',
30             default => sub {
31             JSON::Lines->new( utf8 => 1 );
32             }
33             );
34              
35             sub get {
36             shift->request(
37 0     0 1   type => 'GET',
38             @_
39             );
40             }
41              
42             sub post {
43             shift->request(
44 0     0 1   type => 'POST',
45             @_
46             );
47             }
48              
49             sub delete {
50             shift->request(
51 0     0 1   type => 'DELETE',
52             @_
53             );
54             }
55              
56             sub request {
57 0     0 0   my ($self, %params) = @_;
58 0           my $url = URI->new($self->base_url . $params{url});
59 0           my $res;
60              
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::Ollama::Response->new(%{$_}));
  0            
69             }
70 0           return 1;
71 0           });
72             }
73              
74 0 0         if ($params{type} eq 'GET') {
    0          
75 0           $url->query_form($params{data});
76 0           $res = $self->ua->get($url);
77             } elsif ($params{type} eq 'DELETE') {
78             $res = $self->ua->delete(
79             $url,
80 0           content => $self->json->encode([$params{data}]),
81             'Content-Type' => 'application/json'
82             );
83             } else {
84             $res = $self->ua->post(
85             $url,
86 0           content => $self->json->encode([$params{data}]),
87             'Content-Type' => 'application/json'
88             );
89             }
90 0           return $self->response($res);
91             }
92              
93             sub response {
94 0     0 0   my ($self, $res) = @_;
95              
96 0 0         if ($res->is_success) {
97 0           my $content = $self->json->decode($res->decoded_content);
98             my @res = map {
99 0           WebService::Ollama::Response->new(%{$_})
  0            
100 0           } @{$content};
  0            
101 0 0         return scalar @res == 1 ? $res[0] : \@res;
102             }
103            
104 0           die $res->decoded_content;
105             }
106              
107             sub base64_images {
108 0     0 1   my ($self, $images) = @_;
109              
110 0           my @out;
111 0           for my $image (@{$images}) {
  0            
112 0           open my $fh, '<', $image;
113 0           my $content = do { local $/; <$fh> };
  0            
  0            
114 0           close $fh;
115 0           push @out, encode_base64($content);
116             }
117 0           return \@out;
118             }
119              
120             1;
121              
122             __END__