mod event;
mod kwai;
mod stream;
pub use event::*;
use futures::Stream;
use reqwest::Proxy;
use serde::{Deserialize, Serialize};
use stream::EventStream;
#[derive(Default, Debug)]
pub struct ConnectParams {
pub host: String,
pub app_id: String,
pub code: String,
pub play_id: Option<u32>,
pub header: Option<String>,
pub role_name: Option<String>,
pub http_proxys: Vec<Proxy>,
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ConnectReq {
pub host: String,
pub app_id: String,
pub code: String,
pub play_id: Option<u32>,
pub header: Option<String>,
pub role_name: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ConnectResp {
pub ks_uid: u32,
pub user: User,
pub token: String,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DisconnectParams {
pub host: String,
pub token: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct User {
pub id: String,
pub user_name: Option<String>,
pub head_url: Option<String>,
pub gender: Option<String>,
}
pub async fn connect(
params: ConnectParams,
) -> anyhow::Result<(ConnectResp, impl Stream<Item = Event>)> {
let mut http_client_builder = reqwest::ClientBuilder::new();
let req = ConnectReq {
host: params.host,
app_id: params.app_id,
code: params.code,
play_id: params.play_id,
header: params.header,
role_name: params.role_name,
};
for proxy in params.http_proxys {
http_client_builder = http_client_builder.proxy(proxy);
}
let http_client = http_client_builder.build()?;
let connect_resp = kwai::connect(&http_client, &req).await?;
let stream = EventStream::new(http_client, &req.host, connect_resp.token.clone())?;
Ok((connect_resp, stream.into_stream()))
}
pub async fn disconnect(params: &DisconnectParams) -> anyhow::Result<()> {
kwai::disconnect(params).await
}