ib-update 0.1.0

A lightweight library for software update
Documentation
//! Async HTTP extension.

/// Async HTTP client dispatcher.
pub struct AsyncHttp;

impl AsyncHttp {
    /// Send a GET request, trying each client builder.
    ///
    /// When the `multi-server` feature is enabled, retries on every builder until one succeeds
    /// or all are exhausted (returning the last error). Otherwise only the first builder is used.
    ///
    /// ## Returns
    /// - [`nyquest::Error::InvalidUrl`] when `builders` is empty.
    pub async fn get(
        #[allow(unused_mut)] mut builders: impl Iterator<Item = nyquest::ClientBuilder>,
        path: String,
    ) -> Result<nyquest::r#async::Response, nyquest::Error> {
        cfg_select! {
            feature = "multi-server" => {
                let mut last_err = nyquest::Error::InvalidUrl;
                for builder in builders {
                    match builder
                        .build_async()
                        .await?
                        .request(nyquest::Request::get(path.clone()))
                        .await
                        .and_then(|res| res.with_successful_status())
                    {
                        Ok(res) => return Ok(res),
                        Err(e) => last_err = e,
                    }
                }
                Err(last_err)
            }
            _ => {
                let client = unsafe { builders.next().unwrap_unchecked() }
                    .build_async()
                    .await?;
                client.request(nyquest::Request::get(path)).await
            }
        }
    }
}