notionrs/client/file_upload/
create_file_upload.rs

1#[derive(Debug, Default, notionrs_macro::Setter)]
2pub struct CreateFileUploadClient {
3    pub(crate) reqwest_client: reqwest::Client,
4
5    pub(crate) mode: FileUplpadMode,
6
7    pub(crate) filename: Option<String>,
8
9    pub(crate) content_type: Option<String>,
10
11    pub(crate) number_of_parts: Option<i32>,
12
13    pub(crate) external_url: Option<String>,
14}
15
16#[derive(Debug, Default, serde::Serialize, serde::Deserialize, Clone)]
17pub struct CreateFileUploadRequestBody {
18    pub(crate) mode: FileUplpadMode,
19
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub(crate) filename: Option<String>,
22
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub(crate) content_type: Option<String>,
25
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub(crate) number_of_parts: Option<i32>,
28
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub(crate) external_url: Option<String>,
31}
32
33#[derive(Debug, Default, serde::Serialize, serde::Deserialize, Clone, Copy)]
34#[serde(rename_all = "snake_case")]
35pub enum FileUplpadMode {
36    #[default]
37    SinglePart,
38    MultiPart,
39    ExternalUrl,
40}
41
42impl CreateFileUploadClient {
43    fn validate_request(&self) -> Result<(), crate::error::Error> {
44        if matches!(
45            self.mode,
46            FileUplpadMode::MultiPart | FileUplpadMode::ExternalUrl
47        ) && self.filename.is_none()
48        {
49            return Err(crate::error::Error::RequestParameter(
50                "`filename` is required when `mode` is `multi_part` or `external_url`.".to_owned(),
51            ));
52        };
53
54        if matches!(self.mode, FileUplpadMode::MultiPart) && self.number_of_parts.is_none() {
55            return Err(crate::error::Error::RequestParameter(
56                "`number_of_parts` is required when `mode` is `multi_part`.".to_owned(),
57            ));
58        };
59
60        if matches!(self.mode, FileUplpadMode::ExternalUrl) && self.filename.is_none() {
61            return Err(crate::error::Error::RequestParameter(
62                "`external_url` is required when `mode` is `external_url`.".to_owned(),
63            ));
64        };
65
66        Ok(())
67    }
68
69    pub async fn send(self) -> Result<notionrs_types::prelude::FileUpload, crate::error::Error> {
70        self.validate_request()?;
71
72        let request_body = serde_json::to_string(&CreateFileUploadRequestBody {
73            mode: self.mode,
74            filename: self.filename,
75            content_type: self.content_type,
76            number_of_parts: self.number_of_parts,
77            external_url: self.external_url,
78        })?;
79
80        let request = self
81            .reqwest_client
82            .post("https://api.notion.com/v1/file_uploads")
83            .header("Content-Type", "application/json")
84            .body(request_body);
85
86        let response =
87            crate::util::send_and_convert::<notionrs_types::prelude::FileUpload>(request).await?;
88
89        Ok(response)
90    }
91}