wadm_client/
topics.rs

1use wadm_types::api::{DEFAULT_WADM_TOPIC_PREFIX, WADM_STATUS_API_PREFIX};
2
3/// A generator that uses various config options to generate the proper topic names for the wadm API
4pub struct TopicGenerator {
5    topic_prefix: String,
6    model_prefix: String,
7}
8
9impl TopicGenerator {
10    /// Creates a new topic generator with a lattice ID and an optional API prefix
11    pub fn new(lattice: &str, prefix: Option<&str>) -> TopicGenerator {
12        let topic_prefix = format!(
13            "{}.{}",
14            prefix.unwrap_or(DEFAULT_WADM_TOPIC_PREFIX),
15            lattice
16        );
17        let model_prefix = format!("{}.model", topic_prefix);
18        TopicGenerator {
19            topic_prefix,
20            model_prefix,
21        }
22    }
23
24    /// Returns the full prefix for the topic, including the API prefix and the lattice ID
25    pub fn prefix(&self) -> &str {
26        &self.topic_prefix
27    }
28
29    /// Returns the full prefix for model operations (currently the only operations supported in the
30    /// API)
31    pub fn model_prefix(&self) -> &str {
32        &self.model_prefix
33    }
34
35    /// Returns the full topic for a model put operation
36    pub fn model_put_topic(&self) -> String {
37        format!("{}.put", self.model_prefix())
38    }
39
40    /// Returns the full topic for a model get operation
41    pub fn model_get_topic(&self, model_name: &str) -> String {
42        format!("{}.get.{model_name}", self.model_prefix())
43    }
44
45    /// Returns the full topic for a model delete operation
46    pub fn model_delete_topic(&self, model_name: &str) -> String {
47        format!("{}.del.{model_name}", self.model_prefix())
48    }
49
50    /// Returns the full topic for a model list operation
51    pub fn model_list_topic(&self) -> String {
52        format!("{}.list", self.model_prefix())
53    }
54
55    /// Returns the full topic for listing the versions of a model
56    pub fn model_versions_topic(&self, model_name: &str) -> String {
57        format!("{}.versions.{model_name}", self.model_prefix())
58    }
59
60    /// Returns the full topic for a model deploy operation
61    pub fn model_deploy_topic(&self, model_name: &str) -> String {
62        format!("{}.deploy.{model_name}", self.model_prefix())
63    }
64
65    /// Returns the full topic for a model undeploy operation
66    pub fn model_undeploy_topic(&self, model_name: &str) -> String {
67        format!("{}.undeploy.{model_name}", self.model_prefix())
68    }
69
70    /// Returns the full topic for getting a model status
71    pub fn model_status_topic(&self, model_name: &str) -> String {
72        format!("{}.status.{model_name}", self.model_prefix())
73    }
74
75    /// Returns the full topic for WADM status subscriptions
76    pub fn wadm_status_topic(&self, app_name: &str) -> String {
77        // Extract just the lattice name from topic_prefix
78        let lattice = self.topic_prefix.split('.').last().unwrap_or("default");
79        format!("{}.{}.{}", WADM_STATUS_API_PREFIX, lattice, app_name)
80    }
81}