openwhisk_rust/client/
openwhisk_client.rs

1use super::{
2    common::{Context, WskProperties},
3    OpenWhisk,
4};
5use crate::api::{ActionService, NamespaceService, RuleService, TriggerService};
6
7/// Representation of Openwhisk Client
8#[derive(Debug, Default, Clone)]
9pub struct OpenwhiskClient<T> {
10    /// Openwhisk client context (Properties set for the openwhisk)
11    pub context: Context,
12    /// Client represents the http client (OpenwhiskClient takes generic type for http client)
13    pub client: T,
14    /// Action endpoint to access Openwhisk API
15    actions: ActionService<T>,
16    /// triggers endpoint to access Openwhisk API
17    triggers: TriggerService<T>,
18    /// rules endpoint to access Openwhisk API
19    rules: RuleService<T>,
20    /// namespace endpoint to access Openwhisk API
21    namespaces: NamespaceService<T>,
22}
23
24impl<T: Clone> OpenwhiskClient<T>
25where
26    T: OpenWhisk + OpenWhisk<Output = T>,
27{
28    /// To set Openwhisk config for library to interact with Openwhisk API's
29    ///
30    /// # Arguments
31    /// * `config` - Can be None or Openwhisk Properties defined by User
32    ///    when None is supplied poperties are set by environment
33    ///
34    /// # Example
35    /// ```
36    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
37    /// // setting openwhisk props with user Input
38    /// let new_wsk_props = WskProperties::new(
39    ///         "your:auth_token".to_string(),
40    ///         "host".to_string(),
41    ///         true,
42    ///         "namespace".to_string()
43    ///  );
44    ///
45    /// // creating new client from using the propety
46    ///
47    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
48    /// // use initilalised client to interact with openwhisk API
49    ///
50    /// ```
51    pub fn new(config: Option<&WskProperties>) -> Self {
52        let context = Context::new(config);
53        let client = T::new_whisk_client(Some(context.is_secure()));
54        let actions = ActionService::new(client.clone(), context.clone());
55        let triggers = TriggerService::new(client.clone(), context.clone());
56        let rules = RuleService::new(client.clone(), context.clone());
57        let namespaces = NamespaceService::new(client.clone(), context.clone());
58        Self {
59            client,
60            context,
61            actions,
62            triggers,
63            rules,
64            namespaces,
65        }
66    }
67
68    /// To Access action endpoints from the Openwhisk Client using this method
69    ///
70    /// Returns ActionService
71    ///
72    /// This can be used to call underlying action service methods
73    ///
74    /// * `list`    - Lists all the actions in the namesapce
75    ///
76    /// # Example
77    /// ```
78    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
79    /// // setting openwhisk props with user Input
80    /// let new_wsk_props = WskProperties::new(
81    ///         "your:auth_token".to_string(),
82    ///         "host".to_string(),
83    ///         true,
84    ///         "namespace".to_string()
85    ///  );
86    ///
87    /// // creating new client from using the propety
88    ///
89    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
90    /// // use initilalised client to interact with openwhisk API
91    ///
92    /// // Lists the actions deployed in the openwhisk
93    /// let actions = client.actions().list().unwrap();
94    /// ```
95    ///
96    /// * `get`     - Get the action property based on the action name provided as paramter
97    ///
98    /// # Example
99    ///
100    /// ```
101    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
102    /// // setting openwhisk props with user Input
103    /// let new_wsk_props = WskProperties::new(
104    ///         "your:auth_token".to_string(),
105    ///         "host".to_string(),
106    ///         true,
107    ///         "namespace".to_string()
108    ///  );
109    ///
110    /// // creating new client from using the propety
111    ///
112    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
113    /// // use initilalised client to interact with openwhisk API
114    ///
115    /// // get the action properties which is deployed in the openwhisk
116    /// let action_property = client.actions().get("action_name").unwrap();
117    /// ```
118    ///  
119    /// * `delete`  - Delete action based on the action name
120    ///
121    /// # Example
122    ///
123    /// ```
124    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
125    /// // setting openwhisk props with user Input
126    /// let new_wsk_props = WskProperties::new(
127    ///         "your:auth_token".to_string(),
128    ///         "host".to_string(),
129    ///         true,
130    ///         "namespace".to_string()
131    ///  );
132    ///
133    /// // creating new client from using the propety
134    ///
135    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
136    /// // use initilalised client to interact with openwhisk API
137    ///
138    /// // deletes the action  which is deployed in the openwhisk
139    /// let action = client.actions().delete("action_name").unwrap();
140    ///
141    /// ```
142    ///
143    /// * `insert`  - Insert action and returns new action created
144    /// # Example
145    ///
146    /// ```
147    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
148    /// // setting openwhisk props with user Input
149    /// let new_wsk_props = WskProperties::new(
150    ///         "your:auth_token".to_string(),
151    ///         "host".to_string(),
152    ///         true,
153    ///         "namespace".to_string()
154    ///  );
155    ///
156    /// // creating new client from using the propety
157    ///
158    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
159    /// // use initilalised client to interact with openwhisk API
160    ///
161    /// // insert the action and deploys in the openwhisk
162    /// let action = client.actions().insert(action,true,true).unwrap();
163    ///
164    /// ```
165    /// * `invoke`  - Invoke action based on the action name and payload for the actions
166    ///  
167    /// # Example
168    ///
169    /// ```
170    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
171    /// // setting openwhisk props with user Input
172    /// let new_wsk_props = WskProperties::new(
173    ///         "your:auth_token".to_string(),
174    ///         "host".to_string(),
175    ///         true,
176    ///         "namespace".to_string()
177    ///  );
178    ///
179    /// // creating new client from using the propety
180    ///
181    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
182    /// // use initilalised client to interact with openwhisk API
183    ///
184    /// // invoke the action deployed in the openwhisk
185    /// let action_result = client.actions().invoke("action_name","action_payload",true,true).unwrap();
186    ///
187    /// ```
188    pub fn actions(&self) -> &ActionService<T> {
189        &self.actions
190    }
191
192    /// To Access trigger endpoints from the Openwhisk Client using this method
193    ///
194    /// Returns TriggerService
195    /// This can be used to call underlying action service methods
196    ///
197    /// * `list`    - Lists all the triggers in the namesapce
198    ///
199    /// # Example
200    /// ```
201    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
202    /// // setting openwhisk props with user Input
203    /// let new_wsk_props = WskProperties::new(
204    ///         "your:auth_token".to_string(),
205    ///         "host".to_string(),
206    ///         true,
207    ///         "namespace".to_string()
208    ///  );
209    ///
210    /// // creating new client from using the propety
211    ///
212    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
213    /// // use initilalised client to interact with openwhisk API
214    ///
215    /// // Lists the triggers deployed in the openwhisk
216    /// let triggers = client.triggers().list().unwrap();
217    /// ```
218    ///
219    /// * `get`     - Get the trigger property based on the trigger name provided as paramter
220    ///
221    /// # Example
222    ///
223    /// ```
224    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
225    /// // setting openwhisk props with user Input
226    /// let new_wsk_props = WskProperties::new(
227    ///         "your:auth_token".to_string(),
228    ///         "host".to_string(),
229    ///         true,
230    ///         "namespace".to_string()
231    ///  );
232    ///
233    /// // creating new client from using the propety
234    ///
235    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
236    /// // use initilalised client to interact with openwhisk API
237    ///
238    /// // get the trigger properties which is deployed in the openwhisk
239    /// let trigger_property = client.triggers().get("trigger_name").unwrap();
240    /// ```
241    ///  
242    /// * `delete`  - Delete trigger based on the trigger name
243    ///
244    /// # Example
245    ///
246    /// ```
247    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
248    /// // setting openwhisk props with user Input
249    /// let new_wsk_props = WskProperties::new(
250    ///         "your:auth_token".to_string(),
251    ///         "host".to_string(),
252    ///         true,
253    ///         "namespace".to_string()
254    ///  );
255    ///
256    /// // creating new client from using the propety
257    ///
258    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
259    /// // use initilalised client to interact with openwhisk API
260    ///
261    /// // deletes the trigger  which is deployed in the openwhisk
262    /// let trigger = client.triggers().delete("trigger_name").unwrap();
263    ///
264    /// ```
265    ///
266    /// * `insert`  - Insert trigger and returns new trigger created
267    /// # Example
268    ///
269    /// ```
270    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
271    /// // setting openwhisk props with user Input
272    /// let new_wsk_props = WskProperties::new(
273    ///         "your:auth_token".to_string(),
274    ///         "host".to_string(),
275    ///         true,
276    ///         "namespace".to_string()
277    ///  );
278    ///
279    /// // creating new client from using the propety
280    ///
281    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
282    /// // use initilalised client to interact with openwhisk API
283    ///
284    /// // insert the trigger and deploys in the openwhisk
285    /// let trigger = client.triggers().insert(trigger,true).unwrap();
286    ///
287    /// ```
288    ///
289    /// * `fire`  - Fires a trigger to an action
290    ///  
291    /// # Example
292    ///
293    /// ```
294    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
295    /// // setting openwhisk props with user Input
296    /// let new_wsk_props = WskProperties::new(
297    ///         "your:auth_token".to_string(),
298    ///         "host".to_string(),
299    ///         true,
300    ///         "namespace".to_string()
301    ///  );
302    ///
303    /// // creating new client from using the propety
304    ///
305    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
306    /// // use initilalised client to interact with openwhisk API
307    ///
308    /// // Fires a trigger to an action
309    /// let trigger = client.actions().fire("trigger_name",value).unwrap();
310    ///
311    /// ```
312
313    pub fn triggers(&self) -> &TriggerService<T> {
314        &self.triggers
315    }
316
317    /// To Access action endpoints from the Openwhisk Client using this method
318    ///
319    /// Returns RuleService
320    /// This can be used to call underlying action service methods
321    ///
322    /// * `list`    - Lists all the rules in the namesapce
323    ///
324    /// # Example
325    /// ```
326    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
327    /// // setting openwhisk props with user Input
328    /// let new_wsk_props = WskProperties::new(
329    ///         "your:auth_token".to_string(),
330    ///         "host".to_string(),
331    ///         true,
332    ///         "namespace".to_string()
333    ///  );
334    ///
335    /// // creating new client from using the propety
336    ///
337    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
338    /// // use initilalised client to interact with openwhisk API
339    ///
340    /// // Lists the rules deployed in the openwhisk
341    /// let rules = client.rules().list().unwrap();
342    /// ```
343    ///
344    /// * `get`     - Get the rule property based on the rule name provided as paramter
345    ///
346    /// # Example
347    ///
348    /// ```
349    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
350    /// // setting openwhisk props with user Input
351    /// let new_wsk_props = WskProperties::new(
352    ///         "your:auth_token".to_string(),
353    ///         "host".to_string(),
354    ///         true,
355    ///         "namespace".to_string()
356    ///  );
357    ///
358    /// // creating new client from using the propety
359    ///
360    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
361    /// // use initilalised client to interact with openwhisk API
362    ///
363    /// // get the rule properties which is deployed in the openwhisk
364    /// let rule_property = client.rules().get("rule_name").unwrap();
365    /// ```
366    ///  
367    /// * `delete`  - Delete rule based on the rule name
368    ///
369    /// # Example
370    ///
371    /// ```
372    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
373    /// // setting openwhisk props with user Input
374    /// let new_wsk_props = WskProperties::new(
375    ///         "your:auth_token".to_string(),
376    ///         "host".to_string(),
377    ///         true,
378    ///         "namespace".to_string()
379    ///  );
380    ///
381    /// // creating new client from using the propety
382    ///
383    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
384    /// // use initilalised client to interact with openwhisk API
385    ///
386    /// // deletes the rule  which is deployed in the openwhisk
387    /// let rule = client.rules().delete("rule_name").unwrap();
388    ///
389    /// ```
390    ///
391    /// * `setstate`  - Sets the state of the rule
392    ///  
393    /// # Example
394    ///
395    /// ```
396    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
397    /// // setting openwhisk props with user Input
398    /// let new_wsk_props = WskProperties::new(
399    ///         "your:auth_token".to_string(),
400    ///         "host".to_string(),
401    ///         true,
402    ///         "namespace".to_string()
403    ///  );
404    ///
405    /// // creating new client from using the propety
406    ///
407    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
408    /// // use initilalised client to interact with openwhisk API
409    ///
410    /// // set state for the rule deployed in the openwhisk
411    /// let rule = client.rules().setstate("rule_name","state").unwrap();
412    ///
413    /// ```
414    pub fn rules(&self) -> &RuleService<T> {
415        &self.rules
416    }
417
418    /// To Access namespace endpoints from the Openwhisk Client using this method
419    ///
420    /// Returns NamespaceService
421    ///
422    /// This can be used to call underlying action service methods
423    ///
424    /// * `list`    - Lists all the actions in the namesapce
425    ///
426    /// # Example
427    /// ```
428    /// use openwhisk_rust::{NativeClient, OpenwhiskClient, WskProperties};
429    /// // setting openwhisk props with user Input
430    /// let new_wsk_props = WskProperties::new(
431    ///         "your:auth_token".to_string(),
432    ///         "host".to_string(),
433    ///         true,
434    ///         "namespace".to_string()
435    ///  );
436    ///
437    /// // creating new client from using the propety
438    ///
439    /// let client = OpenwhiskClient::<NativeClient>::new(Some(&new_wsk_props));
440    /// // use initilalised client to interact with openwhisk API
441    ///
442    /// // Lists the namespaces available in the openwhisk
443    /// let namespaces = client.namespaces().list().unwrap();
444    /// ```
445    ///
446    pub fn namespaces(&self) -> &NamespaceService<T> {
447        &self.namespaces
448    }
449}