File Coverage

blib/lib/WebService/Chroma/UA.pm
Criterion Covered Total %
statement 12 30 40.0
branch 0 6 0.0
condition n/a
subroutine 4 9 44.4
pod 0 5 0.0
total 16 50 32.0


line stmt bran cond sub pod time code
1             package WebService::Chroma::UA;
2              
3 1     1   8 use Moo;
  1         2  
  1         35  
4 1     1   1238 use LWP::UserAgent;
  1         56384  
  1         35  
5 1     1   1844 use JSON;
  1         8448  
  1         4  
6 1     1   156 use Module::Load;
  1         1  
  1         7  
7              
8             has base_url => (
9             is => 'ro',
10             );
11              
12             has embeddings_model => (
13             is => 'rw',
14             trigger => sub {
15             if ($_[1] && $_[0]->embeddings) {
16             $_[0]->embeddings->model($_[1]);
17             }
18             }
19             );
20              
21             has embeddings_api_key => (
22             is => 'rw',
23             lazy => 1,
24             trigger => sub {
25             if ($_[1] && $_[0]->embeddings) {
26             $_[0]->embeddings->api_key($_[1]);
27             }
28             }
29             );
30              
31             has embeddings_base_url => (
32             is => 'rw',
33             trigger => sub {
34             if ($_[1] && $_[0]->embeddings) {
35             $_[0]->embeddings->base_url($_[1]);
36             }
37             }
38             );
39              
40             has embeddings_class => (
41             is => 'ro',
42             trigger => sub {
43             return unless $_[1];
44             my $class = 'WebService::Chroma::Embeddings::' . $_[1];
45             load $class;
46             $_[0]->embeddings(
47             $class->new(
48             ($_[0]->embeddings_model ? (model => $_[0]->embeddings_model) : ()),
49             ($_[0]->embeddings_api_key ? (api_key => $_[0]->embeddings_api_key) : ()),
50             )
51             );
52             }
53             );
54              
55             has embeddings => (
56             is => 'rw',
57             );
58              
59             has ua => (
60             is => 'ro',
61             default => sub {
62             LWP::UserAgent->new();
63             }
64             );
65              
66             has json => (
67             is => 'ro',
68             default => sub {
69             JSON->new;
70             }
71             );
72              
73             sub get {
74             shift->request(
75 0     0 0   type => 'GET',
76             @_
77             );
78             }
79              
80             sub post {
81             shift->request(
82 0     0 0   type => 'POST',
83             @_
84             );
85             }
86              
87             sub delete {
88             shift->request(
89 0     0 0   type => 'DELETE',
90             @_
91             );
92             }
93              
94             sub request {
95 0     0 0   my ($self, %params) = @_;
96 0           my $url = URI->new($self->base_url . $params{url});
97 0           my $res;
98 0 0         if ($params{type} eq 'GET') {
    0          
99 0           $url->query_form($params{data});
100 0           $res = $self->ua->get($url);
101             } elsif ($params{type} eq 'DELETE') {
102 0           $url->query_form($params{data});
103 0           $res = $self->ua->delete($url);
104             } else {
105             $res = $self->ua->post(
106             $url,
107 0           content => $self->json->encode($params{data}),
108             'Content-Type' => 'application/json'
109             );
110             }
111 0           return $self->response($res);
112             }
113              
114             sub response {
115 0     0 0   my ($self, $res) = @_;
116              
117 0 0         if ($res->is_success) {
118 0           my $content = $self->json->decode($res->decoded_content);
119 0           return $content;
120             }
121            
122 0           die $res->decoded_content;
123             }
124              
125             1;