pub struct HttpClient { /* private fields */ }Expand description
Internal HTTP client that handles requests with authentication and retries
Implementations§
Source§impl HttpClient
impl HttpClient
pub fn new(config: ClientConfig) -> Result<Self, ApiError>
Sourcepub async fn execute_request<T>(
&self,
method: Method,
path: &str,
body: Option<Value>,
query_params: Option<Vec<(String, String)>>,
options: Option<RequestOptions>,
) -> Result<T, ApiError>where
T: DeserializeOwned,
pub async fn execute_request<T>(
&self,
method: Method,
path: &str,
body: Option<Value>,
query_params: Option<Vec<(String, String)>>,
options: Option<RequestOptions>,
) -> Result<T, ApiError>where
T: DeserializeOwned,
Execute a request with the given method, path, and options
Sourcepub async fn execute_stream_request(
&self,
method: Method,
path: &str,
body: Option<Value>,
query_params: Option<Vec<(String, String)>>,
options: Option<RequestOptions>,
) -> Result<ByteStream, ApiError>
pub async fn execute_stream_request( &self, method: Method, path: &str, body: Option<Value>, query_params: Option<Vec<(String, String)>>, options: Option<RequestOptions>, ) -> Result<ByteStream, ApiError>
Execute a request and return a streaming response (for large file downloads)
This method returns a ByteStream that can be used to download large files
efficiently without loading the entire content into memory. The stream can be
consumed chunk by chunk, written directly to disk, or collected into bytes.
§Examples
Option 1: Collect all bytes into memory
let stream = client.execute_stream_request(
Method::GET,
"/file",
None,
None,
None,
).await?;
let bytes = stream.collect().await?;Option 2: Process chunks with try_next()
let mut stream = client.execute_stream_request(
Method::GET,
"/large-file",
None,
None,
None,
).await?;
while let Some(chunk) = stream.try_next().await? {
process_chunk(&chunk);
}Option 3: Stream with futures::Stream trait
use futures::StreamExt;
let stream = client.execute_stream_request(
Method::GET,
"/large-file",
None,
None,
None,
).await?;
let mut file = tokio::fs::File::create("output.mp4").await?;
let mut stream = std::pin::pin!(stream);
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
tokio::io::AsyncWriteExt::write_all(&mut file, &chunk).await?;
}Sourcepub async fn execute_sse_request<T>(
&self,
method: Method,
path: &str,
body: Option<Value>,
query_params: Option<Vec<(String, String)>>,
options: Option<RequestOptions>,
terminator: Option<String>,
) -> Result<SseStream<T>, ApiError>where
T: DeserializeOwned + Send + 'static,
pub async fn execute_sse_request<T>(
&self,
method: Method,
path: &str,
body: Option<Value>,
query_params: Option<Vec<(String, String)>>,
options: Option<RequestOptions>,
terminator: Option<String>,
) -> Result<SseStream<T>, ApiError>where
T: DeserializeOwned + Send + 'static,
Execute a request and return an SSE stream
This method returns an SseStream<T> that automatically parses
Server-Sent Events and deserializes the JSON data in each event.
§SSE-Specific Headers
This method automatically sets the following headers after applying custom headers, which means these headers will override any user-supplied values:
Accept: text/event-stream- Required for SSE protocolCache-Control: no-store- Prevents caching of streaming responses
This ensures proper SSE behavior even if custom headers are provided.
§Example
use futures::StreamExt;
let stream = client.execute_sse_request::<CompletionChunk>(
Method::POST,
"/stream",
Some(serde_json::json!({"query": "Hello"})),
None,
None,
Some("[[DONE]]".to_string()),
).await?;
let mut stream = std::pin::pin!(stream);
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
println!("Received: {:?}", chunk);
}Trait Implementations§
Source§impl Clone for HttpClient
impl Clone for HttpClient
Source§fn clone(&self) -> HttpClient
fn clone(&self) -> HttpClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more