2022-10-14 16:31:03 +00:00
|
|
|
use crate::{proto, token};
|
|
|
|
use anyhow::{anyhow, Result};
|
2022-10-17 09:24:09 +00:00
|
|
|
use prost::Message;
|
|
|
|
use reqwest::header::CONTENT_TYPE;
|
2022-10-17 12:03:44 +00:00
|
|
|
use std::{future::Future, sync::Arc};
|
2022-10-14 15:00:38 +00:00
|
|
|
|
2022-10-17 12:03:44 +00:00
|
|
|
#[derive(Clone)]
|
2022-10-14 15:00:38 +00:00
|
|
|
pub struct Client {
|
2022-10-17 09:24:09 +00:00
|
|
|
http: reqwest::Client,
|
2022-10-17 12:03:44 +00:00
|
|
|
uri: Arc<str>,
|
|
|
|
key: Arc<str>,
|
|
|
|
secret: Arc<str>,
|
2022-10-14 15:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2022-10-17 09:24:09 +00:00
|
|
|
pub fn new(mut uri: String, key: String, secret: String) -> Self {
|
|
|
|
if uri.ends_with('/') {
|
|
|
|
uri.pop();
|
|
|
|
}
|
|
|
|
|
2022-10-14 15:00:38 +00:00
|
|
|
Self {
|
2022-10-17 09:24:09 +00:00
|
|
|
http: reqwest::Client::new(),
|
2022-10-17 12:03:44 +00:00
|
|
|
uri: uri.into(),
|
|
|
|
key: key.into(),
|
|
|
|
secret: secret.into(),
|
2022-10-14 15:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 16:31:03 +00:00
|
|
|
pub fn create_room(&self, name: String) -> impl Future<Output = Result<proto::Room>> {
|
2022-10-17 09:24:09 +00:00
|
|
|
self.request(
|
|
|
|
"twirp/livekit.RoomService/CreateRoom",
|
2022-10-14 16:31:03 +00:00
|
|
|
token::VideoGrant {
|
|
|
|
room_create: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2022-10-17 09:24:09 +00:00
|
|
|
proto::CreateRoomRequest {
|
|
|
|
name,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_room(&self, name: String) -> impl Future<Output = Result<()>> {
|
|
|
|
let response = self.request(
|
|
|
|
"twirp/livekit.RoomService/DeleteRoom",
|
|
|
|
token::VideoGrant {
|
|
|
|
room_create: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
proto::DeleteRoomRequest { room: name },
|
2022-10-14 16:31:03 +00:00
|
|
|
);
|
2022-10-17 09:24:09 +00:00
|
|
|
async move {
|
2022-10-17 12:03:44 +00:00
|
|
|
let _: proto::DeleteRoomResponse = response.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_participant(
|
|
|
|
&self,
|
|
|
|
room: String,
|
|
|
|
identity: String,
|
|
|
|
) -> impl Future<Output = Result<()>> {
|
|
|
|
let response = self.request(
|
|
|
|
"twirp/livekit.RoomService/RemoveParticipant",
|
|
|
|
token::VideoGrant {
|
|
|
|
room_admin: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
proto::RoomParticipantIdentity { room, identity },
|
|
|
|
);
|
|
|
|
async move {
|
|
|
|
let _: proto::RemoveParticipantResponse = response.await?;
|
2022-10-17 09:24:09 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2022-10-14 15:00:38 +00:00
|
|
|
|
2022-10-17 10:20:55 +00:00
|
|
|
pub fn room_token_for_user(&self, room: &str, identity: &str) -> Result<String> {
|
|
|
|
token::create(
|
|
|
|
&self.key,
|
|
|
|
&self.secret,
|
|
|
|
Some(identity),
|
|
|
|
token::VideoGrant {
|
|
|
|
room: Some(room),
|
|
|
|
room_join: Some(true),
|
|
|
|
can_publish: Some(true),
|
|
|
|
can_subscribe: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:24:09 +00:00
|
|
|
fn request<Req, Res>(
|
|
|
|
&self,
|
|
|
|
path: &str,
|
|
|
|
grant: token::VideoGrant,
|
|
|
|
body: Req,
|
|
|
|
) -> impl Future<Output = Result<Res>>
|
|
|
|
where
|
|
|
|
Req: Message,
|
|
|
|
Res: Default + Message,
|
|
|
|
{
|
2022-10-14 16:31:03 +00:00
|
|
|
let client = self.http.clone();
|
2022-10-17 09:24:09 +00:00
|
|
|
let token = token::create(&self.key, &self.secret, None, grant);
|
|
|
|
let uri = format!("{}/{}", self.uri, path);
|
2022-10-14 16:31:03 +00:00
|
|
|
async move {
|
|
|
|
let token = token?;
|
2022-10-17 09:24:09 +00:00
|
|
|
let response = client
|
|
|
|
.post(&uri)
|
|
|
|
.header(CONTENT_TYPE, "application/protobuf")
|
|
|
|
.bearer_auth(token)
|
|
|
|
.body(body.encode_to_vec())
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
if response.status().is_success() {
|
|
|
|
Ok(Res::decode(response.bytes().await?)?)
|
|
|
|
} else {
|
|
|
|
Err(anyhow!(
|
|
|
|
"POST {} failed with status code {:?}, {:?}",
|
|
|
|
uri,
|
|
|
|
response.status(),
|
|
|
|
response.text().await
|
|
|
|
))
|
|
|
|
}
|
2022-10-14 16:31:03 +00:00
|
|
|
}
|
2022-10-14 15:00:38 +00:00
|
|
|
}
|
|
|
|
}
|