openwhisk_rust/client/
common.rs1use 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#[derive(Debug, Clone, Default, Serialize, Deserialize)]
13pub struct WskProperties {
14 pub auth_token: String,
16 pub host: String,
18 #[serde(default = "default")]
20 pub version: String,
21 pub insecure: bool,
23 pub namespace: String,
25 #[serde(default = "bool::default")]
27 pub verbose: bool,
28 #[serde(default = "bool::default")]
30 pub debug: bool,
31}
32
33fn default() -> String {
34 "v1".to_string()
35}
36
37#[derive(Debug, Deserialize, Serialize, Clone, Default)]
39pub struct Context {
40 host: String,
42 namespace: String,
44 insecure: bool,
46 username: String,
48 password: String,
50 version: String,
52}
53
54impl WskProperties {
55 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 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
122pub trait OpenWhisk {
124 type Output;
125 fn new_whisk_client(insecure: Option<bool>) -> Self::Output;
127}
128
129impl Context {
130 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 pub fn namespace(&self) -> &str {
183 &self.namespace
184 }
185
186 pub fn is_secure(&self) -> bool {
188 self.insecure
189 }
190
191 pub fn auth(&self) -> (&str, &str) {
193 (&self.username, &self.password)
194 }
195
196 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}