Skip to main content

deribit_http/model/
access_log.rs

1//! Access log models for Deribit API
2//!
3//! This module contains types for account access history.
4
5use serde::{Deserialize, Serialize};
6
7/// Access log entry representing a single access event
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct AccessLogEntry {
10    /// Timestamp of the access event in milliseconds
11    pub timestamp: u64,
12    /// IP address from which the access occurred
13    pub ip: String,
14    /// Action performed (e.g., "login", "api_call")
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub action: Option<String>,
17    /// Result of the action (e.g., "success", "failure")
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub result: Option<String>,
20    /// Country code derived from IP address
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub country: Option<String>,
23    /// City derived from IP address
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub city: Option<String>,
26    /// Log entry ID
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub id: Option<u64>,
29    /// Additional data associated with the access event
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub data: Option<serde_json::Value>,
32}
33
34/// Response for get_access_log endpoint
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub struct AccessLogResponse {
37    /// List of access log entries
38    pub data: Vec<AccessLogEntry>,
39    /// Continuation token for pagination
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub continuation: Option<String>,
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_access_log_entry_deserialization() {
50        let json = r#"{
51            "timestamp": 1550058362000,
52            "ip": "192.168.1.1",
53            "action": "login",
54            "result": "success",
55            "country": "US",
56            "city": "New York"
57        }"#;
58
59        let entry: AccessLogEntry = serde_json::from_str(json).expect("Failed to parse");
60        assert_eq!(entry.timestamp, 1550058362000);
61        assert_eq!(entry.ip, "192.168.1.1");
62        assert_eq!(entry.action, Some("login".to_string()));
63    }
64
65    #[test]
66    fn test_access_log_response_deserialization() {
67        let json = r#"{
68            "data": [
69                {
70                    "timestamp": 1000000,
71                    "ip": "10.0.0.1"
72                }
73            ],
74            "continuation": "abc123"
75        }"#;
76
77        let response: AccessLogResponse = serde_json::from_str(json).expect("Failed to parse");
78        assert_eq!(response.data.len(), 1);
79        assert_eq!(response.continuation, Some("abc123".to_string()));
80    }
81}