use crate::internal::path_escape;
use crate::runtime::{self, Error};
#[derive(Clone, Debug, Default)]
pub struct ShieldInformationBarriersListOptions {
pub marker: Option<String>,
pub limit: Option<i64>,
}
pub struct ShieldInformationBarriersListPaginator {
manager: ShieldInformationBarriersManager,
options: ShieldInformationBarriersListOptions,
buffer: std::vec::IntoIter<crate::models::schemas::ShieldInformationBarrier>,
done: bool,
}
impl ShieldInformationBarriersListPaginator {
pub async fn next(
&mut self,
) -> Option<Result<crate::models::schemas::ShieldInformationBarrier, Error>> {
loop {
if let Some(item) = self.buffer.next() {
return Some(Ok(item));
}
if self.done {
return None;
}
let page = match self.manager.list_page(Some(self.options.clone())).await {
Ok(page) => page,
Err(err) => {
self.done = true;
return Some(Err(err));
}
};
self.buffer = page.entries.unwrap_or_default().into_iter();
match page.next_marker.flatten() {
Some(cursor) if !cursor.is_empty() => self.options.marker = Some(cursor),
_ => self.done = true,
}
}
}
}
pub struct ShieldInformationBarriersManager {
session: std::sync::Arc<runtime::Client>,
}
impl ShieldInformationBarriersManager {
pub(crate) fn new(session: std::sync::Arc<runtime::Client>) -> Self {
Self { session }
}
pub async fn get(
&self,
shield_information_barrier_id: String,
) -> Result<crate::models::schemas::ShieldInformationBarrier, Error> {
let mut url = self.session.base_url("api");
url.push_str("/shield_information_barriers");
url.push('/');
let seg = path_escape(&shield_information_barrier_id);
url.push_str(&seg);
let req = self.session.new_request("GET", &url);
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
pub async fn create_change_status(
&self,
body: crate::models::schemas::ChangeStatusCreateRequest,
) -> Result<crate::models::schemas::ShieldInformationBarrier, Error> {
let mut url = self.session.base_url("api");
url.push_str("/shield_information_barriers");
url.push_str("/change_status");
let mut req = self.session.new_request("POST", &url);
let payload = serde_json::to_vec(&body)?;
req = runtime::with_json_body(req, &payload);
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
async fn list_page(
&self,
opts: Option<ShieldInformationBarriersListOptions>,
) -> Result<crate::models::schemas::ShieldInformationBarriers, Error> {
let mut url = self.session.base_url("api");
url.push_str("/shield_information_barriers");
let mut req = self.session.new_request("GET", &url);
let opts = opts.unwrap_or_default();
if let Some(value) = opts.marker {
req = runtime::with_query(req, "marker", &value);
}
if let Some(value) = opts.limit {
req = runtime::with_query(req, "limit", &value.to_string());
}
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
pub fn list(
&self,
opts: Option<ShieldInformationBarriersListOptions>,
) -> ShieldInformationBarriersListPaginator {
ShieldInformationBarriersListPaginator {
manager: ShieldInformationBarriersManager::new(self.session.clone()),
options: opts.unwrap_or_default(),
buffer: Vec::new().into_iter(),
done: false,
}
}
pub async fn create(
&self,
body: crate::models::schemas::ShieldInformationBarrierCreateRequest,
) -> Result<crate::models::schemas::ShieldInformationBarrier, Error> {
let mut url = self.session.base_url("api");
url.push_str("/shield_information_barriers");
let mut req = self.session.new_request("POST", &url);
let payload = serde_json::to_vec(&body)?;
req = runtime::with_json_body(req, &payload);
let resp = self.session.fetch(req).await?;
let data = runtime::response_bytes(&resp)?;
Ok(serde_json::from_slice(&data)?)
}
}