|
|
|
@ -1,16 +1,25 @@
|
|
|
|
|
use crate::api::methods::sync::SyncResponse;
|
|
|
|
|
use crate::api::methods::{login::ValidLoginFlows, sync::SyncResponse};
|
|
|
|
|
use reqwest::{
|
|
|
|
|
header::{HeaderMap, HeaderValue, CONTENT_TYPE, USER_AGENT},
|
|
|
|
|
Client as reqwest_client, Response, StatusCode,
|
|
|
|
|
};
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use std::{collections::HashMap, error::Error, fmt, time};
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
error::Error,
|
|
|
|
|
fmt,
|
|
|
|
|
sync::{
|
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
|
Arc,
|
|
|
|
|
},
|
|
|
|
|
thread, time,
|
|
|
|
|
};
|
|
|
|
|
use url::{ParseError, Url};
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
use mockito;
|
|
|
|
|
|
|
|
|
|
const V2_API_PATH: &str = "/_matrix/client/r0";
|
|
|
|
|
const SUPPORTED_VERSION: &str = "r0.5.0";
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
@ -71,18 +80,18 @@ pub struct SupportedSpecs {
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct DiscoveryInformation {
|
|
|
|
|
#[serde(rename = "m.homeserver")]
|
|
|
|
|
homeserver: HomeserverInformation,
|
|
|
|
|
identity_server: Option<IdentityServerInformation>,
|
|
|
|
|
pub homeserver: HomeserverInformation,
|
|
|
|
|
pub identity_server: Option<IdentityServerInformation>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct HomeserverInformation {
|
|
|
|
|
base_url: String,
|
|
|
|
|
pub base_url: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
pub struct IdentityServerInformation {
|
|
|
|
|
base_url: String,
|
|
|
|
|
pub base_url: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
@ -175,6 +184,7 @@ impl Client {
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
let url = &mockito::server_url();
|
|
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
|
let url = &self.homeserver_url;
|
|
|
|
|
|
|
|
|
@ -218,16 +228,17 @@ impl Client {
|
|
|
|
|
if res.status().is_success() {
|
|
|
|
|
return Ok(res);
|
|
|
|
|
} else if res.status() == StatusCode::TOO_MANY_REQUESTS {
|
|
|
|
|
let mut body: HashMap<String, String> = res.json()?;
|
|
|
|
|
let mut body: &Value = &res.json()?;
|
|
|
|
|
if let Some(value) = body.get("error") {
|
|
|
|
|
body = serde_json::from_str(value)?;
|
|
|
|
|
body = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(value) = body.get("retry_after_ms") {
|
|
|
|
|
std::thread::sleep(time::Duration::from_millis(value.parse::<u64>()?));
|
|
|
|
|
} else {
|
|
|
|
|
std::thread::sleep(time::Duration::from_millis(self.default_492_wait_ms));
|
|
|
|
|
}
|
|
|
|
|
std::thread::sleep(time::Duration::from_millis(
|
|
|
|
|
body.get("retry_after_ms").map_or_else(
|
|
|
|
|
|| self.default_492_wait_ms,
|
|
|
|
|
|v| v.as_u64().unwrap_or_else(|| self.default_492_wait_ms),
|
|
|
|
|
),
|
|
|
|
|
));
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Box::from(ResponseError {
|
|
|
|
|
code: res.status(),
|
|
|
|
@ -289,6 +300,21 @@ impl Client {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Holds implementation for Section 5.4, Login
|
|
|
|
|
impl Client {
|
|
|
|
|
pub fn login_flows(&self) -> Result<ValidLoginFlows, Box<dyn Error>> {
|
|
|
|
|
Ok(self
|
|
|
|
|
.send(
|
|
|
|
|
MatrixHTTPMethod::Get,
|
|
|
|
|
"/_matrix/client/r0/login",
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
None,
|
|
|
|
|
)?
|
|
|
|
|
.json()?)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
@ -340,6 +366,42 @@ mod tests {
|
|
|
|
|
assert!(resp.unstable_features.is_none());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn send_handles_too_many_requests() {
|
|
|
|
|
let _mock_version_check = mock("GET", "/_matrix/client/versions")
|
|
|
|
|
.with_body(r#"{"versions": ["r0.5.0"]}"#)
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
let _m2 = mock("GET", "/hello")
|
|
|
|
|
.with_body(
|
|
|
|
|
r#"{
|
|
|
|
|
"errcode": "M_LIMIT_EXCEEDED",
|
|
|
|
|
"error": "Too many requests",
|
|
|
|
|
"retry_after_ms": 2000
|
|
|
|
|
}"#,
|
|
|
|
|
)
|
|
|
|
|
.with_status(429)
|
|
|
|
|
.create();
|
|
|
|
|
|
|
|
|
|
// Run request in separate thread. Once async lands use async/await
|
|
|
|
|
thread::spawn(|| {
|
|
|
|
|
let resp: Value = Client::new("http://dummy.website", None, None, None)
|
|
|
|
|
.expect("failed to get valid client")
|
|
|
|
|
.send(MatrixHTTPMethod::Get, "/hello", None, None, None)
|
|
|
|
|
.expect("failed to unwrap Response")
|
|
|
|
|
.json()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(dbg!(resp)["hello"], "world");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
thread::sleep(time::Duration::from_secs(1));
|
|
|
|
|
|
|
|
|
|
// Override 429 response with valid response.
|
|
|
|
|
let _m = mock("GET", "/hello")
|
|
|
|
|
.with_body(r#"{"hello": "world"}"#)
|
|
|
|
|
.create();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct ApiError {}
|
|
|
|
|