scaleway-rs 0.2.4

A pure Rust scaleway API binding.
Documentation
use crate::{
    data::flexible_ip::{ScalewayFlexibleIp, ScalewayFlexibleIpsRoot},
    ScalewayApi, ScalewayError,
};

pub struct ScalewayListFlexibleIpsBuilder {
    api: ScalewayApi,
    zone: String,
    params: Vec<(&'static str, String)>,
}

impl ScalewayListFlexibleIpsBuilder {
    pub fn new(api: ScalewayApi, zone: &str) -> Self {
        ScalewayListFlexibleIpsBuilder {
            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 project ID.
    pub fn project(mut self, project: &str) -> Self {
        self.params.push(("project", project.to_string()));
        self
    }

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

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

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

    /// Filter by IP type (e.g. "routed_ipv4", "nat").
    pub fn ip_type(mut self, ip_type: &str) -> Self {
        self.params.push(("type", ip_type.to_string()));
        self
    }

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

    pub async fn run_async(self) -> Result<Vec<ScalewayFlexibleIp>, ScalewayError> {
        let mut list = vec![];
        let mut page = 1;
        loop {
            let url = &format!(
                "https://api.scaleway.com/instance/v1/zones/{zone}/ips",
                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::<ScalewayFlexibleIpsRoot>()
                .await?;
            if result.ips.is_empty() {
                break;
            }
            list.extend(result.ips.into_iter());
            page += 1;
        }
        Ok(list)
    }
}