clickatell_api/one_api/message_status/
response.rs1use crate::one_api::message_status::status::Status;
2use crate::one_api::Channel;
3use chrono::{DateTime, TimeZone, Utc};
4use serde::Deserialize;
5
6#[derive(Deserialize, PartialEq, Debug)]
8pub struct Response {
9 sms: Option<StatusEntry>,
10 whatsapp: Option<StatusEntry>,
11}
12
13impl Response {
14 pub fn channel(&self) -> Channel {
18 if self.whatsapp.is_some() {
19 Channel::WhatsApp
20 } else if self.sms.is_some() {
21 Channel::SMS
22 } else {
23 Channel::Unknown
24 }
25 }
26
27 fn status_entry(&self) -> StatusEntry {
28 match self.channel() {
29 Channel::SMS => self.sms.unwrap_or_default(),
30 Channel::WhatsApp => self.whatsapp.unwrap_or_default(),
31 Channel::Unknown => StatusEntry::default(),
32 }
33 }
34
35 pub fn status(&self) -> Status {
39 self.status_entry().status
40 }
41
42 pub fn updated_at(&self) -> DateTime<Utc> {
46 Utc.timestamp_millis(self.status_entry().timestamp)
47 }
48}
49
50impl std::fmt::Display for Response {
51 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
52 write!(f, "{:?}", self.status())
53 }
54}
55
56#[derive(Deserialize, PartialEq, Debug, Clone, Copy)]
57struct StatusEntry {
58 pub status: Status,
59 pub timestamp: i64,
60}
61
62impl Default for StatusEntry {
63 fn default() -> Self {
64 Self {
65 timestamp: Utc::now().timestamp_millis(),
66 status: Status::Unknown,
67 }
68 }
69}
70
71#[test]
72fn test_deserialize() {
73 let response_200 = r#" {
74 "sms": {
75 "status": "QUEUED",
76 "timestamp": 1506607698000
77 }
78 }"#;
79
80 let response = serde_json::from_str::<Response>(response_200).unwrap();
81
82 assert_eq!(Channel::SMS, response.channel());
83 assert_eq!(Status::Queued, response.status());
84}