openai-interface 0.14.0

A low-level Rust interface for the OpenAI API
Documentation
//! File list request functionality
//!
//! This module provides functionality for listing files via GET requests,
//! demonstrating the use of the GET request traits.

use url::Url;

use crate::{
    errors::OapiError,
    rest::get::{Get, GetNoStream},
};

/// Request parameters for listing files
#[derive(Debug, Clone, Default)]
pub struct ListFilesRequest {
    /// A cursor for use in pagination.
    ///
    /// `after` is an object ID that defines your place
    /// in the list. For instance, if you make a list request and receive 100 objects,
    /// ending with obj_foo, your subsequent call can include after=obj_foo in order to
    /// fetch the next page of the list.
    pub after: Option<String>,
    /// A limit on the number of objects to be returned. Limit can range between 1 and
    /// 10,000, and the default is 10,000.
    pub limit: Option<u32>,
    /// Sort order by the `created_at` timestamp of the objects.
    pub order: Option<ListOrder>,
    /// Only return files with the given purpose.
    pub purpose: Option<String>,
}

/// Sort order by the `created_at` timestamp of the objects. `asc` for ascending
/// order and `desc` for descending order.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ListOrder {
    Asc,
    #[default]
    Desc,
}

impl ListOrder {
    fn as_str(self) -> &'static str {
        match self {
            ListOrder::Asc => "asc",
            ListOrder::Desc => "desc",
        }
    }
}

impl Get for ListFilesRequest {
    #[allow(rustdoc::bare_urls)]
    /// base_url should look 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("files");

        // Only touch the query string when there is something to add:
        // `query_pairs_mut` marks the query as present, which would leave a
        // trailing `?` on the URL even without any parameter.
        if self.purpose.is_some()
            || self.limit.is_some()
            || self.after.is_some()
            || self.order.is_some()
        {
            let mut query_pairs = url.query_pairs_mut();

            if let Some(ref purpose) = self.purpose {
                query_pairs.append_pair("purpose", purpose);
            }

            if let Some(limit) = self.limit {
                query_pairs.append_pair("limit", &limit.to_string());
            }

            if let Some(order) = self.order {
                query_pairs.append_pair("order", order.as_str());
            }

            if let Some(ref after) = self.after {
                query_pairs.append_pair("after", after);
            }
        }

        Ok(url.to_string())
    }
}

impl GetNoStream for ListFilesRequest {
    type Response = super::response::ListFilesResponse;
}