pub struct Client {
pub hostname: String,
pub port: String,
/* private fields */
}Fields§
§hostname: String§port: StringImplementations§
Source§impl Client
impl Client
Sourcepub fn new(hostname: String, port: Option<String>) -> Client
pub fn new(hostname: String, port: Option<String>) -> Client
Creates a new Client for the given hostname.
If port is None, the client will use the default port "6982".
§Examples
let c = Client::new("http://127.0.0.1".to_string(), None);Sourcepub fn get(&self) -> Result<(u16, Vec<Task>), ApiError>
pub fn get(&self) -> Result<(u16, Vec<Task>), ApiError>
Retrieves all tasks from the configured server.
§Returns
A tuple (u16, Vec<Task>) where u16 is the HTTP response status and Vec<Task> is the list of tasks.
§Examples
use yesser_todo_api::Client;
use yesser_todo_db::Task;
let client = Client::new("http://127.0.0.1".into(), None);
let (status, tasks) = client.get().ok()?;
// `tasks` is a Vec<yesser_todo_db::Task>Sourcepub fn add(&self, task_name: &str) -> Result<(u16, Task), ApiError>
pub fn add(&self, task_name: &str) -> Result<(u16, Task), ApiError>
Adds a new task with the given name to the to-do service.
Sends the task name as JSON to the service’s /add endpoint and returns the HTTP status
together with the created Task parsed from the response.
§Examples
use yesser_todo_api::{ Client, helpers::is_success };
fn example_add() {
let client = Client::new("http://127.0.0.1".to_string(), None);
let (status, task) = client.add("example task").unwrap();
assert!(is_success(status));
assert_eq!(task.name, "example task");
}Sourcepub fn get_index(&self, task_name: &str) -> Result<(u16, usize), ApiError>
pub fn get_index(&self, task_name: &str) -> Result<(u16, usize), ApiError>
Retrieves the zero-based index of the task with the given name from the API.
Returns the HTTP response status together with the parsed index on success.
§Examples
use yesser_todo_api::Client;
let client = Client::new("http://127.0.0.1".into(), None);
let (status, index) = client.get_index("example-task")?;
println!("status: {}, index: {}", status, index);Sourcepub fn remove(&self, task_name: &str) -> Result<u16, ApiError>
pub fn remove(&self, task_name: &str) -> Result<u16, ApiError>
Delete the task with the given name from the remote server.
Resolves the task’s index on the server and sends a DELETE request for that index.
§Returns
Ok(u16) if the server responded with a success status for the delete request, Err(ApiError) on transport or HTTP error.
§Examples
fn main() {
let client = Client::new("http://127.0.0.1".to_string(), None);
let _status = client.remove("example-task");
}Sourcepub fn done(&self, task_name: &str) -> Result<(u16, Task), ApiError>
pub fn done(&self, task_name: &str) -> Result<(u16, Task), ApiError>
Mark the task with the given name as done and return the server status and the updated task.
On success returns a tuple containing the response u16 status code and the Task as returned by the server.
On failure returns an Err(ApiError).
§Examples
let client = Client::new("http://127.0.0.1".to_string(), None);
let res = client.done("test");
match res {
Ok((status, task)) => {
let _ = task.name;
}
Err(e) => panic!("request failed: {:?}", e),
}Sourcepub fn undone(&self, task_name: &str) -> Result<(u16, Task), ApiError>
pub fn undone(&self, task_name: &str) -> Result<(u16, Task), ApiError>
Mark the task identified by task_name as not done and return the updated task.
§Returns
Ok((u16, Task)) with the HTTP response status and the updated task on success, Err(ApiError) on failure.
§Examples
use yesser_todo_api::Client;
let client = Client::new("http://127.0.0.1".to_string(), None);
let res = client.undone("example")?;Sourcepub fn clear(&self) -> Result<u16, ApiError>
pub fn clear(&self) -> Result<u16, ApiError>
Clears all tasks on the remote to-do service.
Sends a DELETE request to the configured /clear endpoint and returns the HTTP status code.
§Returns
- status code of the request;
- on failure returns
Err(ApiError)
§Examples
let client = Client::new("http://127.0.0.1".to_string(), None);
let status = client.clear().unwrap();
assert_eq!(status, 200);Sourcepub fn clear_done(&self) -> Result<u16, ApiError>
pub fn clear_done(&self) -> Result<u16, ApiError>
Clears all tasks marked as done on the remote to-do service.
Sends a DELETE request to “{hostname}:{port}/cleardone”.
§Returns
Ok(u16) with the HTTP response status when the request succeeds, Err(ApiError) otherwise.
§Examples
use yesser_todo_api::Client;
use yesser_todo_api::helpers::is_success;
let client = Client::new("http://127.0.0.1".into(), None);
let status = client.clear_done().unwrap();