Fix errorhandling in habitask
This commit is contained in:
parent
29d81e40c3
commit
2a382ebfee
3 changed files with 43 additions and 27 deletions
1
home-manager/packages/habitask/Cargo.lock
generated
1
home-manager/packages/habitask/Cargo.lock
generated
|
@ -254,6 +254,7 @@ name = "habitask"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"config 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"config 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
"hyper 0.11.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"reqwest 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"reqwest 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -1567,10 +1567,11 @@ rec {
|
||||||
num_cpus_1_8_0.default = true;
|
num_cpus_1_8_0.default = true;
|
||||||
}) [ futures_0_1_23_features num_cpus_1_8_0_features ];
|
}) [ futures_0_1_23_features num_cpus_1_8_0_features ];
|
||||||
habitask_0_1_0 = { features?(habitask_0_1_0_features {}) }: habitask_0_1_0_ {
|
habitask_0_1_0 = { features?(habitask_0_1_0_features {}) }: habitask_0_1_0_ {
|
||||||
dependencies = mapFeatures features ([ config_0_9_0 hyper_0_11_27 reqwest_0_8_6 serde_1_0_70 serde_derive_1_0_70 serde_json_1_0_24 task_hookrs_0_5_0 ]);
|
dependencies = mapFeatures features ([ config_0_9_0 error_chain_0_11_0 hyper_0_11_27 reqwest_0_8_6 serde_1_0_70 serde_derive_1_0_70 serde_json_1_0_24 task_hookrs_0_5_0 ]);
|
||||||
};
|
};
|
||||||
habitask_0_1_0_features = f: updateFeatures f (rec {
|
habitask_0_1_0_features = f: updateFeatures f (rec {
|
||||||
config_0_9_0.default = true;
|
config_0_9_0.default = true;
|
||||||
|
error_chain_0_11_0.default = true;
|
||||||
habitask_0_1_0.default = (f.habitask_0_1_0.default or true);
|
habitask_0_1_0.default = (f.habitask_0_1_0.default or true);
|
||||||
hyper_0_11_27.default = true;
|
hyper_0_11_27.default = true;
|
||||||
reqwest_0_8_6.default = true;
|
reqwest_0_8_6.default = true;
|
||||||
|
@ -1578,7 +1579,7 @@ rec {
|
||||||
serde_derive_1_0_70.default = true;
|
serde_derive_1_0_70.default = true;
|
||||||
serde_json_1_0_24.default = true;
|
serde_json_1_0_24.default = true;
|
||||||
task_hookrs_0_5_0.default = true;
|
task_hookrs_0_5_0.default = true;
|
||||||
}) [ config_0_9_0_features hyper_0_11_27_features reqwest_0_8_6_features serde_1_0_70_features serde_derive_1_0_70_features serde_json_1_0_24_features task_hookrs_0_5_0_features ];
|
}) [ config_0_9_0_features error_chain_0_11_0_features hyper_0_11_27_features reqwest_0_8_6_features serde_1_0_70_features serde_derive_1_0_70_features serde_json_1_0_24_features task_hookrs_0_5_0_features ];
|
||||||
httparse_1_3_2 = { features?(httparse_1_3_2_features {}) }: httparse_1_3_2_ {
|
httparse_1_3_2 = { features?(httparse_1_3_2_features {}) }: httparse_1_3_2_ {
|
||||||
features = mkFeatures (features.httparse_1_3_2 or {});
|
features = mkFeatures (features.httparse_1_3_2 or {});
|
||||||
};
|
};
|
||||||
|
|
|
@ -22,6 +22,10 @@ use config::{Config, Environment};
|
||||||
error_chain! {
|
error_chain! {
|
||||||
foreign_links {
|
foreign_links {
|
||||||
Io(::std::io::Error);
|
Io(::std::io::Error);
|
||||||
|
Reqwust(::reqwest::Error);
|
||||||
|
Utf8(::std::str::Utf8Error);
|
||||||
|
JSON(::serde_json::Error);
|
||||||
|
Config(::config::ConfigError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,10 +78,10 @@ impl Habitask {
|
||||||
let mut s = Config::new();
|
let mut s = Config::new();
|
||||||
s.merge(Environment::with_prefix("habitask"))?;
|
s.merge(Environment::with_prefix("habitask"))?;
|
||||||
let s = s.try_into()?;
|
let s = s.try_into()?;
|
||||||
Habitask {
|
Ok(Habitask {
|
||||||
client: Client::new(),
|
client: Client::new(),
|
||||||
settings: s,
|
settings: s,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn login(&self, mut builder: RequestBuilder) -> RequestBuilder {
|
fn login(&self, mut builder: RequestBuilder) -> RequestBuilder {
|
||||||
|
@ -95,7 +99,7 @@ impl Habitask {
|
||||||
|
|
||||||
fn make_todo(&self, name: &str, prio: &str, tasks: Vec<Task>) -> Result<()> {
|
fn make_todo(&self, name: &str, prio: &str, tasks: Vec<Task>) -> Result<()> {
|
||||||
if tasks.len() == 0 {
|
if tasks.len() == 0 {
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
let Todo { id, .. } = self.create_todo(name, prio)?;
|
let Todo { id, .. } = self.create_todo(name, prio)?;
|
||||||
let mut checklist: Vec<Item> = Vec::new();
|
let mut checklist: Vec<Item> = Vec::new();
|
||||||
|
@ -112,40 +116,34 @@ impl Habitask {
|
||||||
|
|
||||||
fn create_todo(&self, name: &str, prio: &str) -> Result<Todo> {
|
fn create_todo(&self, name: &str, prio: &str) -> Result<Todo> {
|
||||||
let map = vec![("text", name), ("type", "todo"), ("priority", prio)];
|
let map = vec![("text", name), ("type", "todo"), ("priority", prio)];
|
||||||
|
Ok(
|
||||||
self.post("https://habitica.com/api/v3/tasks/user")
|
self.post("https://habitica.com/api/v3/tasks/user")
|
||||||
.json(&map)
|
.json(&map)
|
||||||
.send()?
|
.send()?
|
||||||
.json::<Response>()?
|
.json::<Response>()?
|
||||||
.data
|
.data,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_item(&self, id: &str, item: &str) -> Result<Todo> {
|
fn add_item(&self, id: &str, item: &str) -> Result<Todo> {
|
||||||
let map = vec![("text", item)];
|
let map = vec![("text", item)];
|
||||||
let url = format!("https://habitica.com/api/v3/tasks/{}/checklist", id);
|
let url = format!("https://habitica.com/api/v3/tasks/{}/checklist", id);
|
||||||
Ok(self.post(&url)
|
Ok(self.post(&url).json(&map).send()?.json::<Response>()?.data)
|
||||||
.json(&map)
|
|
||||||
.send()?
|
|
||||||
.json::<Response>()?
|
|
||||||
.data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_item(&self, id: &str, item: &str) -> Result<()>{
|
fn check_item(&self, id: &str, item: &str) -> Result<()> {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"https://habitica.com/api/v3/tasks/{}/checklist/{}/score",
|
"https://habitica.com/api/v3/tasks/{}/checklist/{}/score",
|
||||||
id,
|
id,
|
||||||
item
|
item
|
||||||
);
|
);
|
||||||
self.post(&url)
|
self.post(&url).json(&Vec::<(&str, &str)>::new()).send()?;
|
||||||
.json(&Vec::<(&str, &str)>::new())
|
|
||||||
.send()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn score_task(&self, id: &str) -> Result<()>{
|
fn score_task(&self, id: &str) -> Result<()> {
|
||||||
let url = format!("https://habitica.com/api/v3/tasks/{}/score/up", id);
|
let url = format!("https://habitica.com/api/v3/tasks/{}/score/up", id);
|
||||||
self.post(&url)
|
self.post(&url).json(&Vec::<(&str, &str)>::new()).send()?;
|
||||||
.json(&Vec::<(&str, &str)>::new())
|
|
||||||
.send();
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,14 +178,30 @@ fn main() -> Result<()> {
|
||||||
blink();
|
blink();
|
||||||
}
|
}
|
||||||
habitask.make_todo("Routinen erledigt!", "0.1", routines)?;
|
habitask.make_todo("Routinen erledigt!", "0.1", routines)?;
|
||||||
habitask.make_todo("Herausforderungen bezwungen!", "1", tasks)?;
|
habitask.make_todo(
|
||||||
|
"Herausforderungen bezwungen!",
|
||||||
|
"1",
|
||||||
|
tasks,
|
||||||
|
)?;
|
||||||
habitask.make_todo(
|
habitask.make_todo(
|
||||||
"Ein paar Tage alte Herausforderungen bezwungen!",
|
"Ein paar Tage alte Herausforderungen bezwungen!",
|
||||||
"1.5",
|
"1.5",
|
||||||
a_little_old,
|
a_little_old,
|
||||||
)?;
|
)?;
|
||||||
habitask.make_todo("Einige Wochen alte Herausforderungen bezwungen!", "2", old)?;
|
habitask.make_todo(
|
||||||
habitask.make_todo("Monate alte Herausforderungen bezwungen!", "2", very_old)?;
|
"Einige Wochen alte Herausforderungen bezwungen!",
|
||||||
habitask.make_todo("Uralte Herausforderungen bezwungen!", "2", crazy_old)?;
|
"2",
|
||||||
|
old,
|
||||||
|
)?;
|
||||||
|
habitask.make_todo(
|
||||||
|
"Monate alte Herausforderungen bezwungen!",
|
||||||
|
"2",
|
||||||
|
very_old,
|
||||||
|
)?;
|
||||||
|
habitask.make_todo(
|
||||||
|
"Uralte Herausforderungen bezwungen!",
|
||||||
|
"2",
|
||||||
|
crazy_old,
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue