use serde::Serialize;
use crate::types::{Snowflake, StageInstance, StagePrivacyLevel};
use super::{Body, BotClient, Response, Result, Transport, route::routes};
routes! {
CREATE_STAGE_INSTANCE = Post "/stage-instances";
GET_STAGE_INSTANCE = Get "/stage-instances/{channel_id}";
MODIFY_STAGE_INSTANCE = Patch "/stage-instances/{channel_id}";
DELETE_STAGE_INSTANCE = Delete "/stage-instances/{channel_id}";
}
#[derive(Debug, Clone, Copy, Serialize)]
pub struct CreateStageInstance<'a> {
pub channel_id: Snowflake,
pub topic: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub privacy_level: Option<StagePrivacyLevel>,
#[serde(skip_serializing_if = "Option::is_none")]
pub send_start_notification: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub guild_scheduled_event_id: Option<Snowflake>,
#[serde(skip)]
pub reason: Option<&'a str>,
}
#[derive(Debug, Clone, Copy, Default, Serialize)]
pub struct ModifyStageInstance<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub topic: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub privacy_level: Option<StagePrivacyLevel>,
#[serde(skip)]
pub reason: Option<&'a str>,
}
impl<T: Transport> BotClient<T> {
pub async fn create_stage_instance(
&self,
stage: &CreateStageInstance<'_>,
) -> Result<Response<StageInstance>> {
let url = self.rest.url(CREATE_STAGE_INSTANCE, &[]);
self.rest
.call(CREATE_STAGE_INSTANCE, url, Body::Json(stage), stage.reason)
.await
}
pub async fn get_stage_instance(
&self,
channel_id: Snowflake,
) -> Result<Response<StageInstance>> {
let url = self.rest.url(GET_STAGE_INSTANCE, &[&channel_id]);
self.rest
.call(GET_STAGE_INSTANCE, url, Body::NONE, None)
.await
}
pub async fn modify_stage_instance(
&self,
channel_id: Snowflake,
stage: &ModifyStageInstance<'_>,
) -> Result<Response<StageInstance>> {
let url = self.rest.url(MODIFY_STAGE_INSTANCE, &[&channel_id]);
self.rest
.call(MODIFY_STAGE_INSTANCE, url, Body::Json(stage), stage.reason)
.await
}
pub async fn delete_stage_instance(
&self,
channel_id: Snowflake,
reason: Option<&str>,
) -> Result<()> {
let url = self.rest.url(DELETE_STAGE_INSTANCE, &[&channel_id]);
self.rest
.call_empty(DELETE_STAGE_INSTANCE, url, Body::NONE, reason)
.await
}
}
#[cfg(test)]
mod tests {
use futures::executor::block_on;
use serde_json::json;
use super::super::{Method::*, testing::MockTransport};
use super::*;
#[test]
fn routes() {
let c = BotClient::new(MockTransport::default(), "t", Snowflake::from_u64(1));
let channel = Snowflake::from_u64(5);
let create = CreateStageInstance {
channel_id: channel,
topic: "Release party",
privacy_level: None,
send_start_notification: Some(true),
guild_scheduled_event_id: None,
reason: None,
};
let modify = ModifyStageInstance {
topic: Some("Q&A"),
..Default::default()
};
block_on(c.create_stage_instance(&create)).unwrap();
block_on(c.get_stage_instance(channel)).unwrap();
block_on(c.modify_stage_instance(channel, &modify)).unwrap();
block_on(c.delete_stage_instance(channel, Some("over"))).unwrap();
let t = c.transport();
assert_eq!(
t.sent(),
[
(Post, "/stage-instances".into()),
(Get, "/stage-instances/5".into()),
(Patch, "/stage-instances/5".into()),
(Delete, "/stage-instances/5".into()),
]
);
assert_eq!(
t.body(0),
json!({ "channel_id": "5", "topic": "Release party", "send_start_notification": true })
);
assert_eq!(t.body(2), json!({ "topic": "Q&A" }));
assert_eq!(t.request(3).header("x-audit-log-reason"), Some("over"));
}
}