coman/core/
endpoint_ops.rs

1use crate::core::collection_manager::CollectionResult;
2use crate::core::errors::CollectionError;
3use crate::core::utils::merge_headers;
4use crate::{CollectionManager, Method, Request};
5
6impl CollectionManager {
7    /// Get the full URL for an endpoint (base URL + endpoint path)
8    pub async fn get_endpoint_url(
9        &self,
10        col_name: &str,
11        ep_name: &str,
12    ) -> CollectionResult<String> {
13        if let Some(col) = self.get_collection(col_name).await? {
14            if let Some(ep) = self.get_endpoint(col_name, ep_name).await? {
15                return Ok(format!("{}{}", col.url, ep.endpoint));
16            }
17        }
18        Err(CollectionError::EndpointNotFound(format!(
19            "{} in {}",
20            ep_name, col_name
21        )))
22    }
23
24    /// Get merged headers for an endpoint (collection headers + endpoint headers)
25    pub async fn get_endpoint_headers(
26        &self,
27        collection: &str,
28        endpoint: &str,
29    ) -> Vec<(String, String)> {
30        let mut headers = Vec::new();
31        if let Some(col) = self.get_collection(collection).await.unwrap() {
32            headers = merge_headers(headers, &col.headers);
33            if let Some(ep) = self.get_endpoint(collection, endpoint).await.unwrap() {
34                headers = merge_headers(headers, &ep.headers);
35            }
36        }
37        headers
38    }
39
40    /// Add an endpoint to a collection
41    ///
42    /// If an endpoint with the same name exists, it will be updated.
43    pub async fn add_endpoint(
44        &self,
45        col_name: &str,
46        ep_name: &str,
47        path: &str,
48        method: Method,
49        headers: Vec<(String, String)>,
50        body: Option<String>,
51    ) -> CollectionResult<()> {
52        let request = Request {
53            name: ep_name.to_string(),
54            endpoint: path.to_string(),
55            method,
56            headers,
57            body,
58        };
59
60        self.update_add_request(col_name, ep_name, request).await?;
61        Ok(())
62    }
63
64    /// Update an endpoint in a collection
65    pub async fn update_endpoint(
66        &self,
67        col_name: &str,
68        ep_name: &str,
69        path: Option<&str>,
70        headers: Option<Vec<(String, String)>>,
71        body: Option<String>,
72    ) -> CollectionResult<()> {
73        if let Some(mut req) = self.get_endpoint(col_name, ep_name).await? {
74            if let Some(p) = path {
75                req.endpoint = p.to_string();
76            }
77            if let Some(h) = headers {
78                req.headers = merge_headers(req.headers.clone(), &h);
79            }
80            if let Some(b) = body {
81                req.body = if b.is_empty() { None } else { Some(b) };
82            }
83            self.update_add_request(col_name, ep_name, req).await?;
84            Ok(())
85        } else {
86            Err(CollectionError::EndpointNotFound(format!(
87                "{} in {}",
88                ep_name, col_name
89            )))
90        }
91    }
92
93    /// Copy an endpoint within the same collection or to another collection
94    pub async fn copy_endpoint(
95        &self,
96        col_name: &str,
97        ep_name: &str,
98        new_name: &str,
99        to_col: Option<&str>,
100    ) -> CollectionResult<()> {
101        let request = self.get_endpoint(col_name, ep_name).await?;
102        if let Some(req) = request {
103            let mut new_req = req.clone();
104            if let Some(target_col_name) = to_col {
105                // Copy to another collection (keep original name)
106                self.update_add_request(target_col_name, &new_req.name, new_req.clone())
107                    .await?;
108            } else {
109                // Copy within the same collection with a new name
110                new_req.name = new_name.to_string();
111                self.update_add_request(col_name, &new_req.name, new_req.clone())
112                    .await?;
113            }
114            Ok(())
115        } else {
116            Err(CollectionError::EndpointNotFound(format!(
117                "{} in {}",
118                ep_name, col_name
119            )))
120        }
121    }
122
123    /// Delete an endpoint from a collection
124    pub async fn delete_endpoint(&self, collection: &str, endpoint: &str) -> CollectionResult<()> {
125        if let Some(mut col) = self.get_collection(collection).await? {
126            if let Some(ref mut requests) = col.requests {
127                let original_len = requests.len();
128                requests.retain(|r| r.name != endpoint);
129
130                if requests.len() == original_len {
131                    return Err(CollectionError::EndpointNotFound(endpoint.to_string()));
132                }
133
134                self.update_add_collection(col).await?;
135                return Ok(());
136            }
137            return Err(CollectionError::EndpointNotFound(endpoint.to_string()));
138        }
139        Err(CollectionError::CollectionNotFound(collection.to_string()))
140    }
141}