-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #565 from Stremio/feat/streaming-server-remote-end…
…point Implement streaming server remote endpoint
- Loading branch information
Showing
10 changed files
with
361 additions
and
75 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
mod network_info; | ||
pub use network_info::*; | ||
|
||
mod response; | ||
pub use response::*; | ||
|
||
mod settings; | ||
pub use settings::*; | ||
|
||
mod statistics; | ||
pub use statistics::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct NetworkInfo { | ||
pub available_interfaces: Vec<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use url::Url; | ||
|
||
use crate::types::streaming_server::Settings; | ||
|
||
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct SettingsResponse { | ||
pub base_url: Url, | ||
pub values: Settings, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct GetHTTPSResponse { | ||
pub ip_address: String, | ||
pub domain: String, | ||
pub port: u16, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Settings { | ||
pub app_path: String, | ||
pub cache_root: String, | ||
pub server_version: String, | ||
pub remote_https: Option<String>, | ||
pub cache_size: Option<f64>, | ||
pub bt_max_connections: u64, | ||
pub bt_handshake_timeout: u64, | ||
pub bt_request_timeout: u64, | ||
pub bt_download_speed_soft_limit: f64, | ||
pub bt_download_speed_hard_limit: f64, | ||
pub bt_min_peers_for_stable: u64, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod remote_endpoint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
use std::any::Any; | ||
|
||
use futures::future; | ||
use stremio_derive::Model; | ||
use url::Url; | ||
|
||
use crate::{ | ||
models::{ | ||
ctx::Ctx, | ||
streaming_server::{PlaybackDevice, StreamingServer}, | ||
}, | ||
runtime::{ | ||
msg::{Action, ActionStreamingServer}, | ||
EnvFutureExt, Runtime, RuntimeAction, TryEnvFuture, | ||
}, | ||
types::{ | ||
api::SuccessResponse, | ||
profile::{Auth, AuthKey, Profile}, | ||
streaming_server::{ | ||
GetHTTPSResponse, NetworkInfo, Settings as StreamingServerSettings, SettingsResponse, | ||
}, | ||
True, | ||
}, | ||
unit_tests::{default_fetch_handler, Request, TestEnv, FETCH_HANDLER}, | ||
}; | ||
|
||
const STREAMING_SERVER_URL: &str = "http://127.0.0.1:11470"; | ||
const STREAMING_SERVER_SETTINGS: StreamingServerSettings = StreamingServerSettings { | ||
remote_https: None, | ||
app_path: String::new(), | ||
cache_root: String::new(), | ||
server_version: String::new(), | ||
cache_size: None, | ||
bt_max_connections: 0, | ||
bt_handshake_timeout: 0, | ||
bt_request_timeout: 0, | ||
bt_download_speed_soft_limit: 0.0, | ||
bt_download_speed_hard_limit: 0.0, | ||
bt_min_peers_for_stable: 0, | ||
}; | ||
|
||
const AVAILABLE_INTERFACE: &str = "192.168.0.10"; | ||
|
||
#[test] | ||
fn remote_endpoint() { | ||
#[derive(Model, Clone, Debug)] | ||
#[model(TestEnv)] | ||
struct TestModel { | ||
ctx: Ctx, | ||
streaming_server: StreamingServer, | ||
} | ||
|
||
fn fetch_handler(request: Request) -> TryEnvFuture<Box<dyn Any + Send>> { | ||
match request { | ||
Request { url, method, .. } | ||
if method == "GET" && url == "http://127.0.0.1:11470/settings" => | ||
{ | ||
future::ok(Box::new(SettingsResponse { | ||
base_url: Url::parse(&STREAMING_SERVER_URL.to_string()).unwrap(), | ||
values: STREAMING_SERVER_SETTINGS, | ||
}) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
Request { url, method, .. } | ||
if method == "POST" && url == "http://127.0.0.1:11470/settings" => | ||
{ | ||
future::ok(Box::new(SuccessResponse { success: True }) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
Request { url, .. } if url == "http://127.0.0.1:11470/casting" => { | ||
future::ok(Box::new(Vec::<PlaybackDevice>::new()) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
Request { url, .. } if url == "http://127.0.0.1:11470/network-info" => { | ||
future::ok(Box::new(NetworkInfo { | ||
available_interfaces: vec![AVAILABLE_INTERFACE.to_string()], | ||
}) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
Request { url, .. } if url.starts_with("http://127.0.0.1:11470/get-https") => { | ||
future::ok(Box::new(GetHTTPSResponse { | ||
ip_address: AVAILABLE_INTERFACE.to_string(), | ||
domain: "https://stremio.com".to_string(), | ||
port: 3333, | ||
}) as Box<dyn Any + Send>) | ||
.boxed_env() | ||
} | ||
_ => default_fetch_handler(request), | ||
} | ||
} | ||
|
||
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv"); | ||
|
||
*FETCH_HANDLER.write().unwrap() = Box::new(fetch_handler); | ||
|
||
let profile = Profile { | ||
auth: Some(Auth { | ||
key: AuthKey("auth_key".to_owned()), | ||
..Default::default() | ||
}), | ||
..Default::default() | ||
}; | ||
|
||
let (streaming_server, ..) = StreamingServer::new::<TestEnv>(&profile); | ||
|
||
let (runtime, _rx) = Runtime::<TestEnv, _>::new( | ||
TestModel { | ||
ctx: Ctx { | ||
profile, | ||
..Default::default() | ||
}, | ||
streaming_server, | ||
}, | ||
vec![], | ||
1000, | ||
); | ||
|
||
assert!( | ||
runtime | ||
.model() | ||
.unwrap() | ||
.streaming_server | ||
.remote_url | ||
.is_none(), | ||
"Remote url should not be set" | ||
); | ||
|
||
TestEnv::run(|| { | ||
runtime.dispatch(RuntimeAction { | ||
field: None, | ||
action: Action::StreamingServer(ActionStreamingServer::Reload), | ||
}); | ||
}); | ||
|
||
TestEnv::run(|| { | ||
runtime.dispatch(RuntimeAction { | ||
field: None, | ||
action: Action::StreamingServer(ActionStreamingServer::UpdateSettings( | ||
StreamingServerSettings { | ||
remote_https: Some(AVAILABLE_INTERFACE.to_string()), | ||
..STREAMING_SERVER_SETTINGS | ||
}, | ||
)), | ||
}); | ||
}); | ||
|
||
assert!( | ||
runtime | ||
.model() | ||
.unwrap() | ||
.streaming_server | ||
.remote_url | ||
.is_some(), | ||
"Remote url should be set" | ||
); | ||
} |