use crate::{
data::volume::{ScalewayVolume, ScalewayVolumesRoot},
ScalewayApi, ScalewayError,
};
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![],
}
}
pub fn per_page(mut self, count: u32) -> Self {
self.params.push(("per_page", count.to_string()));
self
}
pub fn volume_type(mut self, volume_type: &str) -> Self {
self.params.push(("volume_type", volume_type.to_string()));
self
}
pub fn project(mut self, project_id: &str) -> Self {
self.params.push(("project", project_id.to_string()));
self
}
pub fn organization(mut self, organization_id: &str) -> Self {
self.params.push(("organization", organization_id.to_string()));
self
}
pub fn name(mut self, name: &str) -> Self {
self.params.push(("name", name.to_string()));
self
}
#[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)
}
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)
}
}