//! The type that represents a response of `files.getUploadURLExternal`.
//!
//! <https://api.slack.com/methods/files.getUploadURLExternal>

use crate::generated::files_get_upload_url_external::FilesGetUploadUrlExternalResponse as Generated;
use crate::Error;
use serde::Deserialize;

/// The type that represents a response of `files.getUploadURLExternal`.
///
/// <https://api.slack.com/methods/files.getUploadURLExternal>
#[derive(Debug, Deserialize)]
pub struct FilesGetUploadUrlExternalResponse(Generated);

impl FilesGetUploadUrlExternalResponse {
    /// Converts to generated type.
    pub fn into_inner(self) -> Generated {
        self.0
    }

    /// Takes `upload_url`.
    pub fn upload_url(&self) -> Option<&str> {
        self.0.upload_url.as_deref()
    }

    /// Takes `file_id`.
    pub fn file_id(&self) -> Option<&str> {
        self.0.file_id.as_deref()
    }
}

impl TryFrom<serde_json::Value> for FilesGetUploadUrlExternalResponse {
    type Error = Error;

    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
        serde_json::from_value(value).map_err(Error::DeserializeJson)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_de_files_get_upload_url_external_response() -> anyhow::Result<()> {
        let testdata = r#"{
            "ok": true,
            "upload_url": "https://files.slack.com/upload/v1/ABC123...",
            "file_id": "F123ABC456"
        }"#;
        let deserialized: FilesGetUploadUrlExternalResponse = serde_json::from_str(testdata)?;
        let deserialized = deserialized.into_inner();

        assert_eq!(
            deserialized.upload_url.unwrap(),
            "https://files.slack.com/upload/v1/ABC123..."
        );
        assert_eq!(deserialized.file_id.unwrap(), "F123ABC456");

        Ok(())
    }
}
