scaleway-rs 0.2.0

A pure Rust scaleway API binding.
Documentation
use crate::data::security_group::{ScalewaySecurityGroupDetails, ScalewaySecurityGroupDetailsListRoot};
use crate::ScalewayApi;
use crate::ScalewayError;

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

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

    pub fn per_page(mut self, per_page: u32) -> Self {
        self.params.push(("per_page", per_page.to_string()));
        self
    }

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

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

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

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

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

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