pub struct Client<E>{
pub http_client: Client,
pub environment: E,
pub failover_manager: Option<FailoverManager>,
}Expand description
Core HTTP client for XAI API requests. Core HTTP client for XAI API requests. XAI API client.
The main client for interacting with the XAI API. Handles HTTP communication, authentication, and error handling.
§Generic Parameter
E- Environment type implementingXaiEnvironmenttrait
§Examples
use api_xai::{ Client, XaiEnvironmentImpl, Secret };
let secret = Secret::load_with_fallbacks( "XAI_API_KEY" )?;
let env = XaiEnvironmentImpl::new( secret )?;
let client = Client::build( env )?;Fields§
§http_client: ClientHTTP client for making requests.
environment: EEnvironment configuration.
failover_manager: Option<FailoverManager>Failover manager for endpoint rotation (optional).
Implementations§
Source§impl<E> Client<E>
impl<E> Client<E>
Sourcepub fn build(environment: E) -> Result<Self>
pub fn build(environment: E) -> Result<Self>
Builds a new client with the given environment configuration.
§HTTP Client Configuration
The client is configured with resilient network settings:
- Total timeout: From environment (default 30s, tests use 120s)
- Connect timeout: 15s for DNS resolution + TCP + TLS handshake
- Pool idle timeout: 90s to reuse connections efficiently
- TCP keepalive: 60s to detect dead connections
The connect timeout is set to 15s to ensure sufficient time remains for the actual API response in production (30s total - 15s connect = 15s for API). For integration tests with 120s timeout, this leaves 105s for API processing.
These settings prevent timeout issues on slow networks while still catching genuine failures within reasonable time.
§Errors
Returns XaiError::Http if the HTTP client cannot be created.
Sourcepub fn with_failover(self, endpoints: Vec<String>) -> Self
pub fn with_failover(self, endpoints: Vec<String>) -> Self
Adds failover support with multiple endpoints (requires failover feature).
§Panics
Panics if endpoints list is empty.
Sourcepub fn with_failover_config(
self,
endpoints: Vec<String>,
config: FailoverConfig,
) -> Self
pub fn with_failover_config( self, endpoints: Vec<String>, config: FailoverConfig, ) -> Self
Adds failover support with custom configuration (requires failover feature).
§Panics
Panics if endpoints list is empty.
Sourcepub async fn post<I, O>(&self, path: &str, body: &I) -> Result<O>where
I: Serialize,
O: DeserializeOwned,
pub async fn post<I, O>(&self, path: &str, body: &I) -> Result<O>where
I: Serialize,
O: DeserializeOwned,
Makes a POST request to the API.
§Errors
Returns errors for network failures, HTTP errors, or deserialization failures.
Sourcepub async fn get<O>(&self, path: &str) -> Result<O>where
O: DeserializeOwned,
pub async fn get<O>(&self, path: &str) -> Result<O>where
O: DeserializeOwned,
Makes a GET request to the API.
§Errors
Returns errors for network failures, HTTP errors, or deserialization failures.
Trait Implementations§
Auto Trait Implementations§
impl<E> !RefUnwindSafe for Client<E>
impl<E> !UnwindSafe for Client<E>
impl<E> Freeze for Client<E>where
E: Freeze,
impl<E> Send for Client<E>
impl<E> Sync for Client<E>
impl<E> Unpin for Client<E>where
E: Unpin,
impl<E> UnsafeUnpin for Client<E>where
E: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.
Source§type Entry = <C as CollectionValToEntry<Val>>::Entry
type Entry = <C as CollectionValToEntry<Val>>::Entry
Entry is defined by the Collection trait.