pub struct Client { /* private fields */ }Expand description
A client used to send requests to Piston.
Implementations§
Source§impl Client
impl Client
Sourcepub fn with_url(url: &str) -> Self
pub fn with_url(url: &str) -> Self
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");Sourcepub fn with_key(key: &str) -> Self
pub fn with_key(key: &str) -> Self
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");Sourcepub fn with_url_and_key(url: &str, key: &str) -> Self
pub fn with_url_and_key(url: &str, key: &str) -> Self
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");Sourcepub fn get_headers(&self) -> HeaderMap
pub fn get_headers(&self) -> HeaderMap
Sourcepub async fn fetch_runtimes(&self) -> Result<Vec<Runtime>, Box<dyn Error>>
pub async fn fetch_runtimes(&self) -> Result<Vec<Runtime>, Box<dyn Error>>
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
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.
}Sourcepub async fn execute(
&self,
executor: &Executor,
) -> Result<ExecResponse, Box<dyn Error>>
pub async fn execute( &self, executor: &Executor, ) -> Result<ExecResponse, Box<dyn Error>>
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
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.
}Trait Implementations§
Source§impl Default for Client
impl Default for Client
Source§fn default() -> Self
fn default() -> Self
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());Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more