#![doc(html_root_url = "https://docs.rs/pixelast/0.1.0")]
extern crate reqwest;
extern crate regex;
extern crate serde;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate failure;
use failure::Error;
mod response;
mod endpoint;
mod http_client;
mod error;
mod graph;
mod pixel;
mod user;
pub use self::user::ConsentAnswer;
pub use self::graph::{Graph, GraphType, GraphColor};
pub use self::pixel::Pixel;
pub use self::error::PixelaClientError;
use self::user::CreateUserParam;
use self::graph::UpdateGraphParam;
use self::http_client::TinyHttpClient;
pub struct PixelaClient {
auth: Authentication,
}
pub(crate) struct Authentication {
username: String,
token: String,
}
impl PixelaClient {
pub fn new(username: &str, token: &str) -> Self {
Self {
auth: Authentication {
username: username.to_owned(),
token: token.to_owned(),
}
}
}
pub fn create_new_user(username: &str, token: &str, agree_terms_of_service: ConsentAnswer, not_minor: ConsentAnswer) -> Result<(), Error> {
let param = CreateUserParam {
username: username.to_owned(),
token: token.to_owned(),
agree_terms_of_service,
not_minor
};
user::create::<TinyHttpClient>(¶m)
}
pub fn update_user_token(&self, new_token: &str) -> Result<(), Error> {
user::update::<TinyHttpClient>(&self.auth, new_token)
}
pub fn delete_user(&self) -> Result<(), Error> {
user::delete::<TinyHttpClient>(&self.auth)
}
pub fn create_graph(&self, id: &str, name: &str, unit: &str, graph_type: GraphType, color: GraphColor) -> Result<(), Error> {
let param = Graph {
id: id.to_owned(),
name: name.to_owned(),
unit: unit.to_owned(),
graph_type,
color,
};
graph::create::<TinyHttpClient>(&self.auth, ¶m)
}
pub fn get_graphs(&self) -> Result<Vec<Graph>, Error> {
graph::get_all::<TinyHttpClient>(&self.auth)
}
pub fn get_graph_svg(&self, graph_id: &str, date: Option<&str>) -> Result<String, Error> {
graph::get_graph_svg::<TinyHttpClient>(&self.auth, graph_id, date)
}
pub fn update_graph(&self, graph_id: &str, name: &str, unit: &str, color: GraphColor) -> Result<(), Error> {
let param = UpdateGraphParam {
name: name.to_owned(),
unit: unit.to_owned(),
color,
};
graph::update::<TinyHttpClient>(&self.auth, graph_id, ¶m)
}
pub fn delete_graph(&self, graph_id: &str) -> Result<(), Error> {
graph::delete::<TinyHttpClient>(&self.auth, graph_id)
}
pub fn record_pixel(&self, graph_id: &str, date: &str, quantity: &str) -> Result<(), Error> {
let param = Pixel {
date: date.to_owned(),
quantity: quantity.to_owned(),
};
pixel::create::<TinyHttpClient>(&self.auth, graph_id, ¶m)
}
pub fn get_pixel(&self, graph_id: &str, date: &str) -> Result<Pixel, Error> {
pixel::get::<TinyHttpClient>(&self.auth, graph_id, date)
}
pub fn update_pixel(&self, graph_id: &str, date: &str, quantity: &str) -> Result<(), Error> {
let param = Pixel {
date: date.to_owned(),
quantity: quantity.to_owned(),
};
pixel::update::<TinyHttpClient>(&self.auth, graph_id, ¶m)
}
pub fn delete_pixel(&self, graph_id: &str, date: &str) -> Result<(), Error> {
pixel::delete::<TinyHttpClient>(&self.auth, graph_id, date)
}
pub fn increment(&self, graph_id: &str) -> Result<(), Error> {
pixel::increment::<TinyHttpClient>(&self.auth, graph_id)
}
pub fn decrement(&self, graph_id: &str) -> Result<(), Error> {
pixel::decrement::<TinyHttpClient>(&self.auth, graph_id)
}
}