Skip to main content

Client

Struct Client 

Source
pub struct Client {
    pub hostname: String,
    pub port: String,
    /* private fields */
}

Fields§

§hostname: String§port: String

Implementations§

Source§

impl Client

Source

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);
Source

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>
Source

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");
}
Source

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);
Source

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");
}
Source

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),
}
Source

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")?;
Source

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);
Source

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();

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more