1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
mod private
{
use crate::environment::XaiEnvironment;
use crate::client::Client;
use crate::chat::Chat;
use crate::models::Models;
/// Trait providing convenient API accessors for the client.
///
/// This trait adds methods like `chat()` and `models()` to the `Client` type,
/// providing a fluent interface for accessing different API endpoints.
///
/// # Examples
///
/// ```no_run
/// use api_xai::{ Client, XaiEnvironmentImpl, Secret, ClientApiAccessors, Message };
///
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
/// let env = XaiEnvironmentImpl::new( secret )?;
/// let client = Client::build( env )?;
///
/// // Using the chat accessor
/// let request = api_xai::ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "Hello!" ) ] )
/// .form();
/// let response = client.chat().create( request ).await?;
///
/// // Using the models accessor
/// let models = client.models().list().await?;
/// # Ok( () )
/// # }
/// ```
pub trait ClientApiAccessors< E >
where
E : XaiEnvironment + Send + Sync + 'static,
{
/// Returns a chat completions API accessor.
///
/// Provides access to the `/v1/chat/completions` endpoint.
///
/// # Examples
///
/// ```no_run
/// # use api_xai::{ Client, XaiEnvironmentImpl, Secret, ClientApiAccessors };
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// # let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
/// # let env = XaiEnvironmentImpl::new( secret )?;
/// # let client = Client::build( env )?;
/// let chat_accessor = client.chat();
/// # Ok( () )
/// # }
/// ```
fn chat( &self ) -> Chat< '_, E >;
/// Returns a models API accessor.
///
/// Provides access to the `/v1/models` endpoint.
///
/// # Examples
///
/// ```no_run
/// # use api_xai::{ Client, XaiEnvironmentImpl, Secret, ClientApiAccessors };
/// # async fn example() -> Result< (), Box< dyn std::error::Error > > {
/// # let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
/// # let env = XaiEnvironmentImpl::new( secret )?;
/// # let client = Client::build( env )?;
/// let models_accessor = client.models();
/// # Ok( () )
/// # }
/// ```
fn models( &self ) -> Models< '_, E >;
}
impl< E > ClientApiAccessors< E > for Client< E >
where
E : XaiEnvironment + Send + Sync + 'static,
{
fn chat( &self ) -> Chat< '_, E >
{
Chat::new( self )
}
fn models( &self ) -> Models< '_, E >
{
Models::new( self )
}
}
}
crate::mod_interface!
{
exposed use
{
ClientApiAccessors,
};
}