1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#![doc(html_root_url = "https://docs.rs/pixelast/0.1.0")]

//! This library is Pixela client for Rust.
//! 
//! # Example
//! 
//! ```rust,ignore
//! use pixelast::{PixelaClient, ConsentAnswer, GraphType, GraphColor};
//! 
//! fn main() {
//!     let res = PixelaClient::create_new_user(
//!         "username",
//!         "usertoken",
//!         ConsentAnswer::Yes,
//!         ConsentAnswer::Yes
//!     );
//! 
//!     match res {
//!         Ok(()) => println!("create new user."),
//!         Err(v) => panic!("create new user failed. {}", v),
//!     }
//! 
//!     let client = PixelaClient::new("username", "usertoken");
//!     client.create_graph("graphid", "graphname", "cal", GraphType::Int, GraphColor::Shibafu).unwrap();
//!     client.update_graph("graphid", "graphname", "cal", GraphColor::Shibafu).unwrap();
//! 
//!     client.record_pixel("graphid", "20181016", "10").unwrap();
//!     client.record_pixel("graphid", "20181017", "10").unwrap();
//!     client.record_pixel("graphid", "20181018", "10").unwrap();
//! 
//!     client.update_pixel("graphid", "20181018", "20").unwrap();
//!     client.delete_pixel("graphid", "20181016").unwrap();
//! 
//!     let pixel = client.get_pixel("graphid", "20181018").unwrap();
//!     println!("{:?}", pixel);
//! 
//!     client.increment("graphid").unwrap();
//!     client.decrement("graphid").unwrap();
//! 
//!     let graphs = client.get_graphs().unwrap();
//!     println!("{:?}", graphs);
//! 
//!     let svg = client.get_graph_svg("graphid", Some("20181020")).unwrap();
//!     println!("{}", svg);
//! 
//!     client.delete_graph("graphid").unwrap();
//!     client.delete_user().unwrap();
//! }
//! ```

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;

/// A `PixelaClient` to request to Pixela with.
pub struct PixelaClient {
    auth: Authentication,
}

pub(crate) struct Authentication {
    username: String,
    token: String,
}

impl PixelaClient {
    /// Constructs a new `PixelaClient`.
    ///
    /// This method does not verify authentication.
    pub fn new(username: &str, token: &str) -> Self {
        Self {
            auth: Authentication {
                username: username.to_owned(),
                token: token.to_owned(),
            }
        }
    }

    /// Create a new Pixela user.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    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>(&param)
    }

    /// Updates the authentication token for the specified user.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn update_user_token(&self, new_token: &str) -> Result<(), Error> {
        user::update::<TinyHttpClient>(&self.auth, new_token)
    }

    /// Deletes the specified registered user.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn delete_user(&self) -> Result<(), Error> {
        user::delete::<TinyHttpClient>(&self.auth)
    }

    /// Create a new pixelation graph definition.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    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, &param)
    }

    /// Get all predefined pixelation graph definitions.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn get_graphs(&self) -> Result<Vec<Graph>, Error> {
        graph::get_all::<TinyHttpClient>(&self.auth)
    }

    /// Based on the registered information, express the graph in SVG format diagram.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    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)
    }

    /// Update predefined pixelation graph definitions. The items that can be updated are limited as compared with the pixelation graph definition creation.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    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, &param)
    }

    /// Delete the predefined pixelation graph definition.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn delete_graph(&self, graph_id: &str) -> Result<(), Error> {
        graph::delete::<TinyHttpClient>(&self.auth, graph_id)
    }

    /// It records the quantity of the specified date as a "Pixel".
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    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, &param)
    }

    /// Get registered quantity as "Pixel".
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn get_pixel(&self, graph_id: &str, date: &str) -> Result<Pixel, Error> {
        pixel::get::<TinyHttpClient>(&self.auth, graph_id, date)
    }

    /// Update the quantity already registered as a "Pixel".
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    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, &param)
    }

    /// Delete the registered "Pixel".
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn delete_pixel(&self, graph_id: &str, date: &str) -> Result<(), Error> {
        pixel::delete::<TinyHttpClient>(&self.auth, graph_id, date)
    }

    /// Increment quantity "Pixel" of the day (UTC).
    /// If the graph type is int then 1 added, and for float then 0.01 added.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn increment(&self, graph_id: &str) -> Result<(), Error> {
        pixel::increment::<TinyHttpClient>(&self.auth, graph_id)
    }

    /// Decrement quantity "Pixel" of the day (UTC).
    /// If the graph type is int then -1 added, and for float then -0.01 added.
    ///
    /// # Errors
    ///
    /// This method fails when request not success in Pixela.
    pub fn decrement(&self, graph_id: &str) -> Result<(), Error> {
        pixel::decrement::<TinyHttpClient>(&self.auth, graph_id)
    }
}