mirror of
https://github.com/asciinema/asciinema.git
synced 2025-12-16 11:48:13 +01:00
Refactor api module
This commit is contained in:
73
src/api.rs
73
src/api.rs
@@ -32,16 +32,9 @@ pub fn get_auth_url(config: &Config) -> Result<Url> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn upload_asciicast(path: &str, config: &Config) -> Result<UploadAsciicastResponse> {
|
pub fn upload_asciicast(path: &str, config: &Config) -> Result<UploadAsciicastResponse> {
|
||||||
let client = Client::new();
|
let server_url = &config.get_server_url()?;
|
||||||
let form = Form::new().file("asciicast", path)?;
|
let install_id = config.get_install_id()?;
|
||||||
|
let response = upload_request(server_url, path, install_id)?.send()?;
|
||||||
let response = client
|
|
||||||
.post(upload_url(&config.get_server_url()?))
|
|
||||||
.multipart(form)
|
|
||||||
.basic_auth(get_username(), Some(config.get_install_id()?))
|
|
||||||
.header(header::USER_AGENT, build_user_agent())
|
|
||||||
.header(header::ACCEPT, "application/json")
|
|
||||||
.send()?;
|
|
||||||
|
|
||||||
if response.status().as_u16() == 413 {
|
if response.status().as_u16() == 413 {
|
||||||
bail!("The size of the recording exceeds the server's configured limit");
|
bail!("The size of the recording exceeds the server's configured limit");
|
||||||
@@ -52,11 +45,26 @@ pub fn upload_asciicast(path: &str, config: &Config) -> Result<UploadAsciicastRe
|
|||||||
Ok(response.json::<UploadAsciicastResponse>()?)
|
Ok(response.json::<UploadAsciicastResponse>()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn upload_request(server_url: &Url, path: &str, install_id: String) -> Result<RequestBuilder> {
|
||||||
|
let client = Client::new();
|
||||||
|
let mut url = server_url.clone();
|
||||||
|
url.set_path("api/asciicasts");
|
||||||
|
let form = Form::new().file("asciicast", path)?;
|
||||||
|
|
||||||
|
Ok(client
|
||||||
|
.post(url)
|
||||||
|
.multipart(form)
|
||||||
|
.basic_auth(get_username(), Some(install_id))
|
||||||
|
.header(header::USER_AGENT, build_user_agent())
|
||||||
|
.header(header::ACCEPT, "application/json"))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_user_stream(stream_id: String, config: &Config) -> Result<GetUserStreamResponse> {
|
pub fn create_user_stream(stream_id: String, config: &Config) -> Result<GetUserStreamResponse> {
|
||||||
let server_url = config.get_server_url()?;
|
let server_url = config.get_server_url()?;
|
||||||
let server_hostname = server_url.host().unwrap();
|
let server_hostname = server_url.host().unwrap();
|
||||||
|
let install_id = config.get_install_id()?;
|
||||||
|
|
||||||
let response = user_stream_request(&server_url, stream_id, config)?
|
let response = user_stream_request(&server_url, stream_id, install_id)
|
||||||
.send()
|
.send()
|
||||||
.context("cannot obtain stream producer endpoint")?;
|
.context("cannot obtain stream producer endpoint")?;
|
||||||
|
|
||||||
@@ -80,11 +88,26 @@ pub fn create_user_stream(stream_id: String, config: &Config) -> Result<GetUserS
|
|||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn upload_url(server_url: &Url) -> Url {
|
fn user_stream_request(
|
||||||
|
server_url: &Url,
|
||||||
|
stream_id: String,
|
||||||
|
install_id: String,
|
||||||
|
) -> RequestBuilder {
|
||||||
|
let client = Client::new();
|
||||||
let mut url = server_url.clone();
|
let mut url = server_url.clone();
|
||||||
url.set_path("api/asciicasts");
|
|
||||||
|
|
||||||
url
|
let builder = if stream_id.is_empty() {
|
||||||
|
url.set_path("api/streams");
|
||||||
|
client.post(url)
|
||||||
|
} else {
|
||||||
|
url.set_path(&format!("api/user/streams/{stream_id}"));
|
||||||
|
client.get(url)
|
||||||
|
};
|
||||||
|
|
||||||
|
builder
|
||||||
|
.basic_auth(get_username(), Some(install_id))
|
||||||
|
.header(header::USER_AGENT, build_user_agent())
|
||||||
|
.header(header::ACCEPT, "application/json")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_username() -> String {
|
fn get_username() -> String {
|
||||||
@@ -101,25 +124,3 @@ fn build_user_agent() -> String {
|
|||||||
|
|
||||||
ua.to_owned()
|
ua.to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn user_stream_request(
|
|
||||||
server_url: &Url,
|
|
||||||
stream_id: String,
|
|
||||||
config: &Config,
|
|
||||||
) -> Result<RequestBuilder> {
|
|
||||||
let client = Client::new();
|
|
||||||
let mut url = server_url.clone();
|
|
||||||
|
|
||||||
let builder = if stream_id.is_empty() {
|
|
||||||
url.set_path("api/streams");
|
|
||||||
client.post(url)
|
|
||||||
} else {
|
|
||||||
url.set_path(&format!("api/user/streams/{stream_id}"));
|
|
||||||
client.get(url)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(builder
|
|
||||||
.basic_auth(get_username(), Some(config.get_install_id()?))
|
|
||||||
.header(header::USER_AGENT, build_user_agent())
|
|
||||||
.header(header::ACCEPT, "application/json"))
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user