rain_sdk/api/
keys.rs

1//! Keys API
2//!
3//! This module provides functionality to manage API keys.
4
5use crate::client::RainClient;
6use crate::error::Result;
7use crate::models::keys::*;
8use uuid::Uuid;
9
10impl RainClient {
11    /// Create a key
12    ///
13    /// # Arguments
14    ///
15    /// * `request` - The key creation request
16    ///
17    /// # Returns
18    ///
19    /// Returns a [`Key`] containing the created key information (including the key itself).
20    ///
21    /// # Examples
22    ///
23    /// ```no_run
24    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
25    /// use rain_sdk::models::keys::CreateKeyRequest;
26    /// use chrono::Utc;
27    ///
28    /// # #[cfg(feature = "async")]
29    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
30    /// let config = Config::new(Environment::Dev);
31    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
32    /// let client = RainClient::new(config, auth)?;
33    ///
34    /// let request = CreateKeyRequest {
35    ///     name: "My API Key".to_string(),
36    ///     expires_at: Utc::now() + chrono::Duration::days(90),
37    /// };
38    /// let key = client.create_key(&request).await?;
39    /// println!("Created key: {}", key.key);
40    /// # Ok(())
41    /// # }
42    /// ```
43    #[cfg(feature = "async")]
44    pub async fn create_key(&self, request: &CreateKeyRequest) -> Result<Key> {
45        let path = "/issuing/keys";
46        self.post(path, request).await
47    }
48
49    /// Delete a key
50    ///
51    /// # Arguments
52    ///
53    /// * `key_id` - The unique identifier of the key to delete
54    ///
55    /// # Returns
56    ///
57    /// Returns success (204 No Content) with no response body.
58    ///
59    /// # Examples
60    ///
61    /// ```no_run
62    /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
63    /// use uuid::Uuid;
64    ///
65    /// # #[cfg(feature = "async")]
66    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
67    /// let config = Config::new(Environment::Dev);
68    /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
69    /// let client = RainClient::new(config, auth)?;
70    ///
71    /// let key_id = Uuid::new_v4();
72    /// client.delete_key(&key_id).await?;
73    /// # Ok(())
74    /// # }
75    /// ```
76    #[cfg(feature = "async")]
77    pub async fn delete_key(&self, key_id: &Uuid) -> Result<()> {
78        let path = format!("/issuing/keys/{key_id}");
79        self.delete(&path).await
80    }
81
82    // ============================================================================
83    // Blocking Methods
84    // ============================================================================
85
86    /// Create a key (blocking)
87    #[cfg(feature = "sync")]
88    pub fn create_key_blocking(&self, request: &CreateKeyRequest) -> Result<Key> {
89        let path = "/issuing/keys";
90        self.post_blocking(path, request)
91    }
92
93    /// Delete a key (blocking)
94    #[cfg(feature = "sync")]
95    pub fn delete_key_blocking(&self, key_id: &Uuid) -> Result<()> {
96        let path = format!("/issuing/keys/{key_id}");
97        self.delete_blocking(&path)
98    }
99}