conductor/client/integration_client.rs
1// Copyright {{.Year}} Conductor OSS
2// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
4use crate::error::Result;
5use crate::http::{ApiClient, ApiPath};
6use crate::models::{
7 Integration, IntegrationApi, IntegrationApiUpdate, IntegrationUpdate, MetadataTag,
8 PromptTemplate,
9};
10
11/// Client for managing integrations with external systems.
12#[derive(Clone)]
13pub struct IntegrationClient {
14 api: ApiClient,
15}
16
17impl IntegrationClient {
18 /// Create a new integration client.
19 #[must_use]
20 pub fn new(api: ApiClient) -> Self {
21 Self { api }
22 }
23
24 /// Associate a prompt with an AI integration and model.
25 ///
26 /// # Errors
27 ///
28 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
29 pub async fn associate_prompt_with_integration(
30 &self,
31 ai_integration: &str,
32 model_name: &str,
33 prompt_name: &str,
34 ) -> Result<()> {
35 let path = format!(
36 "/integrations/provider/{ai_integration}/integration/{model_name}/prompt/{prompt_name}"
37 );
38 self.api
39 .post_no_body_no_response(ApiPath::templated(
40 &path,
41 "/integrations/provider/{name}/integration/{apiName}/prompt/{promptName}",
42 ))
43 .await
44 }
45
46 /// Delete a specific integration API.
47 ///
48 /// # Errors
49 ///
50 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
51 pub async fn delete_integration_api(
52 &self,
53 api_name: &str,
54 integration_name: &str,
55 ) -> Result<()> {
56 let path = format!("/integrations/provider/{integration_name}/integration/{api_name}");
57 self.api
58 .delete_no_content(ApiPath::templated(
59 &path,
60 "/integrations/provider/{name}/integration/{apiName}",
61 ))
62 .await
63 }
64
65 /// Delete an integration.
66 ///
67 /// # Errors
68 ///
69 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
70 pub async fn delete_integration(&self, integration_name: &str) -> Result<()> {
71 let path = format!("/integrations/provider/{integration_name}");
72 self.api
73 .delete_no_content(ApiPath::templated(&path, "/integrations/provider/{name}"))
74 .await
75 }
76
77 /// Get an integration API.
78 ///
79 /// # Errors
80 ///
81 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
82 pub async fn get_integration_api(
83 &self,
84 api_name: &str,
85 integration_name: &str,
86 ) -> Result<IntegrationApi> {
87 let path = format!("/integrations/provider/{integration_name}/integration/{api_name}");
88 self.api
89 .get(ApiPath::templated(
90 &path,
91 "/integrations/provider/{name}/integration/{apiName}",
92 ))
93 .await
94 }
95
96 /// Get all APIs for an integration.
97 ///
98 /// # Errors
99 ///
100 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
101 pub async fn get_integration_apis(
102 &self,
103 integration_name: &str,
104 ) -> Result<Vec<IntegrationApi>> {
105 let path = format!("/integrations/provider/{integration_name}/integration");
106 self.api
107 .get(ApiPath::templated(
108 &path,
109 "/integrations/provider/{name}/integration",
110 ))
111 .await
112 }
113
114 /// Get an integration.
115 ///
116 /// # Errors
117 ///
118 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
119 pub async fn get_integration(&self, integration_name: &str) -> Result<Integration> {
120 let path = format!("/integrations/provider/{integration_name}");
121 self.api
122 .get(ApiPath::templated(&path, "/integrations/provider/{name}"))
123 .await
124 }
125
126 /// Get all integrations.
127 ///
128 /// # Errors
129 ///
130 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
131 pub async fn get_integrations(&self) -> Result<Vec<Integration>> {
132 self.api.get("/integrations/provider").await
133 }
134
135 /// Get prompts associated with an integration.
136 ///
137 /// # Errors
138 ///
139 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
140 pub async fn get_prompts_with_integration(
141 &self,
142 ai_integration: &str,
143 model_name: &str,
144 ) -> Result<Vec<PromptTemplate>> {
145 let path =
146 format!("/integrations/provider/{ai_integration}/integration/{model_name}/prompt");
147 self.api
148 .get(ApiPath::templated(
149 &path,
150 "/integrations/provider/{name}/integration/{apiName}/prompt",
151 ))
152 .await
153 }
154
155 /// Get token usage for an integration API.
156 ///
157 /// # Errors
158 ///
159 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
160 pub async fn get_token_usage_for_integration(
161 &self,
162 api_name: &str,
163 integration_name: &str,
164 ) -> Result<i64> {
165 let path =
166 format!("/integrations/provider/{integration_name}/integration/{api_name}/metrics");
167 self.api
168 .get(ApiPath::templated(
169 &path,
170 "/integrations/provider/{name}/integration/{apiName}/metrics",
171 ))
172 .await
173 }
174
175 /// Get token usage for an integration provider.
176 ///
177 /// # Errors
178 ///
179 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
180 pub async fn get_token_usage_for_integration_provider(
181 &self,
182 name: &str,
183 ) -> Result<serde_json::Value> {
184 let path = format!("/integrations/provider/{name}/metrics");
185 self.api
186 .get(ApiPath::templated(
187 &path,
188 "/integrations/provider/{name}/metrics",
189 ))
190 .await
191 }
192
193 /// Save (create or update) an integration API.
194 ///
195 /// # Errors
196 ///
197 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
198 pub async fn save_integration_api(
199 &self,
200 integration_name: &str,
201 api_name: &str,
202 api_details: &IntegrationApiUpdate,
203 ) -> Result<()> {
204 let path = format!("/integrations/provider/{integration_name}/integration/{api_name}");
205 self.api
206 .put_no_response(
207 ApiPath::templated(&path, "/integrations/provider/{name}/integration/{apiName}"),
208 api_details,
209 )
210 .await
211 }
212
213 /// Save (create or update) an integration.
214 ///
215 /// # Errors
216 ///
217 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
218 pub async fn save_integration(
219 &self,
220 integration_name: &str,
221 integration_details: &IntegrationUpdate,
222 ) -> Result<()> {
223 let path = format!("/integrations/provider/{integration_name}");
224 self.api
225 .put_no_response(
226 ApiPath::templated(&path, "/integrations/provider/{name}"),
227 integration_details,
228 )
229 .await
230 }
231
232 // Tags
233
234 /// Delete a tag from an integration.
235 ///
236 /// # Errors
237 ///
238 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
239 pub async fn delete_tag_for_integration(
240 &self,
241 tags: &[MetadataTag],
242 integration_name: &str,
243 api_name: &str,
244 ) -> Result<()> {
245 let path = format!("/integrations/provider/{integration_name}/integration/{api_name}/tags");
246 self.api
247 .delete_with_body(
248 ApiPath::templated(
249 &path,
250 "/integrations/provider/{name}/integration/{apiName}/tags",
251 ),
252 tags,
253 )
254 .await
255 }
256
257 /// Delete a tag from an integration provider.
258 ///
259 /// # Errors
260 ///
261 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
262 pub async fn delete_tag_for_integration_provider(
263 &self,
264 tags: &[MetadataTag],
265 name: &str,
266 ) -> Result<()> {
267 let path = format!("/integrations/provider/{name}/tags");
268 self.api
269 .delete_with_body(
270 ApiPath::templated(&path, "/integrations/provider/{name}/tags"),
271 tags,
272 )
273 .await
274 }
275
276 /// Set tags for an integration.
277 ///
278 /// # Errors
279 ///
280 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
281 pub async fn put_tag_for_integration(
282 &self,
283 tags: &[MetadataTag],
284 integration_name: &str,
285 api_name: &str,
286 ) -> Result<()> {
287 let path = format!("/integrations/provider/{integration_name}/integration/{api_name}/tags");
288 self.api
289 .put_no_response(
290 ApiPath::templated(
291 &path,
292 "/integrations/provider/{name}/integration/{apiName}/tags",
293 ),
294 tags,
295 )
296 .await
297 }
298
299 /// Set tags for an integration provider.
300 ///
301 /// # Errors
302 ///
303 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
304 pub async fn put_tag_for_integration_provider(
305 &self,
306 tags: &[MetadataTag],
307 name: &str,
308 ) -> Result<()> {
309 let path = format!("/integrations/provider/{name}/tags");
310 self.api
311 .put_no_response(
312 ApiPath::templated(&path, "/integrations/provider/{name}/tags"),
313 tags,
314 )
315 .await
316 }
317
318 /// Get tags for an integration.
319 ///
320 /// # Errors
321 ///
322 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
323 pub async fn get_tags_for_integration(
324 &self,
325 integration_name: &str,
326 api_name: &str,
327 ) -> Result<Vec<MetadataTag>> {
328 let path = format!("/integrations/provider/{integration_name}/integration/{api_name}/tags");
329 self.api
330 .get(ApiPath::templated(
331 &path,
332 "/integrations/provider/{name}/integration/{apiName}/tags",
333 ))
334 .await
335 }
336
337 /// Get tags for an integration provider.
338 ///
339 /// # Errors
340 ///
341 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
342 pub async fn get_tags_for_integration_provider(&self, name: &str) -> Result<Vec<MetadataTag>> {
343 let path = format!("/integrations/provider/{name}/tags");
344 self.api
345 .get(ApiPath::templated(
346 &path,
347 "/integrations/provider/{name}/tags",
348 ))
349 .await
350 }
351
352 /// Get available APIs for an integration provider.
353 ///
354 /// # Errors
355 ///
356 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
357 pub async fn get_integration_available_apis(
358 &self,
359 integration_name: &str,
360 ) -> Result<Vec<String>> {
361 let path = format!("/integrations/provider/{integration_name}/models");
362 self.api
363 .get(ApiPath::templated(
364 &path,
365 "/integrations/provider/{name}/models",
366 ))
367 .await
368 }
369
370 /// Get all integration provider definitions.
371 ///
372 /// # Errors
373 ///
374 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
375 pub async fn get_integration_provider_defs(&self) -> Result<Vec<serde_json::Value>> {
376 self.api.get("/integrations/def").await
377 }
378
379 /// Get all providers and their integrations.
380 ///
381 /// # Errors
382 ///
383 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
384 pub async fn get_providers_and_integrations(&self) -> Result<serde_json::Value> {
385 self.api.get("/integrations").await
386 }
387}
388
389#[cfg(test)]
390mod tests {
391 use super::*;
392 use crate::Configuration;
393
394 #[test]
395 fn test_integration_client_creation() {
396 let config = Configuration::new("http://localhost:8080/api");
397 let api = ApiClient::new(config).unwrap();
398 let _client = IntegrationClient::new(api);
399 }
400}