Merge pull request #2 from vberger/master

Add support for authenticated endpoints.
This commit is contained in:
Jimmy Cuadra 2017-01-08 12:50:01 -08:00 committed by GitHub
commit 324fc5dc0d
3 changed files with 16 additions and 3 deletions

View File

@ -14,6 +14,8 @@ pub enum Error {
SerdeJson(SerdeJsonError),
/// An error when serializing a query string value.
SerdeUrlEncodedSerialize(SerdeUrlEncodedSerializeError),
/// Queried endpoint requires authentication but was called on an anonymous client
AuthenticationRequired
}
impl From<HyperError> for Error {

View File

@ -84,6 +84,14 @@ impl Client {
url.set_query(Some(&serde_urlencoded::to_string(&query_params)?));
if E::requires_authentication() {
if let Some(ref session) = self.session {
url.query_pairs_mut().append_pair("access_token", &session.access_token);
} else {
return Err(Error::AuthenticationRequired)
}
}
let mut request = HyperRequest::new(E::method().into_hyper(), url);
match E::method() {

View File

@ -4,7 +4,10 @@ use url::Host;
/// An active user session with a Matrix homeserver, allowing authenticated requests.
#[derive(Clone, Debug)]
pub struct Session {
access_token: String,
homeserver: Host,
user_id: UserId,
/// The access token of this session
pub access_token: String,
/// The homeserver this session is associated with
pub homeserver: Host,
/// the ID of the user owning this session
pub user_id: UserId,
}