Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client::with_url and Client::with_url_and_key #10

Merged
merged 1 commit into from
Dec 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Client::with_url and Client::with_url_and_key
  • Loading branch information
y21 authored Dec 28, 2021
commit 10f9cddcad825ad61c5e690ca9629da92f571259
49 changes: 48 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ impl Client {
}
}

/// 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
Expand All @@ -84,7 +107,31 @@ impl Client {
}
}

/// The base url for the Piston V2 API.
/// 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
///
Expand Down