runpod_sdk/service/
endpoints.rs

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