openwhisk_client_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
12/// Representation of OpenWhisk Properties
13#[derive(Debug, Clone, Default, Serialize, Deserialize)]
14pub struct WskProperties {
15    /// Auth_token - `:` separated username and password
16    pub auth_token: String,
17    /// Api Host to interact with OpenWhisk API
18    pub host: String,
19    /// Version
20    #[serde(default = "default")]
21    pub version: String,
22    /// Toggle to set secure or insecure connection
23    pub insecure: bool,
24    /// Namespace for the endpoint
25    pub namespace: String,
26    /// Verbose - Toggle to enable it
27    #[serde(default = "bool::default")]
28    pub verbose: bool,
29    /// Debug - Toggle to enable it
30    #[serde(default = "bool::default")]
31    pub debug: bool,
32}
33
34fn default() -> String {
35    "v1".to_string()
36}
37
38/// Context used to set Whisk properties
39#[derive(Debug, Deserialize, Serialize, Clone, Default)]
40pub struct Context {
41    /// API Host Url
42    host: String,
43    /// Namespace where actions, triggers, rules exist
44    namespace: String,
45    /// Toggle to make secure and insecure connection (Set true or false)
46    insecure: bool,
47    /// Username for Authentication which is set by auth_token
48    username: String,
49    /// Password for Authentication which is set by auth_token
50    password: String,
51    /// Version
52    version: String,
53}
54
55impl WskProperties {
56    /// New Creates OpenWhisk properties
57    ///
58    /// # Arguments
59    /// * `auth_token`  - The authorization token
60    /// * `host`        - The API url
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    /// "namespace".to_string()
72    /// );
73    ///
74    /// ```
75    pub fn new(auth_token: String, host: String, namespace: String) -> Self {
76        Self {
77            auth_token,
78            host,
79            insecure: false,
80            namespace,
81            version: default(),
82            ..Default::default()
83        }
84    }
85
86    /// To set Debug
87    ///
88    /// # Arguments
89    /// * `debug`   - Bool to toggle debug
90    ///
91    /// # Example
92    ///
93    /// ```
94    /// use openwhisk_rust::WskProperties;
95    ///
96    /// let wsk_property = WskProperties::new(
97    /// "your:auth_token".to_string(),
98    /// "host".to_string(),
99    /// "namespace".to_string()
100    /// ).set_debug(false);
101    ///
102    /// ```
103    pub fn set_debug(mut self, debug: bool) -> Self {
104        self.debug = debug;
105
106        self
107    }
108
109    /// To set Verbose
110    ///
111    /// # Arguments
112    /// * `verbose` - Bool to toggle verbose
113    ///
114    /// # Example
115    ///
116    /// ```
117    /// use openwhisk_rust::WskProperties;
118    ///
119    /// let wsk_property = WskProperties::new(
120    /// "your:auth_token".to_string(),
121    /// "host".to_string(),
122    /// "namespace".to_string()
123    /// ).set_verbose(false);
124    ///
125    /// ```
126    pub fn set_verbose(mut self, verbose: bool) -> Self {
127        self.verbose = verbose;
128
129        self
130    }
131
132    /// To set Version
133    ///
134    /// # Arguments
135    /// * `version` - Version of Wsk properties
136    ///
137    /// # Example
138    ///
139    /// ```
140    /// use openwhisk_rust::WskProperties;
141    ///
142    /// let wsk_property = WskProperties::new(
143    /// "your:auth_token".to_string(),
144    /// "host".to_string(),
145    /// "namespace".to_string()
146    /// ).set_version("v2".to_string());
147    ///
148    /// ```
149    pub fn set_version(mut self, version: String) -> Self {
150        self.version = version;
151
152        self
153    }
154
155    /// To set client to bypass cerificate check primarily useful if you are using the OpenWhisk API over HTTPS and the API endpoint is using a self-signed or invalid certificate.
156    ///
157    /// # Arguments
158    /// * `bypass`   - Bool to toggle bypass cerificate check
159    ///
160    /// # Example
161    ///
162    /// ```
163    /// use openwhisk_rust::WskProperties;
164    ///
165    /// let new_wsk_property = WskProperties::new(
166    /// "your:auth_token".to_string(),
167    /// "host".to_string(),
168    /// "namespace".to_string()
169    /// ).set_bypass_cerificate_check(true);
170    ///
171    /// ```
172    pub fn set_bypass_cerificate_check(mut self, bypass: bool) -> Self {
173        self.insecure = bypass;
174
175        self
176    }
177}
178
179/// Trait OpenWhisk
180pub trait OpenWhisk {
181    type Output;
182    /// Creates a new OpenWhisk client
183    fn new_whisk_client(insecure: Option<bool>) -> Self::Output;
184}
185
186impl Context {
187    /// Creates and returns context based on the Whisk Properties supplied
188    ///
189    /// # Arguments
190    /// * `wskprops` - Option of WhiskProperties
191    pub fn new(wskprops: Option<&WskProperties>) -> Context {
192        let api_key = if env::var("__OW_API_KEY").is_ok() {
193            env::var("__OW_API_KEY").unwrap()
194        } else {
195            match wskprops {
196                Some(wskprops) => wskprops.auth_token.clone(),
197                None => "test:test".to_string(),
198            }
199        };
200        let auth: Vec<&str> = api_key.split(':').collect();
201        let host = if env::var("__OW_API_HOST").is_ok() {
202            env::var("__OW_API_HOST").unwrap()
203        } else {
204            match wskprops {
205                Some(wskprops) => wskprops.host.clone(),
206                None => "host.docker.internal".to_string(),
207            }
208        };
209        let namespace = if env::var("__OW_NAMESPACE").is_ok() {
210            env::var("__OW_NAMESPACE").unwrap()
211        } else {
212            match wskprops {
213                Some(wskprops) => wskprops.namespace.clone(),
214                None => "guest".to_string(),
215            }
216        };
217
218        let connection_type = match wskprops {
219            Some(config) => config.insecure,
220            None => false,
221        };
222
223        let version = match wskprops {
224            Some(config) => config.version.clone(),
225            None => "v1".to_string(),
226        };
227
228        Context {
229            host,
230            namespace,
231            insecure: connection_type,
232            username: auth[0].to_string(),
233            password: auth[1].to_string(),
234            version,
235        }
236    }
237
238    /// Returns namespace value
239    pub fn namespace(&self) -> &str {
240        &self.namespace
241    }
242
243    /// Returns secure value
244    pub fn is_secure(&self) -> bool {
245        self.insecure
246    }
247
248    /// Returns tuple containing username and password
249    pub fn auth(&self) -> (&str, &str) {
250        (&self.username, &self.password)
251    }
252
253    /// Returns host
254    pub fn host(&self) -> &str {
255        &self.host
256    }
257}
258
259pub fn whisk_errors(code: StatusCode, message: String) -> String {
260    format!(": Error -> [ Status :{}, Message : {} ]", code, message)
261}