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
use super::common::{whisk_errors, OpenWhisk};
use crate::api::{HttpMethods, Service};
use bytes::Bytes;
use http::{HeaderMap, Request, StatusCode};
use serde_json::{Error, Value};
use wasi_experimental_http::request as wasi_request;
#[derive(Debug, Default, Clone)]
pub struct WasmClient {
headers: http::HeaderMap,
}
impl OpenWhisk for WasmClient {
type Output = WasmClient;
fn new_whisk_client(insecure: Option<bool>) -> Self::Output {
let mut header_map = HeaderMap::new();
match insecure {
Some(x) => match x {
true => header_map.insert("Upgrade-Insecure-Requests", "1".parse().unwrap()),
false => header_map.insert("upgrade-insecure-requests", "0".parse().unwrap()),
},
None => todo!(),
};
WasmClient {
headers: header_map,
}
}
}
impl Service for WasmClient {
type Output = Request<Option<Bytes>>;
fn new_request(
&self,
method: Option<HttpMethods>,
url: &str,
user_auth: Option<(&str, &str)>,
body: Option<Value>,
) -> Result<Self::Output, String> {
let mut req = http::request::Builder::new().header("Content-Type", "application/json");
for (key, value) in self.headers.iter() {
req = req.header(key, value);
}
let body = Bytes::from(serde_json::to_vec(&body).unwrap());
match user_auth {
Some(auth) => {
let user = auth.0;
let pass = auth.1;
let bse64_encode = base64::encode(format!("{}:{}", user, pass));
match method {
Some(http_methods) => match http_methods {
HttpMethods::GET => {
let req = req
.header("Authorization", format!("Basic {}", bse64_encode))
.method("GET")
.uri(url)
.body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
HttpMethods::PUT => {
let req = req
.header("Authorization", format!("Basic {}", bse64_encode))
.method("PUT")
.uri(url)
.body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
HttpMethods::POST => {
let req = req
.header("Authorization", format!("Basic {}", bse64_encode))
.method("POST")
.uri(url)
.body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
HttpMethods::DELETE => {
let req = req
.header("Authorization", format!("Basic {}", bse64_encode))
.method("DELETE")
.uri(url)
.body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
},
None => Err("Falied to create request".to_string()),
}
}
None => match method {
Some(http_methods) => match http_methods {
HttpMethods::GET => {
let req = req.method("GET").uri(url).body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
HttpMethods::PUT => {
let req = req.method("PUT").uri(url).body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
HttpMethods::POST => {
let req = req.method("POST").uri(url).body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
HttpMethods::DELETE => {
let req = req.method("DELETE").uri(url).body(Some(body));
match req {
Ok(req) => Ok(req),
Err(error) => Err(format!("{}", error)),
}
}
},
None => Err("Falied to create request".to_string()),
},
}
}
fn invoke_request(&self, request: Self::Output) -> Result<Value, String> {
match wasi_request(request) {
Ok(mut response) => match response.status_code {
StatusCode::OK => match response.body_read_all() {
Ok(response) => match String::from_utf8(response) {
Ok(response) => {
let response_to_value: Result<Value, Error> =
serde_json::from_str(&response);
match response_to_value {
Ok(value) => Ok(value),
Err(error) => Err(error.to_string()),
}
}
Err(error) => Err(error.to_string()),
},
Err(error) => return Err(format!("{}", error)),
},
_ => {
let code = response.status_code;
let error = response.body_read_all().unwrap();
let s = match std::str::from_utf8(&error) {
Ok(v) => v,
Err(error) => return Err(format!("{}", error)),
};
Err(whisk_errors(code, s.to_string()))
}
},
Err(error) => Err(error.to_string()),
}
}
}