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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use std::error::Error;
use reqwest::header::{HeaderMap, HeaderValue};
use super::executor::RawExecResponse;
use super::ExecResponse;
use super::ExecResult;
use super::Executor;
use super::Runtime;
/// A client used to send requests to Piston.
#[derive(Debug, Clone)]
pub struct Client {
/// The base url for Piston.
url: String,
/// The reqwest client to use.
client: reqwest::Client,
/// The headers to send with each request.
headers: HeaderMap,
}
impl Default for Client {
/// Creates a new client. Alias for [`Client::new`].
///
/// # Returns
/// - [`Client`] - The new Client.
///
/// # Example
/// ```
/// let client = piston_rs::Client::default();
///
/// assert!(client.get_headers().contains_key("Accept"));
/// assert!(client.get_headers().contains_key("User-Agent"));
/// assert!(!client.get_headers().contains_key("Authorization"));
/// assert_eq!(client.get_url(), "https://emkc.org/api/v2/piston".to_string());
/// ```
fn default() -> Self {
Self::new()
}
}
impl Client {
/// Creates a new client.
///
/// # Returns
/// - [`Client`] - The new Client.
///
/// # Example
/// ```
/// let client = piston_rs::Client::new();
///
/// assert!(client.get_headers().contains_key("Accept"));
/// assert!(client.get_headers().contains_key("User-Agent"));
/// assert!(!client.get_headers().contains_key("Authorization"));
/// ```
pub fn new() -> Self {
Self {
url: "https://emkc.org/api/v2/piston".to_string(),
client: reqwest::Client::new(),
headers: Self::generate_headers(None),
}
}
/// Creates a new Client with a url that runs the piston code execution engine.
///
/// This makes it possible to interact with a self-hosted instance of piston.
///
/// # Arguments
/// - `url` - The url to use as the underlying piston backend.
///
/// # Returns
/// - [`Client`] - The new Client.
///
/// # Example
/// ```
/// let client = piston_rs::Client::with_url("http://localhost:3000");
/// assert_eq!(client.get_url(), "http://localhost:3000");
/// ```
pub fn with_url(url: &str) -> Self {
Self {
url: url.to_string(),
client: reqwest::Client::new(),
headers: Self::generate_headers(None),
}
}
/// Creates a new client, with an api key.
///
/// # Arguments
/// - `key` - The api key to use.
///
/// # Returns
/// - [`Client`] - The new Client.
///
/// # Example
/// ```
/// let client = piston_rs::Client::with_key("123abc");
///
/// assert!(client.get_headers().contains_key("Authorization"));
/// assert_eq!(client.get_headers().get("Authorization").unwrap(), "123abc");
/// ```
pub fn with_key(key: &str) -> Self {
Self {
url: "https://emkc.org/api/v2/piston".to_string(),
client: reqwest::Client::new(),
headers: Self::generate_headers(Some(key)),
}
}
/// Creates a new Client using a url and an api key.
///
/// # Arguments
/// - `url` - The url to use as the underlying piston backend.
/// - `key` - The api key to use.
///
/// # Returns
/// - [`Client`] - The new Client.
///
/// # Example
/// ```
/// let client = piston_rs::Client::with_url_and_key("http://localhost:3000", "123abc");
/// assert_eq!(client.get_url(), "http://localhost:3000");
/// assert!(client.get_headers().contains_key("Authorization"));
/// assert_eq!(client.get_headers().get("Authorization").unwrap(), "123abc");
/// ```
pub fn with_url_and_key(url: &str, key: &str) -> Self {
Self {
url: url.to_string(),
client: reqwest::Client::new(),
headers: Self::generate_headers(Some(key)),
}
}
/// The base url for the Piston V2 API that is being used by this client.
///
/// # Returns
///
/// - [`String`] - The requested url.
///
/// # Example
/// ```
/// let client = piston_rs::Client::new();
///
/// assert_eq!(client.get_url(), "https://emkc.org/api/v2/piston".to_string());
/// ```
pub fn get_url(&self) -> String {
self.url.clone()
}
/// The headers being used by this client.
///
/// # Returns
///
/// - [`HeaderMap`] - A map of Header key, value pairs.
///
/// # Example
/// ```
/// let client = piston_rs::Client::new();
/// let headers = client.get_headers();
///
/// assert_eq!(headers.get("Accept").unwrap(), "application/json");
/// ```
pub fn get_headers(&self) -> HeaderMap {
self.headers.clone()
}
/// Generates the headers the client should use.
///
/// # Returns
///
/// - [`HeaderMap`] - A map of Header key, value pairs.
///
/// # Example
/// ```ignore # Fails to compile (private function)
/// let headers = piston_rs::Client::generate_headers(None);
///
/// assert!(!headers.contains_key("Authorization"));
/// assert_eq!(headers.get("Accept").unwrap(), "application/json");
/// assert_eq!(headers.get("User-Agent").unwrap(), "piston-rs");
///
/// let headers = piston_rs::Client::generate_headers(Some("123abc"));
///
/// assert_eq!(headers.get("Authorization").unwrap(), "123abc");
/// assert_eq!(headers.get("Accept").unwrap(), "application/json");
/// assert_eq!(headers.get("User-Agent").unwrap(), "piston-rs");
/// ```
fn generate_headers(key: Option<&str>) -> HeaderMap {
let mut headers = HeaderMap::with_capacity(3);
headers.insert("Accept", HeaderValue::from_str("application/json").unwrap());
headers.insert("User-Agent", HeaderValue::from_str("piston-rs").unwrap());
if let Some(k) = key {
headers.insert("Authorization", HeaderValue::from_str(k).unwrap());
};
headers
}
/// Fetches the runtimes from Piston. **This is an http request**.
///
/// # Returns
/// - [`Result<Vec<Runtime>, Box<dyn Error>>`] - The available
/// runtimes or the error, if any.
///
/// # Example
/// ```no_run
/// # #[tokio::test]
/// # async fn test_fetch_runtimes() {
/// let client = piston_rs::Client::new();
///
/// if let Ok(runtimes) = client.fetch_runtimes().await {
/// assert!(!runtimes.is_empty());
/// } else {
/// // There was an error contacting Piston.
/// }
/// # }
/// ```
pub async fn fetch_runtimes(&self) -> Result<Vec<Runtime>, Box<dyn Error>> {
let endpoint = format!("{}/runtimes", self.url);
let runtimes = self
.client
.get(endpoint)
.headers(self.headers.clone())
.send()
.await?
.json::<Vec<Runtime>>()
.await?;
Ok(runtimes)
}
/// Executes code using a given executor. **This is an http
/// request**.
///
/// # Arguments
/// - `executor` - The executor to use.
///
/// # Returns
/// - [`Result<ExecutorResponse, Box<dyn Error>>`] - The response
/// from Piston or the error, if any.
///
/// # Example
/// ```no_run
/// # #[tokio::test]
/// # async fn test_execute() {
/// let client = piston_rs::Client::new();
/// let executor = piston_rs::Executor::new()
/// .set_language("rust")
/// .set_version("1.50.0")
/// .add_file(piston_rs::File::default().set_content(
/// "fn main() { println!(\"42\"); }",
/// ));
///
/// if let Ok(response) = client.execute(&executor).await {
/// assert!(response.compile.is_some());
/// assert!(response.run.is_ok());
/// assert!(response.is_ok());
/// } else {
/// // There was an error contacting Piston.
/// }
/// # }
/// ```
pub async fn execute(&self, executor: &Executor) -> Result<ExecResponse, Box<dyn Error>> {
let endpoint = format!("{}/execute", self.url);
match self
.client
.post(endpoint)
.headers(self.headers.clone())
.json::<Executor>(executor)
.send()
.await
{
Ok(data) => {
let status = data.status();
match status {
reqwest::StatusCode::OK => {
let response = data.json::<RawExecResponse>().await?;
Ok(ExecResponse {
language: response.language,
version: response.version,
run: response.run,
compile: response.compile,
status: status.as_u16(),
})
}
_ => {
let text = format!("{}: {}", data.status(), data.text().await?);
let exec_result = ExecResult {
stdout: String::new(),
stderr: text.clone(),
output: text,
code: Some(1),
signal: None,
};
let exec_response = ExecResponse {
language: executor.language.clone(),
version: executor.version.clone(),
run: exec_result,
compile: None,
status: status.as_u16(),
};
Ok(exec_response)
}
}
}
Err(e) => Err(Box::new(e)),
}
}
}
#[cfg(test)]
mod test_client_private {
use super::Client;
#[test]
fn test_gen_headers_no_key() {
let headers = Client::generate_headers(None);
assert!(!headers.contains_key("Authorization"));
assert_eq!(headers.get("Accept").unwrap(), "application/json");
assert_eq!(headers.get("User-Agent").unwrap(), "piston-rs");
}
#[test]
fn test_gen_headers_with_key() {
let headers = Client::generate_headers(Some("123abc"));
assert_eq!(headers.get("Authorization").unwrap(), "123abc");
assert_eq!(headers.get("Accept").unwrap(), "application/json");
assert_eq!(headers.get("User-Agent").unwrap(), "piston-rs");
}
}