fundamentum_sdk_api/models/
api_response.rs1use serde::Deserialize;
4
5#[derive(Clone, Debug, Eq, PartialEq, Default, Deserialize)]
7pub struct ApiResponse {
8 #[serde(rename = "status", skip_serializing_if = "Option::is_none")]
10 pub status: Option<ApiStatus>,
11 #[serde(rename = "message", skip_serializing_if = "Option::is_none")]
13 pub message: Option<String>,
14 #[serde(rename = "data", skip_serializing_if = "Option::is_none")]
16 pub data: Option<serde_json::Value>,
17}
18
19#[derive(Deserialize, PartialEq, Eq, Debug, Clone)]
21#[serde(rename_all = "lowercase")]
22pub enum ApiStatus {
23 Success,
25 Error,
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use serde_json::json;
33
34 #[test]
35 fn given_good_response_when_deserializing_status_then_return_struct() {
36 let string = r#"{ "status": "success", "data": { "test1": 42 } }"#;
37
38 let status: ApiResponse = serde_json::from_str(string).expect("Error with deserialization");
39
40 assert_eq!(
41 status,
42 ApiResponse {
43 status: Some(ApiStatus::Success),
44 message: None,
45 data: Some(json!({"test1": 42}))
46 }
47 );
48 }
49
50 #[test]
51 fn given_error_repsonse_when_deserializing_status_then_return_struct() {
52 let string =
53 r#"{ "status": "error", "message": "testing message", "data": { "test1": 42 } }"#;
54
55 let status: ApiResponse = serde_json::from_str(string).expect("Error with deserialization");
56
57 assert_eq!(
58 status,
59 ApiResponse {
60 status: Some(ApiStatus::Error),
61 message: Some("testing message".into()),
62 data: Some(json!({"test1": 42}))
63 }
64 );
65 }
66
67 #[test]
68 #[should_panic(expected = "Error with deserialization")]
69 fn given_any_repsonse_when_deserializing_status_then_return_struct() {
70 let string = r#"{ "status": "any", "data": { }"#;
71
72 let status: ApiResponse = serde_json::from_str(string).expect("Error with deserialization");
73
74 assert_eq!(
75 status,
76 ApiResponse {
77 status: Some(ApiStatus::Error),
78 message: None,
79 data: Some(json!({}))
80 }
81 );
82 }
83}