Skip to main content

AirLibrary/Client/AirClient/
DownloadStreamRpc.rs

1//! `AirClient::DownloadStream` - initiates a streaming download from the
2//! Air daemon's `DownloaderService` and returns a [`DownloadStream::Struct`]
3//! that yields chunks via `.next().await`.
4//!
5//! Unlike [`AirClient::DownloadFile`], the gRPC call returns immediately
6//! after the server accepts the request; bytes flow as
7//! [`DownloadStreamChunk::Struct`] items the caller pumps until
8//! `chunk.completed == true`. Suitable for large files where the caller
9//! wants to surface progress or stream into a sink without an intermediate
10//! `Vec<u8>` buffer.
11
12use std::collections::HashMap;
13
14use tonic::Request;
15
16use crate::{
17	AirError,
18	Client::AirClient::{AirClient, DownloadStream},
19	Vine::Generated::air::DownloadStreamRequest,
20	dev_log,
21};
22
23impl AirClient {
24	/// Starts a streaming download.
25	///
26	/// # Arguments
27	///
28	/// - `request_id` - opaque correlation id.
29	/// - `url` - HTTPS URL of the file.
30	/// - `headers` - extra HTTP headers (e.g. `"Authorization"`).
31	pub async fn DownloadStream(
32		&self,
33
34		request_id:String,
35
36		url:String,
37
38		headers:HashMap<String, String>,
39	) -> Result<DownloadStream::Struct, AirError> {
40		dev_log!("grpc", "[AirClient] Starting stream download from: {}", url);
41
42		let RequestPayload = DownloadStreamRequest { request_id, url, headers };
43
44		let Client = self
45			.Client()
46			.ok_or_else(|| AirError::Network("Air client not initialized".to_string()))?;
47
48		let mut ClientGuard = Client.lock().await;
49
50		match ClientGuard.download_stream(Request::new(RequestPayload)).await {
51			Ok(Response) => {
52				dev_log!("grpc", "[AirClient] Stream download initiated successfully");
53
54				Ok(DownloadStream::Struct::new(Response.into_inner()))
55			},
56
57			Err(Status) => {
58				dev_log!("grpc", "error: [AirClient] Download stream RPC error: {}", Status);
59
60				Err(AirError::Network(format!("Download stream RPC error: {}", Status)))
61			},
62		}
63	}
64}