Add support for authenticated endpoints.
This commit is contained in:
parent
6daa7c22f1
commit
bafc48292e
@ -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 {
|
||||
|
@ -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() {
|
||||
|
@ -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,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user