Skip to content

Commit

Permalink
added get user
Browse files Browse the repository at this point in the history
  • Loading branch information
kickthedragon committed Sep 2, 2016
1 parent f4c78ee commit e52a529
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/lib.rs
Expand Up @@ -346,6 +346,44 @@ impl ClientV1 {
}
}

/// Get the user
pub fn get_user(&self, access_token: &AccessToken, user_id: u64) -> Result<User> {
if access_token.scopes().any(|s| match s {
&Scope::User(u_id) => u_id == user_id,
&Scope::Admin => true,
_ => false,
}) && !access_token.has_expired() {
let mut headers = Headers::new();
headers.set(Authorization(access_token.get_token()));
let mut response = try!(self.client
.get(&format!("{}user/{}", self.url, user_id))
.headers(headers)
.send());
match response.status {
StatusCode::Ok => {
let mut response_str = String::new();
try!(response.read_to_string(&mut response_str));
match json::decode::<User>(&response_str) {
Ok(user) => Ok(user),
Err(e) => Err(e.into()),
}
},
StatusCode::Accepted => {
let mut response_str = String::new();
try!(response.read_to_string(&mut response_str));
match json::decode::<ResponseDTO>(&response_str) {
Ok(r) => Err(Error::ClientError(r)),
Err(e) => Err(e.into()),
}
}
StatusCode::Unauthorized => Err(Error::Unauthorized),
_ => Err(Error::ServerError),
}
} else {
Err(Error::Unauthorized)
}
}

/// Registers the user
pub fn register<S: AsRef<str>>(&self,
access_token: &AccessToken,
Expand Down

0 comments on commit e52a529

Please sign in to comment.