openai-interface 0.14.0

A low-level Rust interface for the OpenAI API
Documentation
//! List batch jobs via `GET /batches`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.

use url::Url;

use crate::{
    batches::Batch,
    errors::OapiError,
    pagination::PaginationQuery,
    rest::get::{Get, GetNoStream},
};

/// Lists batch jobs, most recent first.
#[derive(Debug, Clone, Default)]
pub struct ListBatchesRequest<'a> {
    /// The standard pagination parameters (`after`, `limit`, `order`).
    pub pagination: PaginationQuery<'a>,
    /// A cursor for pagination: return batches created *before* this
    /// batch ID.
    pub before: Option<&'a str>,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

impl Get for ListBatchesRequest<'_> {
    /// Builds the URL for the request.
    ///
    /// `base_url` should be like <https://api.openai.com/v1>
    fn build_url(&self, base_url: &str) -> Result<String, OapiError> {
        let mut url = Url::parse(base_url.trim_end_matches('/')).map_err(OapiError::UrlError)?;
        url.path_segments_mut()
            .map_err(|_| OapiError::UrlCannotBeBase(base_url.to_string()))?
            .push("batches");

        let mut touched = false;
        {
            let mut pairs = url.query_pairs_mut();
            if self.pagination.any_set() {
                self.pagination.append_to(&mut pairs);
                touched = true;
            }
            if let Some(before) = self.before {
                pairs.append_pair("before", before);
                touched = true;
            }
            if let Some(extra_query) = &self.extra_query {
                for (key, value) in extra_query {
                    pairs.append_pair(key, value);
                }
                touched = true;
            }
        }
        if !touched {
            url.set_query(None);
        }

        Ok(url.to_string())
    }
}

impl GetNoStream for ListBatchesRequest<'_> {
    type Response = ListBatchesResponse;
}

/// The response of listing batch jobs.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ListBatchesResponse {
    /// The batches on this page.
    #[serde(default)]
    pub data: Vec<Batch>,
    /// Whether more batches exist after this page.
    #[serde(default)]
    pub has_more: Option<bool>,
    /// The ID of the first batch on the page, for cursor pagination.
    #[serde(default)]
    pub first_id: Option<String>,
    /// The ID of the last batch on the page, for cursor pagination.
    #[serde(default)]
    pub last_id: Option<String>,
    /// The object type (`list`), if the provider sends it.
    #[serde(default)]
    pub object: Option<String>,
}

crate::impl_from_str!(ListBatchesResponse);