use frakt::{Body, Client, MultipartPart};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Creating NSURLSession client...");
let client = Client::builder()
.timeout(Duration::from_secs(30))
.user_agent("frakt/0.1.0")
.build()?;
let test_file_content = "Hello, this is a test file content!\nLine 2 of the file.\n";
tokio::fs::write("test_upload.txt", test_file_content).await?;
println!("Testing simple file upload...");
let body = Body::from_file("test_upload.txt", Some("text/plain".to_string())).await?;
let response = client
.post("https://httpbin.org/post")
.body(body)
.send()
.await?;
println!("Simple upload response status: {}", response.status());
println!("Testing multipart file upload...");
let mut parts = vec![
MultipartPart::text("description", "This is a test file upload"),
MultipartPart::text("type", "text"),
];
let file_part =
MultipartPart::from_file("file", "test_upload.txt", Some("text/plain".to_string())).await?;
parts.push(file_part);
let multipart_body = Body::multipart(parts);
let response = client
.post("https://httpbin.org/post")
.body(multipart_body)
.send()
.await?;
println!("Multipart upload response status: {}", response.status());
let response_text = response.text().await?;
println!("Response body:\n{}", response_text);
tokio::fs::remove_file("test_upload.txt").await?;
Ok(())
}