use derive_builder::Builder;
use reqwest::Method;
use std::borrow::Cow;
use crate::api::users::UserEssentials;
use crate::api::{Endpoint, ReturnsJsonResponse};
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Attachment {
pub id: u64,
pub filename: String,
pub filesize: u64,
pub content_type: Option<String>,
#[serde(default)]
pub description: Option<String>,
pub content_url: String,
pub author: UserEssentials,
#[serde(
serialize_with = "crate::api::serialize_rfc3339",
deserialize_with = "crate::api::deserialize_rfc3339"
)]
pub created_on: time::OffsetDateTime,
}
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct GetAttachment {
id: u64,
}
impl ReturnsJsonResponse for GetAttachment {}
impl GetAttachment {
#[must_use]
pub fn builder() -> GetAttachmentBuilder {
GetAttachmentBuilder::default()
}
}
impl Endpoint for GetAttachment {
fn method(&self) -> Method {
Method::GET
}
fn endpoint(&self) -> Cow<'static, str> {
format!("attachments/{}.json", &self.id).into()
}
}
#[derive(Debug, Builder)]
#[builder(setter(strip_option))]
pub struct DeleteAttachment {
id: u64,
}
impl DeleteAttachment {
#[must_use]
pub fn builder() -> DeleteAttachmentBuilder {
DeleteAttachmentBuilder::default()
}
}
impl Endpoint for DeleteAttachment {
fn method(&self) -> Method {
Method::DELETE
}
fn endpoint(&self) -> Cow<'static, str> {
format!("attachments/{}.json", &self.id).into()
}
}
#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AttachmentWrapper<T> {
pub attachment: T,
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
use std::error::Error;
use tracing_test::traced_test;
#[traced_test]
#[test]
fn test_get_attachment() -> Result<(), Box<dyn Error>> {
dotenvy::dotenv()?;
let redmine = crate::api::Redmine::from_env()?;
let endpoint = GetAttachment::builder().id(3).build()?;
redmine.json_response_body::<_, AttachmentWrapper<Attachment>>(&endpoint)?;
Ok(())
}
#[traced_test]
#[test]
fn test_completeness_attachment_type() -> Result<(), Box<dyn Error>> {
dotenvy::dotenv()?;
let redmine = crate::api::Redmine::from_env()?;
let endpoint = GetAttachment::builder().id(3).build()?;
let AttachmentWrapper { attachment: value } =
redmine.json_response_body::<_, AttachmentWrapper<serde_json::Value>>(&endpoint)?;
let o: Attachment = serde_json::from_value(value.clone())?;
let reserialized = serde_json::to_value(o)?;
assert_eq!(value, reserialized);
Ok(())
}
}