Skip to main content

conductor/client/
schema_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::SchemaDef;
7
8/// Client for managing schema definitions.
9#[derive(Clone)]
10pub struct SchemaClient {
11    api: ApiClient,
12}
13
14impl SchemaClient {
15    /// Create a new schema client.
16    #[must_use]
17    pub fn new(api: ApiClient) -> Self {
18        Self { api }
19    }
20
21    /// Register a new schema.
22    ///
23    /// # Errors
24    ///
25    /// 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.
26    pub async fn register_schema(&self, schema: &SchemaDef) -> Result<()> {
27        self.api.post_no_response("/schema", schema).await
28    }
29
30    /// Get a schema by name and version.
31    ///
32    /// # Errors
33    ///
34    /// 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.
35    pub async fn get_schema(&self, schema_name: &str, version: i32) -> Result<SchemaDef> {
36        let path = format!("/schema/{schema_name}");
37        self.api
38            .get_with_params(
39                ApiPath::templated(&path, "/schema/{schemaName}"),
40                &[("version", &version.to_string())],
41            )
42            .await
43    }
44
45    /// Get all schemas.
46    ///
47    /// # Errors
48    ///
49    /// 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.
50    pub async fn get_all_schemas(&self) -> Result<Vec<SchemaDef>> {
51        self.api.get("/schema").await
52    }
53
54    /// Delete a schema by name and version.
55    ///
56    /// # Errors
57    ///
58    /// 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.
59    pub async fn delete_schema(&self, schema_name: &str, version: i32) -> Result<()> {
60        let path = format!("/schema/{schema_name}");
61        self.api
62            .delete_with_params(
63                ApiPath::templated(&path, "/schema/{schemaName}"),
64                &[("version", &version.to_string())],
65            )
66            .await
67    }
68
69    /// Delete all versions of a schema by name.
70    ///
71    /// # Errors
72    ///
73    /// 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.
74    pub async fn delete_schema_by_name(&self, schema_name: &str) -> Result<()> {
75        let path = format!("/schema/{schema_name}/all");
76        self.api
77            .delete_no_content(ApiPath::templated(&path, "/schema/{schemaName}/all"))
78            .await
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85    use crate::Configuration;
86
87    #[test]
88    fn test_schema_client_creation() {
89        let config = Configuration::new("http://localhost:8080/api");
90        let api = ApiClient::new(config).unwrap();
91        let _client = SchemaClient::new(api);
92    }
93}