redmine_api/api/
attachments.rs

1//! Attachments Rest API Endpoint definitions
2//!
3//! [Redmine Documentation](https://www.redmine.org/projects/redmine/wiki/Rest_Attachments)
4//!
5//! - [x] specific attachment endpoint
6//! - [ ] update attachment endpoint (not documented and the link to the issue in the wiki points to an issue about something else)
7//! - [x] delete attachment endpoint
8
9use derive_builder::Builder;
10use reqwest::Method;
11use std::borrow::Cow;
12
13use crate::api::users::UserEssentials;
14use crate::api::{Endpoint, ReturnsJsonResponse};
15
16/// a type for attachment to use as an API return type
17///
18/// alternatively you can use your own type limited to the fields you need
19#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
20pub struct Attachment {
21    /// numeric id
22    pub id: u64,
23    /// filename as specified on upload
24    pub filename: String,
25    /// file size
26    pub filesize: u64,
27    /// content MIME type
28    pub content_type: Option<String>,
29    /// description
30    #[serde(default)]
31    pub description: Option<String>,
32    /// url where the content of this attachment can be downloaded
33    pub content_url: String,
34    /// uploader
35    pub author: UserEssentials,
36    /// The time when this file was uploaded
37    #[serde(
38        serialize_with = "crate::api::serialize_rfc3339",
39        deserialize_with = "crate::api::deserialize_rfc3339"
40    )]
41    pub created_on: time::OffsetDateTime,
42}
43
44/// The endpoint for a specific Redmine attachment
45#[derive(Debug, Clone, Builder)]
46#[builder(setter(strip_option))]
47pub struct GetAttachment {
48    /// id of the attachment to retrieve
49    id: u64,
50}
51
52impl ReturnsJsonResponse for GetAttachment {}
53
54impl GetAttachment {
55    /// Create a builder for the endpoint.
56    #[must_use]
57    pub fn builder() -> GetAttachmentBuilder {
58        GetAttachmentBuilder::default()
59    }
60}
61
62impl Endpoint for GetAttachment {
63    fn method(&self) -> Method {
64        Method::GET
65    }
66
67    fn endpoint(&self) -> Cow<'static, str> {
68        format!("attachments/{}.json", &self.id).into()
69    }
70}
71
72/// The endpoint to delete a Redmine attachment
73#[derive(Debug, Clone, Builder)]
74#[builder(setter(strip_option))]
75pub struct DeleteAttachment {
76    /// id of the attachment to delete
77    id: u64,
78}
79
80impl DeleteAttachment {
81    /// Create a builder for the endpoint.
82    #[must_use]
83    pub fn builder() -> DeleteAttachmentBuilder {
84        DeleteAttachmentBuilder::default()
85    }
86}
87
88impl Endpoint for DeleteAttachment {
89    fn method(&self) -> Method {
90        Method::DELETE
91    }
92
93    fn endpoint(&self) -> Cow<'static, str> {
94        format!("attachments/{}.json", &self.id).into()
95    }
96}
97
98/// helper struct for outer layers with a attachment field holding the inner data
99#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
100pub struct AttachmentWrapper<T> {
101    /// to parse JSON with attachment key
102    pub attachment: T,
103}
104
105#[cfg(test)]
106mod test {
107    use super::*;
108    use pretty_assertions::assert_eq;
109    use std::error::Error;
110    use tracing_test::traced_test;
111
112    #[traced_test]
113    #[test]
114    fn test_get_attachment() -> Result<(), Box<dyn Error>> {
115        dotenvy::dotenv()?;
116        let redmine = crate::api::Redmine::from_env()?;
117        let endpoint = GetAttachment::builder().id(3).build()?;
118        redmine.json_response_body::<_, AttachmentWrapper<Attachment>>(&endpoint)?;
119        Ok(())
120    }
121
122    /// this tests if any of the results contain a field we are not deserializing
123    ///
124    /// this will only catch fields we missed if they are part of the response but
125    /// it is better than nothing
126    #[traced_test]
127    #[test]
128    fn test_completeness_attachment_type() -> Result<(), Box<dyn Error>> {
129        dotenvy::dotenv()?;
130        let redmine = crate::api::Redmine::from_env()?;
131        let endpoint = GetAttachment::builder().id(3).build()?;
132        let AttachmentWrapper { attachment: value } =
133            redmine.json_response_body::<_, AttachmentWrapper<serde_json::Value>>(&endpoint)?;
134        let o: Attachment = serde_json::from_value(value.clone())?;
135        let reserialized = serde_json::to_value(o)?;
136        assert_eq!(value, reserialized);
137        Ok(())
138    }
139}