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'
}

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 Promises promise that will be resolved with the decoded JSON response.

Here's an example usage:

my $cv = AnyEvent->condvar;    # Create a condition variable

$request->send_async()->then(
    sub {
        my $response_data = shift;
        print "Response data: " . Dumper($response_data);
    }
)->catch(
    sub {
        my $error = shift;
        print "$error\n";
    }
)->finally(
    sub {
        print "Request completed\n";
        $cv->send();    # Signal the condition variable when the request is completed
    }
);

$cv->recv;              # Keep the script running until the request is completed.

SEE ALSO

OpenAI::API::Config