livepeer_rs/live/stream.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
use crate::errors;
use serde_json;
#[derive(Debug, Clone)]
pub struct Stream {
pub client: crate::LivepeerClient,
pub urls: crate::api::urls::LivepeerUrls,
}
impl crate::live::Stream for Stream {
/// List all streams
///
/// # Returns
/// * `Result<crate::data::stream::Streams, errors::Error>` - A list of streams or an error
fn list_streams(&self) -> Result<crate::data::stream::Streams, errors::Error> {
self.clone().list_streams()
}
/// Get stream by ID
///
/// # Parameters
/// * `stream_id` - The ID of the stream
///
/// # Returns
/// * `Result<serde_json::Value, errors::Error>` - A JSON value containing the stream or an error
fn get_stream_by_id(&self, stream_id: String) -> Result<serde_json::Value, errors::Error> {
self.clone().get_stream_by_id(stream_id)
}
/// Get streams by user ID
///
/// # Parameters
/// * `user_id` - The ID of the user
///
/// # Returns
/// * `Result<crate::data::stream::Streams, errors::Error>` - A list of streams or an error
fn get_streams_by_user_id(
&self,
user_id: String,
) -> Result<crate::data::stream::Streams, errors::Error> {
self.clone().get_streams_by_user_id(user_id)
}
/// Get stream by playback ID
///
/// # Parameters
/// * `playback_id` - The ID of the playback
/// * `admin` - A boolean indicating if the request is made by an admin
///
/// # Returns
/// * `Result<serde_json::Value, crate::errors::Error>` - A JSON value containing the stream or an error
fn get_stream_by_playback_id(
&self,
playback_id: String,
admin: bool,
) -> Result<serde_json::Value, crate::errors::Error> {
self.clone().get_stream_by_playback_id(playback_id, admin)
}
/// Create a stream
///
/// # Parameters
/// * `name` - The name of the stream
/// * `profiles` - A list of profiles for the stream
/// * `playback_policy` - An optional playback policy
///
/// # Returns
/// * `Result<String, errors::Error>` - The ID of the created stream or an error
fn create_stream(
&self,
name: &String,
profiles: &Vec<crate::data::stream::Profile>,
playback_policy: Option<serde_json::Value>,
) -> Result<String, errors::Error> {
self.clone().create_stream(name, profiles, playback_policy)
}
}
impl Stream {
/// Create a new Stream instance
///
/// # Parameters
/// * `client` - A reference to the LivepeerClient
///
/// # Returns
/// * `Self` - A new instance of Stream
pub fn new(client: &crate::LivepeerClient) -> Self {
Stream {
client: client.clone(),
urls: crate::api::urls::LivepeerUrls::new(),
}
}
/// List all streams
///
/// # Returns
/// * `Result<crate::data::stream::Streams, errors::Error>` - A list of streams or an error
pub fn list_streams(self: Self) -> Result<crate::data::stream::Streams, errors::Error> {
let res: Result<serde_json::Value, errors::Error> = crate::utils::SurfRequest::get(
format!("{}{}", self.client.config.host, "/api/stream?streamsonly=1"),
self.client,
);
let mut r: Result<crate::data::stream::Streams, errors::Error> =
Err(errors::Error::LISTSTREAMS);
if res.is_ok() {
let streams = serde_json::from_value(res.unwrap()).unwrap();
r = Ok(streams)
}
r
}
/// Get stream by ID
///
/// # Parameters
/// * `stream_id` - The ID of the stream
///
/// # Returns
/// * `Result<serde_json::Value, errors::Error>` - A JSON value containing the stream or an error
pub fn get_stream_by_id(
self: Self,
stream_id: String,
) -> Result<serde_json::Value, errors::Error> {
let res: Result<serde_json::Value, errors::Error> = crate::utils::SurfRequest::get(
format!("{}{}/{}", self.client.config.host, "/api/stream", stream_id),
self.client,
);
res
}
/// Get stream by playback ID
///
/// # Parameters
/// * `playback_id` - The ID of the playback
/// * `admin` - A boolean indicating if the request is made by an admin
///
/// # Returns
/// * `Result<serde_json::Value, errors::Error>` - A JSON value containing the stream or an error
pub fn get_stream_by_playback_id(
self: Self,
playback_id: String,
admin: bool,
) -> Result<serde_json::Value, errors::Error> {
let mut admin_string = String::new();
if admin {
admin_string = String::from("&allUsers=true&all=true");
}
let res: Result<serde_json::Value, errors::Error> = crate::utils::SurfRequest::get(
format!(
r#"{}{}?filters=[{{"id":"playbackId","value":"{}"}}]{}"#,
self.client.config.host, "/api/stream", playback_id, admin_string
),
self.client,
);
res
}
/// Get streams by user ID
///
/// # Parameters
/// * `user_id` - The ID of the user
///
/// # Returns
/// * `Result<crate::data::stream::Streams, errors::Error>` - A list of streams or an error
pub fn get_streams_by_user_id(
self: Self,
user_id: String,
) -> Result<crate::data::stream::Streams, errors::Error> {
let res: Result<serde_json::Value, errors::Error> = crate::utils::SurfRequest::get(
format!(
r#"{}{}?allUsers=true&streamsonly=1&order=createdAt-true&limit=1000&filters=[{{"id":"userId","value":"{}"}}]"#,
self.client.config.host, "/api/stream", user_id
),
self.client,
);
let mut r: Result<crate::data::stream::Streams, errors::Error> =
Err(errors::Error::LISTSTREAMS);
if res.is_ok() {
let streams = serde_json::from_value(res.unwrap()).unwrap();
r = Ok(streams)
}
r
}
/// Create a stream
///
/// # Parameters
/// * `name` - The name of the stream
/// * `profiles` - A list of profiles for the stream
/// * `playback_policy` - An optional playback policy
///
/// # Returns
/// * `Result<String, errors::Error>` - The ID of the created stream or an error
pub fn create_stream(
self: Self,
name: &String,
profiles: &Vec<crate::data::stream::Profile>,
playback_policy: Option<serde_json::Value>,
) -> Result<String, errors::Error> {
let mut result: Result<String, errors::Error> = Err(errors::Error::CREATESTREAM);
let mut stream_id: String = "".to_string();
let mut data = serde_json::json!({
"name": name,
"playbackPolicy": playback_policy.unwrap(),
//"profiles": profiles,
});
let res: Result<serde_json::Value, errors::Error> = crate::utils::SurfRequest::post(
format!("{}{}", self.client.config.host, "/api/stream"),
serde_json::to_string(&data).unwrap(),
self.client,
);
if res.is_ok() {
let stream: serde_json::Value = serde_json::from_value(res.unwrap()).unwrap();
stream_id = stream["id"].to_string();
result = Ok(stream_id)
}
result
}
}