scaleway-rs 0.2.7

A pure Rust scaleway API binding.
Documentation
use crate::{
    data::volume::{ScalewayVolume, ScalewayVolumesRoot},
    ScalewayApi, ScalewayError,
};

/// Builder for listing block volumes in a zone.
///
/// Created by [`ScalewayApi::list_volumes`](crate::ScalewayApi::list_volumes).
/// Auto-paginates. Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
pub struct ScalewayListVolumesBuilder {
    api: ScalewayApi,
    zone: String,
    params: Vec<(&'static str, String)>,
}

impl ScalewayListVolumesBuilder {
    pub fn new(api: ScalewayApi, zone: &str) -> Self {
        ScalewayListVolumesBuilder {
            api,
            zone: zone.to_string(),
            params: vec![],
        }
    }

    /// Number of items per page (max 100).
    pub fn per_page(mut self, count: u32) -> Self {
        self.params.push(("per_page", count.to_string()));
        self
    }

    /// Filter by volume type (e.g. "l_ssd", "b_ssd").
    pub fn volume_type(mut self, volume_type: &str) -> Self {
        self.params.push(("volume_type", volume_type.to_string()));
        self
    }

    /// Filter by project ID.
    pub fn project(mut self, project_id: &str) -> Self {
        self.params.push(("project", project_id.to_string()));
        self
    }

    /// Filter by organization ID.
    pub fn organization(mut self, organization_id: &str) -> Self {
        self.params.push(("organization", organization_id.to_string()));
        self
    }

    /// Filter by name.
    pub fn name(mut self, name: &str) -> Self {
        self.params.push(("name", name.to_string()));
        self
    }

    /// Fetches all matching volumes, auto-paginating (blocking).
    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<Vec<ScalewayVolume>, ScalewayError> {
        let mut list = vec![];
        let mut page = 1;
        loop {
            let url = format!(
                "https://api.scaleway.com/instance/v1/zones/{zone}/volumes",
                zone = self.zone
            );
            let mut params = self.params.clone();
            params.push(("page", page.to_string()));
            let result = self
                .api
                .get(&url, params)?
                .json::<ScalewayVolumesRoot>()?;
            if result.volumes.is_empty() {
                break;
            }
            list.extend(result.volumes);
            page += 1;
        }
        Ok(list)
    }

    /// Fetches all matching volumes, auto-paginating.
    pub async fn run_async(self) -> Result<Vec<ScalewayVolume>, ScalewayError> {
        let mut list = vec![];
        let mut page = 1;
        loop {
            let url = format!(
                "https://api.scaleway.com/instance/v1/zones/{zone}/volumes",
                zone = self.zone
            );
            let mut params = self.params.clone();
            params.push(("page", page.to_string()));
            let result = self
                .api
                .get_async(&url, params)
                .await?
                .json::<ScalewayVolumesRoot>()
                .await?;
            if result.volumes.is_empty() {
                break;
            }
            list.extend(result.volumes);
            page += 1;
        }
        Ok(list)
    }
}