1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use crate::client::Context;
use derive_new::new;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{traits::Service, HttpMethods, KeyValue, Limits, ACTION_ENDPOINT, NAMESPACE_ENDPOINT};

#[derive(new, Debug,Default, Deserialize, Serialize, Clone)]
pub struct ActionService<T> {
    client: T,
    context: Context,
}

#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
pub struct Action {
    #[serde(default)]
    pub namespace: String,
    #[serde(default)]
    pub name: String,
    #[serde(skip_serializing)]
    pub version: String,
    #[serde(skip_serializing)]
    pub limits: Limits,
    pub exec: Exec,
    #[serde(default)]
    #[serde(skip_serializing)]
    pub error: String,
    #[serde(skip_serializing)]
    pub publish: bool,
    #[serde(skip_serializing)]
    pub updated: i64,
    pub annotations: Vec<KeyValue>,
}
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq)]
pub struct Exec {
    #[serde(default)]
    pub kind: String,
    #[serde(default)]
    pub code: String,
    #[serde(default)]
    pub image: String,
    #[serde(default)]
    pub init: String,
    #[serde(default)]
    pub main: String,
    #[serde(default)]
    pub components: Vec<String>,
    #[serde(default)]
    pub binary: bool,
}

impl<T> ActionService<T>
where
    T: Service,
{
    pub fn list(&self) -> Result<Vec<Action>, String> {
        let url = format!(
            "{}/api/v1/{}/{}/{}",
            self.context.host(),
            NAMESPACE_ENDPOINT,
            self.context.namespace(),
            ACTION_ENDPOINT
        );

        let user_auth = self.context.auth();
        let user = user_auth.0;
        let pass = user_auth.1;

        let request = self
            .client
            .new_request(HttpMethods::GET, url.as_str(), Some((user, pass)), None)
            .unwrap();

        match self.client.invoke_request(request) {
            Ok(x) => match serde_json::from_value(x) {
                Ok(actions) => Ok(actions),
                Err(err) => Err(format!("Failed to deserailize actions {}", err)),
            },
            Err(x) => Err(format!("Failed to fetch the list of actions {}", x)),
        }
    }

    pub fn get(&self, action_name: &str, fetch_code: bool) -> Result<Action, String> {
        let url = format!(
            "{}/api/v1/{}/{}/{}/{}?code={}",
            self.context.host(),
            NAMESPACE_ENDPOINT,
            self.context.namespace(),
            ACTION_ENDPOINT,
            action_name,
            fetch_code
        );

        let user_auth = self.context.auth();
        let user = user_auth.0;
        let pass = user_auth.1;

        let request = self
            .client
            .new_request(HttpMethods::GET, url.as_str(), Some((user, pass)), None)
            .unwrap();

        match self.client.invoke_request(request) {
            Ok(x) => match serde_json::from_value(x) {
                Ok(actions) => Ok(actions),
                Err(err) => Err(format!("Failed to deserailize actions {}", err)),
            },
            Err(x) => Err(format!("Failed to get action properties {}", x)),
        }
    }

    pub fn delete(&self, action_name: &str) -> Result<Action, String> {
        let url = format!(
            "{}/api/v1/{}/{}/{}/{}?code=false",
            self.context.host(),
            NAMESPACE_ENDPOINT,
            self.context.namespace(),
            ACTION_ENDPOINT,
            action_name,
        );

        let user_auth = self.context.auth();
        let user = user_auth.0;
        let pass = user_auth.1;

        let request = self
            .client
            .new_request(HttpMethods::DELETE, url.as_str(), Some((user, pass)), None)
            .unwrap();

        match self.client.invoke_request(request) {
            Ok(x) => match serde_json::from_value(x) {
                Ok(actions) => Ok(actions),
                Err(err) => Err(format!("Failed to deserailize actions {}", err)),
            },
            Err(x) => Err(format!("Failed to get action properties {}", x)),
        }
    }

    pub fn insert(&self, action: &Action, overwrite: bool) -> Result<Action, String> {
        let url = format!(
            "{}/api/v1/{}/{}/{}/{}?overwrite={}",
            self.context.host(),
            NAMESPACE_ENDPOINT,
            self.context.namespace(),
            ACTION_ENDPOINT,
            action.name,
            overwrite,
        );

        let user_auth = self.context.auth().clone();
        let user = user_auth.0;
        let pass = user_auth.1;

        let body = serde_json::to_value(&action).unwrap();

        let request = self
            .client
            .new_request(
                HttpMethods::PUT,
                url.as_str(),
                Some((user, pass)),
                Some(body),
            )
            .unwrap();

        match self.client.invoke_request(request) {
            Ok(x) => match serde_json::from_value(x) {
                Ok(actions) => Ok(actions),
                Err(err) => Err(format!("Failed to deserailize actions {}", err)),
            },
            Err(x) => Err(format!("Failed to get action properties {}", x)),
        }
    }
    pub fn invoke(
        &self,
        action_name: &str,
        payload: Value,
        blocking: bool,
        result: bool,
    ) -> Result<Value, String> {
        let url = format!(
            "{}/api/v1/{}/{}/{}/{}?blocking={}&result={}",
            self.context.host(),
            NAMESPACE_ENDPOINT,
            self.context.namespace(),
            ACTION_ENDPOINT,
            action_name,
            blocking,
            result
        );
        let user_auth = self.context.auth();
        let user = user_auth.0;
        let pass = user_auth.1;

        let request = self
            .client
            .new_request(
                HttpMethods::POST,
                url.as_str(),
                Some((user, pass)),
                Some(payload),
            )
            .unwrap();

        match self.client.invoke_request(request) {
            Ok(x) => match serde_json::from_value(x) {
                Ok(actions) => Ok(actions),
                Err(err) => Err(format!("Failed to deserailize actions {}", err)),
            },
            Err(x) => Err(format!("Failed to invoke action {}", x)),
        }
    }
}