use crate::{Client, PageMeta, TwilioError};
use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use super::environments::{Environment, Environments};
#[allow(dead_code)]
#[derive(Deserialize)]
pub struct ServerlessServicePage {
services: Vec<ServerlessService>,
meta: PageMeta,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ServerlessService {
pub sid: String,
pub account_sid: String,
pub unique_name: String,
pub friendly_name: String,
pub include_credentials: bool,
pub ui_editable: bool,
pub domain_base: String,
pub date_created: String,
pub date_updated: String,
pub url: String,
pub links: Links,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct Links {
pub environments: String,
pub functions: String,
pub assets: String,
pub builds: String,
}
#[skip_serializing_none]
#[derive(Serialize)]
#[serde(rename_all(serialize = "PascalCase"))]
pub struct CreateOrUpdateParams {
pub unique_name: String,
pub friendly_name: String,
pub include_credentials: Option<bool>,
pub ui_editable: Option<bool>,
}
pub struct Services<'a> {
pub client: &'a Client,
}
impl Services<'_> {
pub async fn create(
&self,
params: CreateOrUpdateParams,
) -> Result<ServerlessService, TwilioError> {
self.client
.send_request::<ServerlessService, CreateOrUpdateParams>(
Method::POST,
"https://serverless.twilio.com/v1/Services",
Some(¶ms),
None,
)
.await
}
pub async fn list(&self) -> Result<Vec<ServerlessService>, TwilioError> {
let mut services_page = self
.client
.send_request::<ServerlessServicePage, ()>(
Method::GET,
"https://serverless.twilio.com/v1/Services?PageSize=20",
None,
None,
)
.await?;
let mut results: Vec<ServerlessService> = services_page.services;
while (services_page.meta.next_page_url).is_some() {
services_page = self
.client
.send_request::<ServerlessServicePage, ()>(
Method::GET,
&services_page.meta.next_page_url.unwrap(),
None,
None,
)
.await?;
results.append(&mut services_page.services);
}
Ok(results)
}
}
pub struct Service<'a, 'b> {
pub client: &'a Client,
pub sid: &'b str,
}
impl<'b> Service<'_, 'b> {
pub async fn get(&self) -> Result<ServerlessService, TwilioError> {
self.client
.send_request::<ServerlessService, ()>(
Method::GET,
&format!("https://serverless.twilio.com/v1/Services/{}", self.sid),
None,
None,
)
.await
}
pub async fn update(
&self,
params: CreateOrUpdateParams,
) -> Result<ServerlessService, TwilioError> {
self.client
.send_request::<ServerlessService, CreateOrUpdateParams>(
Method::POST,
&format!("https://serverless.twilio.com/v1/Services/{}", self.sid),
Some(¶ms),
None,
)
.await
}
pub async fn delete(&self) -> Result<(), TwilioError> {
self.client
.send_request_and_ignore_response::<()>(
Method::DELETE,
&format!("https://serverless.twilio.com/v1/Services/{}", self.sid),
None,
None,
)
.await
}
pub fn environment(&self, sid: &'b str) -> Environment {
Environment {
client: self.client,
service_sid: self.sid,
sid,
}
}
pub fn environments(&self) -> Environments {
Environments {
client: self.client,
service_sid: self.sid,
}
}
}