ridewithgps_client/
auth.rs

1//! Authentication-related types and methods
2
3use crate::{Result, RideWithGpsClient, User};
4use serde::{Deserialize, Serialize};
5
6/// Request to create an authentication token
7#[derive(Debug, Clone, Serialize)]
8pub struct CreateAuthTokenRequest {
9    /// User email
10    pub email: String,
11
12    /// User password
13    pub password: String,
14}
15
16/// Response containing an authentication token
17#[derive(Debug, Clone, Deserialize, Serialize)]
18pub struct AuthToken {
19    /// The authentication token
20    pub auth_token: String,
21
22    /// User ID
23    pub user_id: Option<u64>,
24
25    /// User object (included in the response)
26    pub user: Option<User>,
27}
28
29impl RideWithGpsClient {
30    /// Create an authentication token using email and password
31    ///
32    /// # Arguments
33    ///
34    /// * `email` - User email address
35    /// * `password` - User password
36    ///
37    /// # Example
38    ///
39    /// ```rust,no_run
40    /// use ridewithgps_client::RideWithGpsClient;
41    ///
42    /// let client = RideWithGpsClient::new(
43    ///     "https://ridewithgps.com",
44    ///     "your-api-key",
45    ///     None
46    /// );
47    ///
48    /// let auth = client.create_auth_token("user@example.com", "password").unwrap();
49    /// println!("Auth token: {}", auth.auth_token);
50    /// ```
51    pub fn create_auth_token(&self, email: &str, password: &str) -> Result<AuthToken> {
52        let request = CreateAuthTokenRequest {
53            email: email.to_string(),
54            password: password.to_string(),
55        };
56
57        self.post("/api/v1/auth_tokens", &request)
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_auth_token_request_serialization() {
67        let request = CreateAuthTokenRequest {
68            email: "test@example.com".to_string(),
69            password: "password123".to_string(),
70        };
71
72        let json = serde_json::to_string(&request).unwrap();
73        assert!(json.contains("test@example.com"));
74        assert!(json.contains("password123"));
75    }
76}