pub struct Client { /* private fields */ }Expand description
Client for interacting with the Gradium API.
The client handles authentication and WebSocket connection management.
Implementations§
Source§impl Client
impl Client
Sourcepub fn from_location(api_key: &str, location: Location) -> Self
pub fn from_location(api_key: &str, location: Location) -> Self
Creates a new client for the specified Gradium API location.
§Arguments
api_key- Your Gradium API keylocation- The API location (EU or US)
Sourcepub fn with_additional_header(self, key: &str, value: &str) -> Self
pub fn with_additional_header(self, key: &str, value: &str) -> Self
Adds an additional HTTP header to be sent with each request (builder pattern).
Sourcepub fn from_env(
base_url: Option<String>,
api_key: Option<String>,
) -> Result<Self>
pub fn from_env( base_url: Option<String>, api_key: Option<String>, ) -> Result<Self>
Creates a new client from environment variables.
Uses GRADIUM_API_KEY and GRADIUM_BASE_URL environment variables if
the corresponding parameters are None.
§Arguments
base_url- Optional base URL override. IfNone, usesGRADIUM_BASE_URLenv var or defaultapi_key- Optional API key override. IfNone, usesGRADIUM_API_KEYenv var
§Returns
A configured Client instance
§Errors
Returns an error if:
- API key is not provided and
GRADIUM_API_KEYis not set - Base URL parsing fails
§Example
use gradium::Client;
// Uses environment variables
let client = Client::from_env(None, None)?;
// Override API key
let client = Client::from_env(None, Some("my-key".to_string()))?;Sourcepub fn with_api_key(self, api_key: &str) -> Self
pub fn with_api_key(self, api_key: &str) -> Self
Sourcepub fn with_server_addr(self, server_addr: &str) -> Self
pub fn with_server_addr(self, server_addr: &str) -> Self
Sourcepub fn with_https(self, use_https: bool) -> Self
pub fn with_https(self, use_https: bool) -> Self
Sourcepub fn with_base_url(self, base_url: &str) -> Result<Self>
pub fn with_base_url(self, base_url: &str) -> Result<Self>
Sets the server configuration from a complete base URL (builder pattern).
Parses the URL to extract the server address, port, scheme, and path.
§Arguments
base_url- Complete base URL (e.g., “https://eu.api.gradium.ai/api”)
§Returns
The updated client on success
§Errors
Returns an error if the URL cannot be parsed
§Example
use gradium::Client;
let client = Client::new("api-key")
.with_base_url("https://custom.gradium.ai:8443/v2")?;pub fn http_url(&self, endpoint: &str) -> String
Sourcepub async fn ws_connect(&self, endpoint: &str) -> Result<WebSocket>
pub async fn ws_connect(&self, endpoint: &str) -> Result<WebSocket>
Establishes a WebSocket connection to the specified endpoint.
This method handles authentication by adding the API key to the request headers.
§Arguments
endpoint- The API endpoint to connect to (e.g., “speech/tts”)
§Returns
A WebSocket connection on success
§Errors
Returns an error if the connection fails or if the API key is invalid
Sourcepub async fn tts(&self, text: &str, setup: Setup) -> Result<TtsResult>
pub async fn tts(&self, text: &str, setup: Setup) -> Result<TtsResult>
Performs a one-shot text-to-speech conversion.
This is a convenience method that delegates to crate::tts::tts.
§Arguments
text- The text to synthesizesetup- TTS configuration
§Returns
A TtsResult containing the complete audio data
§Errors
Returns an error if the TTS operation fails
Sourcepub async fn tts_stream(&self, setup: Setup) -> Result<TtsStream>
pub async fn tts_stream(&self, setup: Setup) -> Result<TtsStream>
Creates a new TTS stream for real-time text-to-speech.
This is a convenience method that delegates to crate::tts::tts_stream.
§Arguments
setup- TTS configuration
§Returns
A TtsStream for streaming TTS operations
§Errors
Returns an error if the stream cannot be created
Sourcepub async fn tts_multiplex(&self) -> Result<TtsMultiplexStream>
pub async fn tts_multiplex(&self) -> Result<TtsMultiplexStream>
Opens a multiplexing TTS WebSocket connection.
Returns a TtsMultiplexStream that allows sending multiple independent
TTS requests over a single WebSocket, tracked by client_req_id.
§Errors
Returns an error if the WebSocket connection fails
Sourcepub async fn stt(&self, audio: Vec<u8>, setup: Setup) -> Result<SttResult>
pub async fn stt(&self, audio: Vec<u8>, setup: Setup) -> Result<SttResult>
Performs a one-shot speech-to-text transcription.
This is a convenience method that delegates to crate::stt::stt.
§Arguments
audio- Raw audio data to transcribesetup- STT configuration
§Returns
An SttResult containing the transcription
§Errors
Returns an error if the STT operation fails
Sourcepub async fn stt_stream(&self, setup: Setup) -> Result<SttStream>
pub async fn stt_stream(&self, setup: Setup) -> Result<SttStream>
Creates a new STT stream for real-time speech recognition.
This is a convenience method that delegates to crate::stt::stt_stream.
§Arguments
setup- STT configuration
§Returns
An SttStream for streaming STT operations
§Errors
Returns an error if the stream cannot be created