openai-interface 0.11.0

A low-level Rust interface for the OpenAI API
Documentation
//! List the files of a vector store via
//! `GET /vector_stores/{vector_store_id}/files`.
//!
//! > ![warn] This module is untested!
//! > If you encounter any issues, please report them on the repository.

use url::Url;

use crate::{
    errors::OapiError,
    pagination::PaginationQuery,
    rest::get::{Get, GetNoStream},
    vector_stores::{VectorStoreFile, VectorStoreFileStatus},
};

/// Lists the files of a vector store.
#[derive(Debug, Clone, Default)]
pub struct ListVectorStoreFilesRequest<'a> {
    /// The ID of the vector store, e.g. `vs_...`.
    pub vector_store_id: &'a str,
    /// The standard pagination parameters (`before`, `after`, `limit`,
    /// `order`).
    pub pagination: PaginationQuery<'a>,
    /// Only return files with this processing status.
    pub status: Option<VectorStoreFileStatus>,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

impl ListVectorStoreFilesRequest<'_> {
    fn status_str(status: VectorStoreFileStatus) -> &'static str {
        match status {
            VectorStoreFileStatus::InProgress => "in_progress",
            VectorStoreFileStatus::Completed => "completed",
            VectorStoreFileStatus::Failed => "failed",
            VectorStoreFileStatus::Cancelled => "cancelled",
        }
    }
}

impl Get for ListVectorStoreFilesRequest<'_> {
    /// 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("vector_stores")
            .push(self.vector_store_id)
            .push("files");

        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(status) = self.status {
                pairs.append_pair("status", Self::status_str(status));
                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 ListVectorStoreFilesRequest<'_> {
    type Response = ListVectorStoreFilesResponse;
}

/// The response of listing a store's files.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ListVectorStoreFilesResponse {
    /// The file associations on this page.
    #[serde(default)]
    pub data: Vec<VectorStoreFile>,
    /// Whether more files exist after this page.
    #[serde(default)]
    pub has_more: Option<bool>,
    /// The ID of the first file on the page, for cursor pagination.
    #[serde(default)]
    pub first_id: Option<String>,
    /// The ID of the last file 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!(ListVectorStoreFilesResponse);