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 /// # Errors
22 ///
23 /// This method can return the following errors:
24 /// - `400` - Invalid request
25 /// - `401` - Invalid authorization
26 /// - `500` - Internal server error
27 ///
28 /// # Examples
29 ///
30 /// ```no_run
31 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
32 /// use rain_sdk::models::keys::CreateKeyRequest;
33 /// use chrono::Utc;
34 ///
35 /// # #[cfg(feature = "async")]
36 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
37 /// let config = Config::new(Environment::Dev);
38 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
39 /// let client = RainClient::new(config, auth)?;
40 ///
41 /// let request = CreateKeyRequest {
42 /// name: "My API Key".to_string(),
43 /// expires_at: Utc::now() + chrono::Duration::days(90),
44 /// };
45 /// let key = client.create_key(&request).await?;
46 /// println!("Created key: {}", key.key);
47 /// # Ok(())
48 /// # }
49 /// ```
50 #[cfg(feature = "async")]
51 pub async fn create_key(&self, request: &CreateKeyRequest) -> Result<Key> {
52 let path = "/keys";
53 self.post(path, request).await
54 }
55
56 /// Delete a key
57 ///
58 /// # Arguments
59 ///
60 /// * `key_id` - The unique identifier of the key to delete
61 ///
62 /// # Returns
63 ///
64 /// Returns success (204 No Content) with no response body.
65 ///
66 /// # Errors
67 ///
68 /// This method can return the following errors:
69 /// - `401` - Invalid authorization
70 /// - `404` - Key not found
71 /// - `500` - Internal server error
72 ///
73 /// # Examples
74 ///
75 /// ```no_run
76 /// use rain_sdk::{RainClient, Config, Environment, AuthConfig};
77 /// use uuid::Uuid;
78 ///
79 /// # #[cfg(feature = "async")]
80 /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
81 /// let config = Config::new(Environment::Dev);
82 /// let auth = AuthConfig::with_api_key("your-api-key".to_string());
83 /// let client = RainClient::new(config, auth)?;
84 ///
85 /// let key_id = Uuid::new_v4();
86 /// client.delete_key(&key_id).await?;
87 /// # Ok(())
88 /// # }
89 /// ```
90 #[cfg(feature = "async")]
91 pub async fn delete_key(&self, key_id: &Uuid) -> Result<()> {
92 let path = format!("/keys/{key_id}");
93 self.delete(&path).await
94 }
95
96 // ============================================================================
97 // Blocking Methods
98 // ============================================================================
99
100 /// Create a key (blocking)
101 #[cfg(feature = "sync")]
102 pub fn create_key_blocking(&self, request: &CreateKeyRequest) -> Result<Key> {
103 let path = "/keys";
104 self.post_blocking(path, request)
105 }
106
107 /// Delete a key (blocking)
108 #[cfg(feature = "sync")]
109 pub fn delete_key_blocking(&self, key_id: &Uuid) -> Result<()> {
110 let path = format!("/keys/{key_id}");
111 self.delete_blocking(&path)
112 }
113}