mod models;
pub mod response;
mod validate;
pub use models::*;
pub use response::*;
pub use validate::*;
use crate::{
api::{
compute::{
error::{ComputeError, FormationValidation},
COMPUTE_API_URL,
},
error::map_api_error,
ApiRequest, RequestBuilder,
},
error::Result,
};
const COMPUTE_API_ROUTE: &str = "v2beta/formations";
#[derive(Debug)]
pub struct FormationsRequestBuilder {
builder: RequestBuilder<FormationId>,
}
impl From<RequestBuilder<FormationId>> for FormationsRequestBuilder {
fn from(builder: RequestBuilder<FormationId>) -> Self { Self { builder } }
}
impl Default for FormationsRequestBuilder {
fn default() -> Self { Self::new() }
}
impl FormationsRequestBuilder {
pub fn new() -> Self { RequestBuilder::new(COMPUTE_API_URL, COMPUTE_API_ROUTE).into() }
pub fn build(self) -> Result<FormationsRequest> { Ok(self.builder.build()?.into()) }
#[must_use]
pub fn token<U: Into<String>>(self, token: U) -> Self { self.builder.token(token).into() }
#[cfg(any(feature = "allow_insecure_urls", feature = "danger_zone"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "allow_insecure_urls", feature = "danger_zone"))))]
pub fn allow_http(self, yes: bool) -> Self { self.builder.allow_http(yes).into() }
#[cfg(any(feature = "allow_invalid_certs", feature = "danger_zone"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "allow_invalid_certs", feature = "danger_zone"))))]
pub fn allow_invalid_certs(self, yes: bool) -> Self {
self.builder.allow_invalid_certs(yes).into()
}
#[doc(hidden)]
pub fn base_url<U: AsRef<str>>(self, url: U) -> Self { self.builder.base_url(url).into() }
#[must_use]
pub fn formation_id(self, oid: FormationId) -> Self { self.builder.target(oid).into() }
}
#[derive(Debug)]
pub struct FormationsRequest {
request: ApiRequest<FormationId>,
}
impl From<ApiRequest<FormationId>> for FormationsRequest {
fn from(request: ApiRequest<FormationId>) -> Self { Self { request } }
}
impl FormationsRequest {
pub fn builder() -> FormationsRequestBuilder { FormationsRequestBuilder::new() }
pub fn create(&self, formation: &Formation) -> Result<CreateFormationResponse> {
let req = self
.request
.client
.post(self.request.endpoint_url.clone())
.bearer_auth(&self.request.token)
.json(formation);
let resp = req.send()?;
map_api_error(resp)?
.json::<CreateFormationResponse>()
.map_err(Into::into)
}
pub fn delete(&self) -> Result<DeleteFormationResponse> {
use FormationValidation::*;
if self.request.target.is_none() {
Err(ComputeError::FormationValidation(MissingFormationId))?;
}
let url = self
.request
.endpoint_url
.join(&format!("formations/{}", self.oid()))?;
let resp = self
.request
.client
.delete(url)
.bearer_auth(&self.request.token)
.send()?;
map_api_error(resp)?;
Ok(())
}
pub fn get_all(&self) -> Result<GetFormationsResponse> {
let client = reqwest::blocking::Client::new();
let resp = client
.get(self.request.endpoint_url.clone())
.bearer_auth(&self.request.token)
.send()?;
map_api_error(resp)?
.json::<GetFormationsResponse>()
.map_err(Into::into)
}
pub fn get(&self) -> Result<GetFormationResponse> {
if self.request.target.is_none() {
Err(ComputeError::FormationValidation(FormationValidation::MissingFormationId))?
}
let url = self
.request
.endpoint_url
.join(&format!("formations/{}", self.oid()))?;
let resp = self
.request
.client
.get(url)
.bearer_auth(&self.request.token)
.send()?;
map_api_error(resp)?
.json::<GetFormationResponse>()
.map_err(Into::into)
}
#[inline]
fn oid(&self) -> &FormationId { self.request.target.as_ref().unwrap() }
}