NAME
OpenAI::API::Request - Base module for making requests to the OpenAI API
SYNOPSIS
This module is a base module for making HTTP requests to the OpenAI API. It should not be used directly.
package OpenAI::API::Request::NewRequest;
use Moo;
extends 'OpenAI::API::Request';
sub endpoint {
'/my_endpoint'
}
sub method {
'POST'
}
# somewhere else...
use OpenAI::API::Request::NewRequest;
my $req = OpenAI::API::Request::NewRequest->new(...);
my $res = $req->send(); # or: my $res = $req->send_async();
DESCRIPTION
This module provides a base class for creating request objects for the OpenAI API. It includes methods for sending synchronous and asynchronous requests, with support for HTTP GET and POST methods.
ATTRIBUTES
config
An instance of OpenAI::API::Config that provides configuration options for the OpenAI API client. Defaults to a new instance of OpenAI::API::Config.
user_agent
An instance of LWP::UserAgent that is used to make HTTP requests. Defaults to a new instance of LWP::UserAgent with a timeout set to the value of
config->timeout
.
METHODS
endpoint
This method must be implemented by subclasses. It should return the API endpoint for the specific request.
method
This method must be implemented by subclasses. It should return the HTTP method for the specific request.
send
Send a request synchronously.
my $response = $request->send();
send_async
Send a request asynchronously. Returns a future that will be resolved with the decoded JSON response.
Here's an example usage:
use IO::Async::Loop;
my $loop = IO::Async::Loop->new();
my $future = $request->send_async()->then(
sub {
my $content = shift;
# ...
}
)->catch(
sub {
my $error = shift;
# ...
}
);
$loop->await($future);
my $res = $future->get;
Note: You can select different event loops via OpenAI::API::Config.