| 1 | package OpenAI::API::Request; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 18 | ▶ | use IO::Async::Loop; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 18 | ▶ | use IO::Async::Future; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 18 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 18 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 18 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 18 | ▶ | use strictures 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 18 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 18 | ▶ | use OpenAI::API::Config; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 18 | ▶ | use OpenAI::API::Error; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | has 'config' => ( | ||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||
| 17 | 0 | ▶ | default => sub { OpenAI::API::Config->new() }, | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 18 | isa => sub { | ||||||||||||||||||||
| 19 | 4 | ▶ | die "config must be an instance of OpenAI::API::Config" | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 20 | unless ref $_[0] eq 'OpenAI::API::Config'; | ||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||
| 22 | coerce => sub { | ||||||||||||||||||||
| 23 | 4 | ▶ | return $_[0] if ref $_[0] eq 'OpenAI::API::Config'; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=10.5
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 0 | return OpenAI::API::Config->new( %{ $_[0] } ); | |||||||||||||||||||
| 25 | }, | ||||||||||||||||||||
| 26 | 18 | ); | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 18 | has 'user_agent' => ( | |||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||
| 31 | builder => '_build_user_agent', | ||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 18 | has 'event_loop' => ( | |||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||
| 36 | lazy => 1, | ||||||||||||||||||||
| 37 | builder => '_build_event_loop', | ||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub _build_user_agent { | ||||||||||||||||||||
| 41 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_build_user_agent: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 4 | $self->{user_agent} = LWP::UserAgent->new( timeout => $self->config->timeout ); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | sub _build_event_loop { | ||||||||||||||||||||
| 46 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_build_event_loop: called CC=2 SCAR=7.0
| |||||||||||||||||||||
| 47 | 4 | my $class = $self->config->event_loop_class; | |||||||||||||||||||
| 48 | 4 | ▶ | eval "require $class" or die "Failed to load event loop class $class: $@"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 4 | return $class->new(); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub endpoint { | ||||||||||||||||||||
| 53 | 0 | ▶ | die "Must be implemented"; | ||||||||||||||||||
|
Subroutine
endpoint: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub method { | ||||||||||||||||||||
| 57 | 0 | ▶ | die "Must be implemented"; | ||||||||||||||||||
|
Subroutine
method: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub _parse_response { | ||||||||||||||||||||
| 61 | 0 | ▶ | my ( $self, $res ) = @_; | ||||||||||||||||||
|
Subroutine
_parse_response: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | # Replace s/Request/Response/ to find the response module | ||||||||||||||||||||
| 66 | 0 | ( my $response_module = $class ) =~ s/Request/Response/; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # Require the OpenAI::API::Response module | ||||||||||||||||||||
| 69 | 0 | ▶ | eval "require $response_module" or die $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | # Return the OpenAI::API::Response object | ||||||||||||||||||||
| 72 | 0 | my $decoded_res = decode_json( $res->decoded_content ); | |||||||||||||||||||
| 73 | 0 | return $response_module->new($decoded_res); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | sub request_params { | ||||||||||||||||||||
| 77 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
request_params: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 78 | 4 | my %request_params = %{$self}; | |||||||||||||||||||
| 79 | 4 | delete $request_params{config}; | |||||||||||||||||||
| 80 | 4 | delete $request_params{user_agent}; | |||||||||||||||||||
| 81 | 4 | delete $request_params{event_loop}; | |||||||||||||||||||
| 82 | 4 | return \%request_params; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | sub send { | ||||||||||||||||||||
| 86 | 4 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
send: called CC=5 SCAR=24.1
Pod
documented
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 4 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 0 | warn "Sending config via send is deprecated. More info: perldoc OpenAI::API::Config\n"; | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 4 | my %args = @_; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 4 | ▶ | my $res = | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 95 | $self->method eq 'POST' ? $self->_post() | ||||||||||||||||||||
| 96 | : $self->method eq 'GET' ? $self->_get() | ||||||||||||||||||||
| 97 | : die "Invalid method"; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | ▶ | if ( $args{http_response} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 0 | return $res; | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | return $self->_parse_response($res); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | sub _get { | ||||||||||||||||||||
| 107 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_get: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 0 | my $req = $self->_create_request('GET'); | |||||||||||||||||||
| 110 | 0 | return $self->_send_request($req); | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | sub _post { | ||||||||||||||||||||
| 114 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_post: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 4 | my $req = $self->_create_request( 'POST', encode_json( $self->request_params() ) ); | |||||||||||||||||||
| 117 | 4 | return $self->_send_request($req); | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | sub send_async { | ||||||||||||||||||||
| 121 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||
|
Subroutine
send_async: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 0 | ▶ | my $res_future = | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 124 | $self->method eq 'POST' ? $self->_post_async() | ||||||||||||||||||||
| 125 | : $self->method eq 'GET' ? $self->_get_async() | ||||||||||||||||||||
| 126 | : die "Invalid method"; | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | ▶ | if ( $args{http_response} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | 0 | return $res_future; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # Return a new future that resolves to $res->decoded_content | ||||||||||||||||||||
| 133 | my $decoded_content_future = $res_future->then( | ||||||||||||||||||||
| 134 | sub { | ||||||||||||||||||||
| 135 | 0 | ▶ | my $res = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 136 | 0 | return $self->_parse_response($res); | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | 0 | ); | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | return $decoded_content_future; | |||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | sub _get_async { | ||||||||||||||||||||
| 144 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_get_async: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 0 | my $req = $self->_create_request('GET'); | |||||||||||||||||||
| 147 | 0 | return $self->_send_request_async($req); | |||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | sub _post_async { | ||||||||||||||||||||
| 151 | 0 | ▶ | my ( $self, $config ) = @_; | ||||||||||||||||||
|
Subroutine
_post_async: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | 0 | my $req = $self->_create_request( 'POST', encode_json( $self->request_params() ) ); | |||||||||||||||||||
| 154 | 0 | return $self->_send_request_async($req); | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | sub _create_request { | ||||||||||||||||||||
| 158 | 4 | ▶ | my ( $self, $method, $content ) = @_; | ||||||||||||||||||
|
Subroutine
_create_request: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 4 | my $req = HTTP::Request->new( | |||||||||||||||||||
| 161 | $method => $self->config->api_base . "/" . $self->endpoint, | ||||||||||||||||||||
| 162 | $self->_request_headers(), | ||||||||||||||||||||
| 163 | $content, | ||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 4 | return $req; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | sub _request_headers { | ||||||||||||||||||||
| 170 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
_request_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | return [ | ||||||||||||||||||||
| 173 | 4 | 'Content-Type' => 'application/json', | |||||||||||||||||||
| 174 | 'Authorization' => 'Bearer ' . $self->config->api_key, | ||||||||||||||||||||
| 175 | ]; | ||||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | sub _send_request { | ||||||||||||||||||||
| 179 | 4 | ▶ | my ( $self, $req ) = @_; | ||||||||||||||||||
|
Subroutine
_send_request: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 4 | my $loop = IO::Async::Loop->new(); | |||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | 4 | my $future = $self->_async_http_send_request($req); | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 4 | $loop->await($future); | |||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 4 | my $res = $future->get; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 4 | ▶ | if ( !$res->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 4 | OpenAI::API::Error->throw( | |||||||||||||||||||
| 191 | 4 | message => "Error: '@{[ $res->status_line ]}'", | |||||||||||||||||||
| 192 | request => $req, | ||||||||||||||||||||
| 193 | response => $res, | ||||||||||||||||||||
| 194 | ); | ||||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | return $res; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | sub _send_request_async { | ||||||||||||||||||||
| 201 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||||||||||||
|
Subroutine
_send_request_async: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | return $self->_async_http_send_request($req)->then( | ||||||||||||||||||||
| 204 | sub { | ||||||||||||||||||||
| 205 | 0 | ▶ | my $res = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 0 | ▶ | if ( !$res->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 208 | 0 | OpenAI::API::Error->throw( | |||||||||||||||||||
| 209 | 0 | message => "Error: '@{[ $res->status_line ]}'", | |||||||||||||||||||
| 210 | request => $req, | ||||||||||||||||||||
| 211 | response => $res, | ||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 0 | return $res; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | )->catch( | ||||||||||||||||||||
| 218 | sub { | ||||||||||||||||||||
| 219 | 0 | ▶ | my $err = shift; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 220 | 0 | die $err; | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | 0 | ); | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | sub _http_send_request { | ||||||||||||||||||||
| 226 | 4 | ▶ | my ( $self, $req ) = @_; | ||||||||||||||||||
|
Subroutine
_http_send_request: called CC=5 SCAR=19.4
| |||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 4 | for my $attempt ( 1 .. $self->config->retry ) { | |||||||||||||||||||
| 229 | 4 | my $res = $self->user_agent->request($req); | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 4 | ▶ | if ( $res->is_success ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 232 | 0 | return $res; | |||||||||||||||||||
| 233 | ▶ | } elsif ( $res->code =~ /^(?:500|503|504|599)$/ && $attempt < $self->config->retry ) { | |||||||||||||||||||
|
Condition33%1/3
$res->code =~ /^(?:500|503|504|599)$/ and $attempt < $self->config->retry
MC/DC0%0/2
$res->code =~ /^(?:500|503|504|599)$/ and $attempt < $self->config->retry
$res->code =~ /^(?:500|503|504|599)$/
$attempt < $self->config->retry
Truth table33%1/3
A and B
A= $res->code =~ /^(?:500|503|504|599)$/B= $attempt < $self->config->retry
| |||||||||||||||||||||
| 234 | 0 | sleep( $self->config->sleep ); | |||||||||||||||||||
| 235 | } else { | ||||||||||||||||||||
| 236 | 4 | return $res; | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | } | ||||||||||||||||||||
| 239 | } | ||||||||||||||||||||
| 240 | |||||||||||||||||||||
| 241 | sub _async_http_send_request { | ||||||||||||||||||||
| 242 | 4 | ▶ | my ( $self, $req ) = @_; | ||||||||||||||||||
|
Subroutine
_async_http_send_request: called CC=1 SCAR=0.2
| |||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | 4 | my $future = IO::Async::Future->new; | |||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | $self->event_loop->later( | ||||||||||||||||||||
| 247 | sub { | ||||||||||||||||||||
| 248 | eval { | ||||||||||||||||||||
| 249 | 4 | my $res = $self->_http_send_request($req); | |||||||||||||||||||
| 250 | 4 | $future->done($res); | |||||||||||||||||||
| 251 | 4 | 1; | |||||||||||||||||||
| 252 | 4 | ▶ | } or do { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=10.5
Branch: 1 branch
| |||||||||||||||||||||
| 253 | 0 | my $err = $@; | |||||||||||||||||||
| 254 | 0 | $future->fail($err); | |||||||||||||||||||
| 255 | }; | ||||||||||||||||||||
| 256 | } | ||||||||||||||||||||
| 257 | 4 | ); | |||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | 4 | return $future; | |||||||||||||||||||
| 260 | } | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | 18 | 1; | |||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | __END__ | ||||||||||||||||||||