kiteconnect_async_wasm/models/common/
response.rs

1/*!
2Response wrapper types for KiteConnect API responses.
3
4All KiteConnect API responses follow a standard format:
5```json
6{
7    "status": "success" | "error",
8    "data": { ... } | null,
9    "message": "string",
10    "error_type": "string" (optional)
11}
12```
13*/
14
15use serde::{Deserialize, Serialize};
16use serde_json::Value as JsonValue;
17
18use super::errors::{KiteError, KiteResult};
19
20/// Standard KiteConnect API response wrapper
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct KiteResponse<T> {
23    /// Response status ("success" or "error")
24    pub status: String,
25
26    /// Response data (None for error responses)
27    pub data: Option<T>,
28
29    /// Response message
30    #[serde(default)]
31    pub message: String,
32
33    /// Error type (for error responses)
34    #[serde(default)]
35    pub error_type: Option<String>,
36}
37
38impl<T> KiteResponse<T> {
39    /// Create a new success response
40    pub fn success(data: T) -> Self {
41        Self {
42            status: "success".to_string(),
43            data: Some(data),
44            message: String::new(),
45            error_type: None,
46        }
47    }
48
49    /// Create a new error response
50    pub fn error(message: impl Into<String>, error_type: Option<String>) -> Self {
51        Self {
52            status: "error".to_string(),
53            data: None,
54            message: message.into(),
55            error_type,
56        }
57    }
58
59    /// Check if the response is successful
60    pub fn is_success(&self) -> bool {
61        self.status == "success"
62    }
63
64    /// Check if the response is an error
65    pub fn is_error(&self) -> bool {
66        self.status == "error"
67    }
68
69    /// Extract the data or return an error
70    pub fn into_result(self) -> KiteResult<T> {
71        match self.status.as_str() {
72            "success" => self
73                .data
74                .ok_or_else(|| KiteError::general("Success response missing data")),
75            "error" => Err(KiteError::api_error_with_type(
76                self.status,
77                self.message,
78                self.error_type.unwrap_or_default(),
79            )),
80            _ => Err(KiteError::general(format!(
81                "Unknown response status: {}",
82                self.status
83            ))),
84        }
85    }
86}
87
88/// Raw response for backward compatibility and debugging
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct RawResponse {
91    /// Response status
92    pub status: String,
93
94    /// Raw JSON data
95    pub data: Option<JsonValue>,
96
97    /// Response message
98    #[serde(default)]
99    pub message: String,
100
101    /// Error type
102    #[serde(default)]
103    pub error_type: Option<String>,
104}
105
106impl From<RawResponse> for KiteResponse<JsonValue> {
107    fn from(raw: RawResponse) -> Self {
108        Self {
109            status: raw.status,
110            data: raw.data,
111            message: raw.message,
112            error_type: raw.error_type,
113        }
114    }
115}
116
117/// Status enum for type-safe status handling
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
119#[serde(rename_all = "lowercase")]
120pub enum Status {
121    Success,
122    Error,
123}
124
125impl Status {
126    pub fn is_success(self) -> bool {
127        matches!(self, Status::Success)
128    }
129
130    pub fn is_error(self) -> bool {
131        matches!(self, Status::Error)
132    }
133}
134
135impl From<String> for Status {
136    fn from(s: String) -> Self {
137        match s.to_lowercase().as_str() {
138            "success" => Status::Success,
139            "error" => Status::Error,
140            _ => Status::Error, // Default to error for unknown status
141        }
142    }
143}
144
145impl From<&str> for Status {
146    fn from(s: &str) -> Self {
147        Self::from(s.to_string())
148    }
149}