openwhisk_rust/client/
common.rs

1use http::StatusCode;
2use serde::{Deserialize, Serialize};
3use std::env;
4use std::fmt::Debug;
5
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct WhiskError {
8    pub code: String,
9    pub error: String,
10}
11/// Representation  of OpenWhisk Properties
12#[derive(Debug, Clone, Default, Serialize, Deserialize)]
13pub struct WskProperties {
14    /// Auth_token - `:` seperated username and passeord
15    pub auth_token: String,
16    /// Api Host to interact with Openwhisk API
17    pub host: String,
18    /// Version
19    #[serde(default = "default")]
20    pub version: String,
21    /// Toggle to set secure or insecure connection
22    pub insecure: bool,
23    /// Namespace for the endpoint
24    pub namespace: String,
25    /// Verbose - Toggle to enable it
26    #[serde(default = "bool::default")]
27    pub verbose: bool,
28    /// Debug - Toggle to ennable it
29    #[serde(default = "bool::default")]
30    pub debug: bool,
31}
32
33fn default() -> String {
34    "v1".to_string()
35}
36
37/// Context which used to set whisk properties
38#[derive(Debug, Deserialize, Serialize, Clone, Default)]
39pub struct Context {
40    /// API Host Url
41    host: String,
42    /// Namespace where actions,triggrs, rules exists
43    namespace: String,
44    /// Toggle to make secure and inscure connection (Set true or false)
45    insecure: bool,
46    /// Username for Authentication which is set by auth_token
47    username: String,
48    /// Passwod for Authentication which is set by auth_token
49    password: String,
50    /// Verion
51    version: String,
52}
53
54impl WskProperties {
55    /// New Creates Openwhisk properties
56    ///
57    /// # Arguments
58    /// * `auth_token`  - The authorization token
59    /// * `host`        - The API url
60    /// * `insecure`    - Toggle for secure connection
61    /// * `namespace`   - Name of the namespace
62    ///
63    /// # Example
64    ///
65    /// ```
66    /// use openwhisk_rust::WskProperties;
67    ///
68    /// let new_wsk_property = WskProperties::new(
69    /// "your:auth_token".to_string(),
70    /// "host".to_string(),
71    /// true,
72    /// "namespace".to_string()
73    /// );
74    ///
75    /// ```
76    pub fn new(auth_token: String, host: String, insecure: bool, namespace: String) -> Self {
77        Self {
78            auth_token,
79            host,
80            insecure,
81            namespace,
82            version: default(),
83            ..Default::default()
84        }
85    }
86
87    /// To set Verbose, Version and Debug
88    ///
89    /// # Arguments
90    /// * `debug`   - Bool to toggle debug
91    /// * `verbose` - Bool to toggle verbose
92    /// * `version` - Version of wsk properties
93    ///
94    /// # Example
95    ///
96    /// ```
97    /// use openwhisk_rust::WskProperties;
98    ///
99    /// let new_wsk_property = WskProperties::new(
100    /// "your:auth_token".to_string(),
101    /// "host".to_string(),
102    /// true,
103    /// "namespace".to_string()
104    /// );
105    ///
106    /// new_wsk_property.set_verbose_debug_version(false,false,"v2".to_string());
107    ///
108    /// ```
109    pub fn set_verbose_debug_version(&self, debug: bool, verbose: bool, version: String) -> Self {
110        Self {
111            auth_token: self.auth_token.clone(),
112            host: self.host.clone(),
113            version,
114            insecure: self.insecure,
115            namespace: self.namespace.clone(),
116            verbose,
117            debug,
118        }
119    }
120}
121
122/// Trait Openwhisk
123pub trait OpenWhisk {
124    type Output;
125    /// creates new openwhisk client
126    fn new_whisk_client(insecure: Option<bool>) -> Self::Output;
127}
128
129impl Context {
130    /// Creates and retruns context based on the Whisk Properties supplied
131    ///
132    /// # Arguments
133    /// * `wskprops` - Option of WhiskProperties
134    pub fn new(wskprops: Option<&WskProperties>) -> Context {
135        let api_key = if env::var("__OW_API_KEY").is_ok() {
136            env::var("__OW_API_KEY").unwrap()
137        } else {
138            match wskprops {
139                Some(wskprops) => wskprops.auth_token.clone(),
140                None => "test:test".to_string(),
141            }
142        };
143        let auth: Vec<&str> = api_key.split(':').collect();
144        let host = if env::var("__OW_API_HOST").is_ok() {
145            env::var("__OW_API_HOST").unwrap()
146        } else {
147            match wskprops {
148                Some(wskprops) => wskprops.host.clone(),
149                None => "host.docker.internal".to_string(),
150            }
151        };
152        let namespace = if env::var("__OW_NAMESPACE").is_ok() {
153            env::var("__OW_NAMESPACE").unwrap()
154        } else {
155            match wskprops {
156                Some(wskprops) => wskprops.namespace.clone(),
157                None => "guest".to_string(),
158            }
159        };
160
161        let connectiontype = match wskprops {
162            Some(config) => config.insecure,
163            None => false,
164        };
165
166        let version = match wskprops {
167            Some(config) => config.version.clone(),
168            None => "v1".to_string(),
169        };
170
171        Context {
172            host,
173            namespace,
174            insecure: connectiontype,
175            username: auth[0].to_string(),
176            password: auth[1].to_string(),
177            version,
178        }
179    }
180
181    /// Returns namspace value
182    pub fn namespace(&self) -> &str {
183        &self.namespace
184    }
185
186    /// Returns secure value
187    pub fn is_secure(&self) -> bool {
188        self.insecure
189    }
190
191    /// Returns tuple containing username and password
192    pub fn auth(&self) -> (&str, &str) {
193        (&self.username, &self.password)
194    }
195
196    /// Returns host
197    pub fn host(&self) -> &str {
198        &self.host
199    }
200}
201
202pub fn whisk_errors(code: StatusCode, message: String) -> String {
203    format!(": Error -> [ Status :{}, Message : {} ]", code, message)
204}