Skip to main content

Client

Struct Client 

Source
pub struct Client<E>
where E: XaiEnvironment + Send + Sync + 'static,
{ 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 implementing XaiEnvironment trait

§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: Client

HTTP client for making requests.

§environment: E

Environment configuration.

§failover_manager: Option<FailoverManager>

Failover manager for endpoint rotation (optional).

Implementations§

Source§

impl<E> Client<E>
where E: XaiEnvironment + Send + Sync + 'static,

Source

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.

Source

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.

Source

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.

Source

pub async fn post<I, O>(&self, path: &str, body: &I) -> Result<O>

Makes a POST request to the API.

§Errors

Returns errors for network failures, HTTP errors, or deserialization failures.

Source

pub async fn get<O>(&self, path: &str) -> Result<O>

Makes a GET request to the API.

§Errors

Returns errors for network failures, HTTP errors, or deserialization failures.

Source

pub async fn post_stream<I, O>( &self, path: &str, body: &I, ) -> Result<Pin<Box<dyn Stream<Item = Result<O>> + Send + 'static>>>
where I: Serialize, O: DeserializeOwned + Send + 'static,

Makes a streaming POST request to the API (requires streaming feature).

§Errors

Returns errors for network failures, HTTP errors, or SSE parsing failures.

Trait Implementations§

Source§

impl<E> ClientApiAccessors<E> for Client<E>
where E: XaiEnvironment + Send + Sync + 'static,

Source§

fn chat(&self) -> Chat<'_, E>

Returns a chat completions API accessor. Read more
Source§

fn models(&self) -> Models<'_, E>

Returns a models API accessor. Read more
Source§

impl<E> Clone for Client<E>
where E: XaiEnvironment + Send + Sync + 'static + Clone,

Source§

fn clone(&self) -> Client<E>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E> Debug for Client<E>
where E: XaiEnvironment + Send + Sync + 'static + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<C, E> EntryToVal<C> for E
where C: Collection<Entry = E>,

Source§

type Val = <C as Collection>::Val

The type of values stored in the collection. This might be distinct from 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

Converts an entry into a value representation specific to the type of collection. This conversion is crucial for handling operations on entries, especially when they need to be treated or accessed as individual values, such as retrieving the value part from a key-value pair in a hash map.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<C, Val> ValToEntry<C> for Val
where C: CollectionValToEntry<Val>,

Source§

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

Represents the type of entry that corresponds to the value within the collection. Type Entry is defined by the Collection trait.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more