openai-interface 0.13.0

A low-level Rust interface for the OpenAI API
Documentation
//! List the files of a vector store file batch via
//! `GET /vector_stores/{vector_store_id}/file_batches/{batch_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::{PaginationOrder, PaginationQuery},
    rest::get::{Get, GetNoStream},
    vector_stores::VectorStoreFile,
};

/// Lists the files of a vector store file batch.
#[derive(Debug, Clone, Default)]
pub struct ListVectorStoreBatchFilesRequest<'a> {
    /// The ID of the vector store, e.g. `vs_...`.
    pub vector_store_id: &'a str,
    /// The ID of the file batch, e.g. `vsfb_...`.
    pub batch_id: &'a str,
    /// The standard pagination parameters (`before`, `after`, `limit`,
    /// `order`).
    pub pagination: PaginationQuery<'a>,
    /// A metadata filter, as raw JSON.
    pub filter: Option<serde_json::Value>,
    /// Additional query parameters appended verbatim to the URL.
    pub extra_query: Option<std::collections::HashMap<String, String>>,
}

impl Get for ListVectorStoreBatchFilesRequest<'_> {
    /// 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("file_batches")
            .push(self.batch_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(filter) = &self.filter {
                let serialized = serde_json::to_string(filter)
                    .map_err(|e| OapiError::ResponseError(e.to_string()))?;
                pairs.append_pair("filter", &serialized);
                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 ListVectorStoreBatchFilesRequest<'_> {
    type Response = crate::vector_stores::files::ListVectorStoreFilesResponse;
}