runpod_sdk/service/
templates.rs

1use std::future::Future;
2
3#[cfg(feature = "tracing")]
4use crate::TRACING_TARGET_SERVICE;
5use crate::model::{
6    GetTemplateQuery, ListTemplatesQuery, Template, TemplateCreateInput, TemplateUpdateInput,
7    Templates,
8};
9use crate::{Result, RunpodClient};
10
11/// Trait for managing templates.
12///
13/// Provides methods for creating, listing, retrieving, updating, and deleting templates.
14/// This trait is implemented on the [`RunpodClient`](crate::client::RunpodClient).
15pub trait TemplatesService {
16    /// Creates a new template.
17    ///
18    /// # Arguments
19    ///
20    /// * `input` - Configuration for the new template
21    ///
22    /// # Returns
23    ///
24    /// Returns the created template information.
25    ///
26    /// # Example
27    ///
28    /// ```no_run
29    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
30    /// # use runpod_sdk::model::TemplateCreateInput;
31    /// # use runpod_sdk::service::TemplatesService;
32    /// # async fn example() -> Result<()> {
33    /// let config = RunpodConfig::from_env()?;
34    /// let client = RunpodClient::new(config)?;
35    ///
36    /// let input = TemplateCreateInput {
37    ///     name: "My Template".to_string(),
38    ///     image_name: "my-image:latest".to_string(),
39    ///     docker_start_cmd: None,
40    ///     container_registry_auth_id: None,
41    ///     env: None,
42    ///     readme: None,
43    ///     is_public: None,
44    ///     is_serverless: None,
45    ///     ports: None,
46    ///     volume_in_gb: None,
47    ///     container_disk_in_gb: None,
48    ///     volume_mount_path: None,
49    ///     category: None,
50    ///     docker_entrypoint: None,
51    /// };
52    ///
53    /// let template = client.create_template(input).await?;
54    /// println!("Created template: {}", template.id);
55    /// # Ok(())
56    /// # }
57    /// ```
58    fn create_template(&self, input: TemplateCreateInput)
59    -> impl Future<Output = Result<Template>>;
60
61    /// Lists templates with optional filtering.
62    ///
63    /// # Arguments
64    ///
65    /// * `query` - Query parameters for filtering and pagination
66    ///
67    /// # Returns
68    ///
69    /// Returns a vector of templates matching the query criteria.
70    ///
71    /// # Example
72    ///
73    /// ```no_run
74    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
75    /// # use runpod_sdk::model::ListTemplatesQuery;
76    /// # use runpod_sdk::service::TemplatesService;
77    /// # async fn example() -> Result<()> {
78    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
79    /// let client = RunpodClient::new(config)?;
80    ///
81    /// let query = ListTemplatesQuery {
82    ///     include_public_templates: Some(true),
83    ///     ..Default::default()
84    /// };
85    ///
86    /// let templates = client.list_templates(query).await?;
87    /// println!("Found {} templates", templates.len());
88    /// # Ok(())
89    /// # }
90    /// ```
91    fn list_templates(&self, query: ListTemplatesQuery) -> impl Future<Output = Result<Templates>>;
92
93    /// Gets a specific template by ID.
94    ///
95    /// # Arguments
96    ///
97    /// * `template_id` - The unique identifier of the template
98    /// * `query` - Query parameters for including additional information
99    ///
100    /// # Returns
101    ///
102    /// Returns the template information.
103    ///
104    /// # Example
105    ///
106    /// ```no_run
107    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
108    /// # use runpod_sdk::model::GetTemplateQuery;
109    /// # use runpod_sdk::service::TemplatesService;
110    /// # async fn example() -> Result<()> {
111    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
112    /// let client = RunpodClient::new(config)?;
113    ///
114    /// let query = GetTemplateQuery::default();
115    ///
116    /// let template = client.get_template("template_id", query).await?;
117    /// println!("Template: {:?}", template);
118    /// # Ok(())
119    /// # }
120    /// ```
121    fn get_template(
122        &self,
123        template_id: &str,
124        query: GetTemplateQuery,
125    ) -> impl Future<Output = Result<Template>>;
126
127    /// Updates an existing template.
128    ///
129    /// # Arguments
130    ///
131    /// * `template_id` - The unique identifier of the template to update
132    /// * `input` - Update parameters for the template
133    ///
134    /// # Returns
135    ///
136    /// Returns the updated template information.
137    ///
138    /// # Example
139    ///
140    /// ```no_run
141    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
142    /// # use runpod_sdk::model::TemplateUpdateInput;
143    /// # use runpod_sdk::service::TemplatesService;
144    /// # async fn example() -> Result<()> {
145    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
146    /// let client = RunpodClient::new(config)?;
147    ///
148    /// let input = TemplateUpdateInput {
149    ///     name: Some("Updated Template".to_string()),
150    ///     ..Default::default()
151    /// };
152    ///
153    /// let template = client.update_template("template_id", input).await?;
154    /// println!("Updated template: {}", template.id);
155    /// # Ok(())
156    /// # }
157    /// ```
158    fn update_template(
159        &self,
160        template_id: &str,
161        input: TemplateUpdateInput,
162    ) -> impl Future<Output = Result<Template>>;
163
164    /// Deletes a template.
165    ///
166    /// This operation will permanently remove the template.
167    ///
168    /// # Arguments
169    ///
170    /// * `template_id` - The unique identifier of the template to delete
171    ///
172    /// # Example
173    ///
174    /// ```no_run
175    /// # use runpod_sdk::{RunpodClient, RunpodConfig, Result};
176    /// # use runpod_sdk::service::TemplatesService;
177    /// # async fn example() -> Result<()> {
178    /// let config = RunpodConfig::builder().with_api_key("your-api-key").build()?;
179    /// let client = RunpodClient::new(config)?;
180    ///
181    /// client.delete_template("template_id").await?;
182    /// println!("Template deleted");
183    /// # Ok(())
184    /// # }
185    /// ```
186    fn delete_template(&self, template_id: &str) -> impl Future<Output = Result<()>>;
187}
188
189impl TemplatesService for RunpodClient {
190    async fn create_template(&self, input: TemplateCreateInput) -> Result<Template> {
191        #[cfg(feature = "tracing")]
192        tracing::debug!(target: TRACING_TARGET_SERVICE, "Creating template");
193
194        let response = self.post("/templates").json(&input).send().await?;
195        let response = response.error_for_status()?;
196        let template: Template = response.json().await?;
197
198        #[cfg(feature = "tracing")]
199        tracing::debug!(target: TRACING_TARGET_SERVICE, template_id = %template.id, "Template created successfully");
200
201        Ok(template)
202    }
203
204    async fn list_templates(&self, query: ListTemplatesQuery) -> Result<Templates> {
205        #[cfg(feature = "tracing")]
206        tracing::debug!(target: TRACING_TARGET_SERVICE, "Listing templates");
207
208        let response = self.get("/templates").query(&query).send().await?;
209        let response = response.error_for_status()?;
210        let templates: Templates = response.json().await?;
211
212        #[cfg(feature = "tracing")]
213        tracing::debug!(target: TRACING_TARGET_SERVICE, count = templates.len(), "Templates retrieved successfully");
214
215        Ok(templates)
216    }
217
218    async fn get_template(&self, template_id: &str, query: GetTemplateQuery) -> Result<Template> {
219        #[cfg(feature = "tracing")]
220        tracing::debug!(target: TRACING_TARGET_SERVICE, "Getting template");
221
222        let path = format!("/templates/{}", template_id);
223        let response = self.get(&path).query(&query).send().await?;
224        let response = response.error_for_status()?;
225        let template: Template = response.json().await?;
226
227        #[cfg(feature = "tracing")]
228        tracing::debug!(target: TRACING_TARGET_SERVICE, "Template retrieved successfully");
229
230        Ok(template)
231    }
232
233    async fn update_template(
234        &self,
235        template_id: &str,
236        input: TemplateUpdateInput,
237    ) -> Result<Template> {
238        #[cfg(feature = "tracing")]
239        tracing::debug!(target: TRACING_TARGET_SERVICE, "Updating template");
240
241        let path = format!("/templates/{}", template_id);
242        let response = self.patch(&path).json(&input).send().await?;
243        let response = response.error_for_status()?;
244        let template: Template = response.json().await?;
245
246        #[cfg(feature = "tracing")]
247        tracing::debug!(target: TRACING_TARGET_SERVICE, "Template updated successfully");
248
249        Ok(template)
250    }
251
252    async fn delete_template(&self, template_id: &str) -> Result<()> {
253        #[cfg(feature = "tracing")]
254        tracing::debug!(target: TRACING_TARGET_SERVICE, "Deleting template");
255
256        let path = format!("/templates/{}", template_id);
257        let response = self.delete(&path).send().await?;
258        response.error_for_status()?;
259
260        #[cfg(feature = "tracing")]
261        tracing::debug!(target: TRACING_TARGET_SERVICE, "Template deleted successfully");
262
263        Ok(())
264    }
265}