Skip to main content

rainy_sdk/endpoints/
keys.rs

1use crate::client::RainyClient;
2use crate::error::Result;
3use crate::models::ApiKey;
4
5impl RainyClient {
6    /// Create a new API key
7    ///
8    /// This endpoint requires user authentication with a master API key.
9    ///
10    /// # Arguments
11    ///
12    /// * `description` - Description of what this API key will be used for
13    /// * `expires_in_days` - Optional expiration time in days
14    ///
15    /// # Returns
16    ///
17    /// Returns the created API key information.
18    ///
19    /// # Example
20    ///
21    /// ```rust,no_run
22    /// # use rainy_sdk::RainyClient;
23    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
24    /// let client = RainyClient::with_api_key("master-api-key")?;
25    ///
26    /// let api_key = client.create_api_key(
27    ///     "Production API key",
28    ///     Some(365)
29    /// ).await?;
30    ///
31    /// println!("Created API key: {}", api_key.key);
32    /// # Ok(())
33    /// # }
34    /// ```
35    #[deprecated(
36        note = "Rainy API v3 key management requires JWT/session auth. Use a session client against /api/v1/auth and /api/v1/keys."
37    )]
38    pub async fn create_api_key(
39        &self,
40        description: &str,
41        expires_in_days: Option<u32>,
42    ) -> Result<ApiKey> {
43        let mut body = serde_json::json!({
44            "description": description
45        });
46
47        if let Some(days) = expires_in_days {
48            body["expiresInDays"] = serde_json::json!(days);
49        }
50
51        self.make_request(reqwest::Method::POST, "/keys", Some(body))
52            .await
53    }
54
55    /// List all API keys for the current user
56    ///
57    /// This endpoint requires user authentication.
58    ///
59    /// # Returns
60    ///
61    /// Returns a vector of all API keys owned by the authenticated user.
62    ///
63    /// # Example
64    ///
65    /// ```rust,no_run
66    /// # use rainy_sdk::RainyClient;
67    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
68    /// let client = RainyClient::with_api_key("user-api-key")?;
69    ///
70    /// let keys = client.list_api_keys().await?;
71    /// for key in keys {
72    ///     println!("Key: {} - Active: {}", key.key, key.is_active);
73    /// }
74    /// # Ok(())
75    /// # }
76    /// ```
77    #[deprecated(
78        note = "Rainy API v3 key management requires JWT/session auth. Use a session client against /api/v1/keys."
79    )]
80    pub async fn list_api_keys(&self) -> Result<Vec<ApiKey>> {
81        #[derive(serde::Deserialize)]
82        struct ApiKeysResponse {
83            api_keys: Vec<ApiKey>,
84        }
85
86        let response: ApiKeysResponse = self
87            .make_request(reqwest::Method::GET, "/users/account", None)
88            .await?;
89
90        Ok(response.api_keys)
91    }
92
93    /// Update an API key
94    ///
95    /// This endpoint requires user authentication.
96    ///
97    /// # Arguments
98    ///
99    /// * `key_id` - The UUID of the API key to update
100    /// * `updates` - JSON object containing the fields to update
101    ///
102    /// # Returns
103    ///
104    /// Returns the updated API key information.
105    ///
106    /// # Example
107    ///
108    /// ```rust,no_run
109    /// # use rainy_sdk::{RainyClient, AuthConfig};
110    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
111    /// use serde_json::json;
112    ///
113    /// let client = RainyClient::with_api_key("user-api-key")?;
114    ///
115    /// let updates = json!({
116    ///     "description": "Updated description"
117    /// });
118    ///
119    /// let updated_key = client.update_api_key(
120    ///     "550e8400-e29b-41d4-a716-446655440000",
121    ///     updates
122    /// ).await?;
123    /// # Ok(())
124    /// # }
125    /// ```
126    #[deprecated(
127        note = "Rainy API v3 key management requires JWT/session auth. Use a session client against /api/v1/keys."
128    )]
129    pub async fn update_api_key(&self, key_id: &str, updates: serde_json::Value) -> Result<ApiKey> {
130        self.make_request(
131            reqwest::Method::PATCH,
132            &format!("/keys/{key_id}"),
133            Some(updates),
134        )
135        .await
136    }
137
138    /// Delete an API key
139    ///
140    /// This endpoint requires user authentication.
141    ///
142    /// # Arguments
143    ///
144    /// * `key_id` - The UUID of the API key to delete
145    ///
146    /// # Example
147    ///
148    /// ```rust,no_run
149    /// # use rainy_sdk::RainyClient;
150    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
151    /// let client = RainyClient::with_api_key("user-api-key")?;
152    ///
153    /// client.delete_api_key("550e8400-e29b-41d4-a716-446655440000").await?;
154    /// println!("API key deleted successfully");
155    /// # Ok(())
156    /// # }
157    /// ```
158    #[deprecated(
159        note = "Rainy API v3 key management requires JWT/session auth. Use a session client against /api/v1/keys."
160    )]
161    pub async fn delete_api_key(&self, key_id: &str) -> Result<()> {
162        self.make_request(reqwest::Method::DELETE, &format!("/keys/{key_id}"), None)
163            .await
164    }
165}