async_openai/
traits.rs

1use reqwest::header::HeaderMap;
2
3use crate::{error::OpenAIError, RequestOptions};
4use serde::Serialize;
5
6pub trait AsyncTryFrom<T>: Sized {
7    /// The type returned in the event of a conversion error.
8    type Error;
9
10    /// Performs the conversion.
11    fn try_from(value: T) -> impl std::future::Future<Output = Result<Self, Self::Error>> + Send;
12}
13
14/// Trait for events to get their event type string.
15pub trait EventType {
16    /// Returns the event type string (e.g., "batch.cancelled")
17    fn event_type(&self) -> &'static str;
18}
19
20/// Trait for events to get their event ID.
21pub trait EventId {
22    /// Returns the event ID
23    fn event_id(&self) -> &str;
24}
25
26/// Trait for types that can build RequestOptions through fluent API
27pub trait RequestOptionsBuilder: Sized {
28    /// Get mutable reference to RequestOptions (for building)
29    fn options_mut(&mut self) -> &mut RequestOptions;
30
31    /// Get reference to RequestOptions
32    fn options(&self) -> &RequestOptions;
33
34    /// Add headers to RequestOptions
35    fn headers(mut self, headers: HeaderMap) -> Self {
36        self.options_mut().with_headers(headers);
37        self
38    }
39
40    /// Add a single header to RequestOptions
41    fn header<K, V>(mut self, key: K, value: V) -> Result<Self, OpenAIError>
42    where
43        K: reqwest::header::IntoHeaderName,
44        V: TryInto<reqwest::header::HeaderValue>,
45        V::Error: Into<reqwest::header::InvalidHeaderValue>,
46    {
47        self.options_mut().with_header(key, value)?;
48        Ok(self)
49    }
50
51    /// Add query parameters to RequestOptions
52    fn query<Q: Serialize + ?Sized>(mut self, query: &Q) -> Result<Self, OpenAIError> {
53        self.options_mut().with_query(query)?;
54        Ok(self)
55    }
56
57    /// Add a path to RequestOptions
58    fn path<P: Into<String>>(mut self, path: P) -> Result<Self, OpenAIError> {
59        self.options_mut().with_path(path.into().as_str())?;
60        Ok(self)
61    }
62}