pub struct Client(/* private fields */);Expand description
A cloneable, non-owning capability for requests to one Session.
Clones refer to the same session and may submit requests concurrently. They
cannot receive updates or initiate graceful closure. Keeping a client alive
does not keep the session operational: requests after owner loss or closure
admission fail with Error::Disconnected.
Methods are asynchronous and do not submit until their futures are polled.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn send<F: Function>(&self, request: &F) -> Result<F::Return>
pub async fn send<F: Function>(&self, request: &F) -> Result<F::Return>
Sends a generated function and returns its direct TDLib response.
F::Return is supplied by td-types::traits::Function. This is the general
entry point for the generated API, including getters and message edits.
For normal sends, the direct response can still describe a pending temporary
message; use track or track_all to
wait for terminal outcomes.
§Errors
Returns Error::Json for serialization/decoding failures, Error::Td
for TDLib error responses, and Error::Disconnected when the session
cannot accept the request or its reply is abandoned during teardown.
§Cancellation
Dropping this future stops waiting, not the submitted native request. A timeout therefore does not establish that a side effect failed to occur. Retries and native cancellation are application decisions.
§Examples
use td_types::{enums::User, fns};
let User::user(user) = client.send(&fns::getMe {}).await?;
println!("{}", user.first_name);Source§impl Client
impl Client
Sourcepub async fn track<F: Function<Return = Message>>(
&self,
request: &F,
cancel: Option<&CancellationToken>,
progress: Option<&mut (dyn FnMut(usize, Progress) + Send)>,
) -> Result<message>
pub async fn track<F: Function<Return = Message>>( &self, request: &F, cancel: Option<&CancellationToken>, progress: Option<&mut (dyn FnMut(usize, Progress) + Send)>, ) -> Result<message>
Sends one normal message and waits for its terminal result.
Accepts a generated function returning td-types::enums::Message, subject
to the normal-send contract. The result
is the final concrete message, not merely the initial pending response.
Internal tracking does not depend on the application receiving updates.
progress receives (0, measurement); it runs synchronously on the task
polling this future and must not block or panic. See Progress for
coalescing and unknown totals.
§Errors
Returns direct-request errors, Error::MessageFailed on terminal send
failure, or Error::MessageDeleted on non-cache deletion. Token-driven
deletion maps to Error::Cancelled. Native deletion failures are returned
while the message remains pending; terminal outcomes or teardown can win
during deletion. Teardown can produce Error::Disconnected; an unexpected
direct response produces
Error::UnexpectedResponse or Error::Json.
§Cancellation
See the cancellation contract. Dropping this future performs no native cancellation and does not undo a sent message.
§Examples
use td_types::{fns, types};
let text = types::formattedText { text: "Hello!".into(), ..Default::default() };
let content = types::inputMessageText { text, ..Default::default() };
let request = fns::sendMessage {
chat_id,
input_message_content: content.into(),
..Default::default()
};
let message = client.track(&request, None, None).await?;
println!("Sent message {}", message.id);Sourcepub async fn track_all<F: Function<Return = Messages>>(
&self,
request: &F,
cancel: Option<&CancellationToken>,
progress: Option<&mut (dyn FnMut(usize, Progress) + Send)>,
) -> Result<Vec<Result<message>>>
pub async fn track_all<F: Function<Return = Messages>>( &self, request: &F, cancel: Option<&CancellationToken>, progress: Option<&mut (dyn FnMut(usize, Progress) + Send)>, ) -> Result<Vec<Result<message>>>
Sends a normal-message batch and returns individual terminal results.
Accepts a generated function returning td-types::enums::Messages, subject
to the normal-send contract. It is not
restricted to albums. Results retain direct-response order, not completion
order; an empty batch returns an empty vector.
The outer result describes submission and direct-response handling. Once bound, each message has its own result: one terminal failure does not erase successful messages elsewhere in the batch.
progress receives the zero-based direct-response item index and a
measurement. One shared observation channel coalesces samples across items;
intermediate callbacks for every item are not guaranteed.
§Errors
The outer result reports direct-request, decoding, and pre-binding
disconnection errors. Each inner result has the terminal/cancellation errors
documented on track.
§Cancellation
One token applies to the whole batch, not one item. Pending items are awaited and, when requested, cancelled sequentially in response order; later items may finish before their cancellation is attempted. Successful results remain successful. There is no all-or-nothing send or rollback guarantee.
Dropping this future abandons observation of the whole batch without native cancellation. See the shared cancellation contract.
§Examples
Handle partial failure instead of assuming the outer Ok means every send
succeeded:
let results = client.track_all(request, None, None).await?;
for (index, result) in results.into_iter().enumerate() {
match result {
Ok(message) => println!("Item {index}: message {}", message.id),
Err(error) => eprintln!("Item {index}: {error}"),
}
}Source§impl Client
impl Client
Sourcepub async fn download(
&self,
request: &downloadFile,
cancel: Option<&CancellationToken>,
progress: Option<&mut (dyn FnMut(usize, Progress) + Send)>,
) -> Result<file>
pub async fn download( &self, request: &downloadFile, cancel: Option<&CancellationToken>, progress: Option<&mut (dyn FnMut(usize, Progress) + Send)>, ) -> Result<file>
Downloads a file or byte range and returns TDLib’s final file state.
The caller supplies the generated downloadFile request, including its
priority and range. synchronous must be true: TDLib retains the response
until the request finishes. This does not block the Rust task’s thread.
offset and limit must be valid nonnegative TDLib arguments whose range
arithmetic fits i64. The method does not repair invalid ranges. See the
range contract. With a callback, progress
uses item index zero; no callback is required for successful cached work.
§Errors
Returns direct-request errors as described on Client::send. When native
cancellation succeeds and the download returns a TDLib error, that error is
reported as Error::Cancelled. This is a cancellation interpretation,
not proof that no other native failure raced with cancellation. A successful
download response wins even if cancellation was also requested.
Cancellation-request failures are returned when no successful download won.
§Panics
Panics when request.synchronous is false. Caller callbacks must not panic.
§Cancellation
TDLib cancellation affects the entire file, including concurrent requests.
Dropping this future does not invoke it. A pre-cancelled token does not
guarantee the download was never submitted, and cached success may win.
Keep the future driven while token-triggered cleanup runs.
§Examples
use td_types::fns;
let request = fns::downloadFile {
file_id, priority: 1, offset: 0, limit: 0, synchronous: true,
};
let mut observe = |_: usize, progress: Progress| {
println!("Available: {} bytes", progress.current);
};
let file = client.download(&request, None, Some(&mut observe)).await?;
println!("Local path: {}", file.local.path);