gdritter repos mattermost-rs / fa8752d
Switch to using a custom error type Getty Ritter 5 years ago
3 changed file(s) with 39 addition(s) and 6 deletion(s). Collapse all Expand all
415415 version = "0.1.0"
416416 dependencies = [
417417 "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
418 "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
419 "http 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
418420 "reqwest 0.9.12 (registry+https://github.com/rust-lang/crates.io-index)",
419421 "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
420422 "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
66
77 [dependencies]
88 failure = "*"
9 failure_derive = "*"
910 serde = "*"
1011 serde_derive = "*"
1112 serde_json = "*"
1213 reqwest = "*"
14 http = "*"
1 #[macro_use] extern crate failure;
1 extern crate failure;
2 #[macro_use] extern crate failure_derive;
23 #[macro_use] extern crate serde_derive;
34
45 use reqwest as req;
6
7 pub mod mm {
8 #[derive(Fail, Debug)]
9 pub enum Error {
10 #[fail(display = "HTTP error: {}", err)]
11 HTTPError { err: reqwest::Error },
12
13 #[fail(display = "Error reading header: {}", err)]
14 EncodingError { err: http::header::ToStrError },
15
16 #[fail(display = "Missing header in response: {}", name)]
17 HeaderMissingError { name: String },
18 }
19
20 impl From<reqwest::Error> for Error {
21 fn from(err: reqwest::Error) -> Error {
22 Error::HTTPError { err }
23 }
24 }
25
26 impl From<http::header::ToStrError> for Error {
27 fn from(err: http::header::ToStrError) -> Error {
28 Error::EncodingError { err }
29 }
30 }
31
32 pub type Result<T> = core::result::Result<T, Error>;
33 }
534
635 #[derive(Serialize, Deserialize)]
736 struct Login {
5281 }
5382
5483 impl Session {
55 fn login(login: &Login, server: &str) -> Result<Session, failure::Error> {
84 fn login(login: &Login, server: &str) -> mm::Result<Session> {
5685 let uri = format!("{}/api/v4", server);
5786 let client = req::Client::new();
5887 let mut res = client.post(&format!("{}/users/login", uri))
5988 .json(login)
6089 .send()?;
6190 let token_val = res.headers().get("token")
62 .ok_or(format_err!("No token present in response"))?;
91 .ok_or(mm::Error::HeaderMissingError { name: "token".to_string() })?;
6392 let token = format!("Bearer {}", token_val.to_str()?);
6493 let user = res.json()?;
6594 Ok(Session {
7099 })
71100 }
72101
73 fn get<T: for<'t> serde::Deserialize<'t>>(&self, path: String) -> Result<T, failure::Error> {
102 fn get<T: for<'t> serde::Deserialize<'t>>(&self, path: String) -> mm::Result<T> {
74103 Ok(self.client.get(&format!("{}/{}", self.uri, path))
75104 .header("Content-Type", "application/json")
76105 .header("Authorization", self.token.clone())
82111 &self.user
83112 }
84113
85 fn get_teams(&self) -> Result<Vec<Team>, failure::Error> {
114 fn get_teams(&self) -> mm::Result<Vec<Team>> {
86115 self.get(format!("users/{}/teams", self.user.id))
87116 }
88117
89 fn get_channels_for_user(&self, user_id: &str, team_id: &str) -> Result<Vec<Channel>, failure::Error> {
118 fn get_channels_for_user(&self, user_id: &str, team_id: &str) -> mm::Result<Vec<Channel>> {
90119 self.get(format!("users/{}/teams/{}/channels", user_id, team_id))
91120 }
92121 }