add upload file
This commit is contained in:
parent
12a332dac2
commit
851f395236
@ -4,6 +4,7 @@ use reqwest::{Response, Error, IntoUrl, Method};
|
||||
use std::env;
|
||||
use dotenv::dotenv;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ApiError {
|
||||
IncorrectRequest(reqwest::Response),
|
||||
EmptyError(reqwest::Error),
|
||||
@ -30,12 +31,18 @@ impl ApiBuilder {
|
||||
|
||||
pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder {
|
||||
dotenv().ok();
|
||||
// todo remove env
|
||||
let host = env::var("HOST").unwrap();
|
||||
let username = env::var("USERNAME").unwrap();
|
||||
let root = env::var("ROOT").unwrap();
|
||||
let mut url = String::from(host);
|
||||
url.push_str("/remote.php/dav/files/");
|
||||
url.push_str(&username);
|
||||
url.push_str("/");
|
||||
url.push_str(&root);
|
||||
url.push_str("/");
|
||||
url.push_str(path);
|
||||
dbg!(url.clone());
|
||||
self.request = Some(self.client.request(method, url));
|
||||
self
|
||||
}
|
||||
@ -70,6 +77,20 @@ impl ApiBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_body(&mut self, body: Vec<u8>) -> &mut ApiBuilder {
|
||||
match self.request.take() {
|
||||
None => {
|
||||
eprintln!("fatal: incorrect request");
|
||||
std::process::exit(1);
|
||||
},
|
||||
Some(req) => {
|
||||
self.request = Some(req.body(body));
|
||||
}
|
||||
}
|
||||
self
|
||||
|
||||
}
|
||||
|
||||
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||
self.set_auth();
|
||||
match self.request.take() {
|
||||
|
@ -72,35 +72,16 @@ impl ReqProps {
|
||||
self.api_builder.send().await
|
||||
}
|
||||
|
||||
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
|
||||
pub async fn send_with_err(&mut self) -> Result<Vec<String>, ApiError> {
|
||||
let res = self.send().await.map_err(ApiError::RequestError)?;
|
||||
if res.status().is_success() {
|
||||
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
||||
Ok(body)
|
||||
Ok(self.parse(body))
|
||||
} else {
|
||||
Err(ApiError::IncorrectRequest(res))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_with_res(&mut self) -> Vec<String> {
|
||||
match self.send_with_err().await {
|
||||
Ok(body) => self.parse(body),
|
||||
Err(ApiError::IncorrectRequest(err)) => {
|
||||
eprintln!("fatal: {}", err.status());
|
||||
std::process::exit(1);
|
||||
},
|
||||
Err(ApiError::EmptyError(_)) => {
|
||||
eprintln!("Failed to get body");
|
||||
vec![]
|
||||
}
|
||||
Err(ApiError::RequestError(err)) => {
|
||||
dbg!("req erro");
|
||||
eprintln!("fatal: {}", err);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(&self, xml: String) -> Vec<String> {
|
||||
let cursor = Cursor::new(xml);
|
||||
let parser = EventReader::new(cursor);
|
||||
|
@ -1,34 +1,47 @@
|
||||
async fn upload_file(path: &str) -> Result<(), Box<dyn Error>> {
|
||||
dotenv().ok();
|
||||
use xml::reader::{EventReader, XmlEvent};
|
||||
use std::fs::File;
|
||||
use crate::services::api::{ApiBuilder, ApiError};
|
||||
use std::path::PathBuf;
|
||||
use std::io::{self, Read};
|
||||
use reqwest::{Method, IntoUrl, Response, Error};
|
||||
|
||||
let mut api_endpoint = env::var("HOST").unwrap().to_owned();
|
||||
api_endpoint.push_str("/remote.php/dav/files/");
|
||||
let username = env::var("USERNAME").unwrap();
|
||||
api_endpoint.push_str(&username);
|
||||
api_endpoint.push_str("/test/ok");
|
||||
let password = env::var("PASSWORD").unwrap();
|
||||
|
||||
let mut file = File::open("./file.test")?;
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
|
||||
// Create a reqwest client
|
||||
let client = Client::new();
|
||||
|
||||
// Send the request
|
||||
let response = client
|
||||
.request(reqwest::Method::PUT, api_endpoint)
|
||||
.basic_auth(username, Some(password))
|
||||
.body(buffer)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
// Handle the response
|
||||
if response.status().is_success() {
|
||||
let body = response.text().await?;
|
||||
println!("Response body: {}", body);
|
||||
} else {
|
||||
println!("Request failed with status code: {}", response.status());
|
||||
}
|
||||
Ok(())
|
||||
pub struct UploadFile {
|
||||
api_builder: ApiBuilder,
|
||||
}
|
||||
|
||||
impl UploadFile {
|
||||
pub fn new() -> Self {
|
||||
UploadFile {
|
||||
api_builder: ApiBuilder::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_url(&mut self, url: &str) -> &mut UploadFile {
|
||||
self.api_builder.build_request(Method::PUT, url);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_file(&mut self, path: PathBuf) -> &mut UploadFile {
|
||||
// todo large file
|
||||
// todo small files
|
||||
let mut file = File::open(path).unwrap();
|
||||
let mut buffer = Vec::new();
|
||||
file.read_to_end(&mut buffer).unwrap();
|
||||
self.api_builder.set_body(buffer);
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||
self.api_builder.send().await
|
||||
}
|
||||
|
||||
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
|
||||
let res = self.send().await.map_err(ApiError::RequestError)?;
|
||||
if res.status().is_success() {
|
||||
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
||||
Ok(body)
|
||||
} else {
|
||||
Err(ApiError::IncorrectRequest(res))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user