use crate::client::WattpadRequestBuilder;
use crate::field::{PartField, StoryField};
use crate::types::{PartContentResponse, PartResponse, StoryResponse};
use crate::WattpadError;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use bytes::Bytes;
pub struct StoryClient {
pub(crate) http: reqwest::Client,
pub(crate) is_authenticated: Arc<AtomicBool>,
}
impl StoryClient {
pub async fn get_story_info(
&self,
story_id: u64,
fields: Option<&[StoryField]>,
) -> Result<StoryResponse, WattpadError> {
WattpadRequestBuilder::new(
&self.http,
&self.is_authenticated,
reqwest::Method::GET,
&format!("/api/v3/stories/{}", story_id),
)
.fields(fields, None)?
.execute()
.await
}
pub async fn get_part_info(
&self,
part_id: u64,
fields: Option<&[PartField]>,
) -> Result<PartResponse, WattpadError> {
WattpadRequestBuilder::new(
&self.http,
&self.is_authenticated,
reqwest::Method::GET,
&format!("/api/v3/story_parts/{}", part_id),
)
.fields(fields, None)?
.execute()
.await
}
pub async fn get_part_content_raw(&self, part_id: u64) -> Result<String, WattpadError> {
WattpadRequestBuilder::new(
&self.http,
&self.is_authenticated,
reqwest::Method::GET,
"/apiv2/",
)
.param("m", Some("storytext"))
.param("id", Some(part_id))
.execute_raw_text()
.await
}
pub async fn get_part_content_json(
&self,
part_id: u64,
) -> Result<PartContentResponse, WattpadError> {
WattpadRequestBuilder::new(
&self.http,
&self.is_authenticated,
reqwest::Method::GET,
"/apiv2/",
)
.param("m", Some("storytext"))
.param("id", Some(part_id))
.param("output", Some("json"))
.execute()
.await
}
pub async fn get_story_content_zip(&self, story_id: u64) -> Result<Bytes, WattpadError> {
WattpadRequestBuilder::new(
&self.http,
&self.is_authenticated,
reqwest::Method::GET,
"/apiv2/",
)
.param("m", Some("storytext"))
.param("group_id", Some(story_id))
.param("output", Some("zip"))
.execute_bytes()
.await
}
}