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 std::env;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum ApiError {
|
pub enum ApiError {
|
||||||
IncorrectRequest(reqwest::Response),
|
IncorrectRequest(reqwest::Response),
|
||||||
EmptyError(reqwest::Error),
|
EmptyError(reqwest::Error),
|
||||||
@ -30,12 +31,18 @@ impl ApiBuilder {
|
|||||||
|
|
||||||
pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder {
|
pub fn build_request(&mut self, method: Method, path: &str) -> &mut ApiBuilder {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
// todo remove env
|
||||||
let host = env::var("HOST").unwrap();
|
let host = env::var("HOST").unwrap();
|
||||||
let username = env::var("USERNAME").unwrap();
|
let username = env::var("USERNAME").unwrap();
|
||||||
|
let root = env::var("ROOT").unwrap();
|
||||||
let mut url = String::from(host);
|
let mut url = String::from(host);
|
||||||
url.push_str("/remote.php/dav/files/");
|
url.push_str("/remote.php/dav/files/");
|
||||||
url.push_str(&username);
|
url.push_str(&username);
|
||||||
|
url.push_str("/");
|
||||||
|
url.push_str(&root);
|
||||||
|
url.push_str("/");
|
||||||
url.push_str(path);
|
url.push_str(path);
|
||||||
|
dbg!(url.clone());
|
||||||
self.request = Some(self.client.request(method, url));
|
self.request = Some(self.client.request(method, url));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -70,6 +77,20 @@ impl ApiBuilder {
|
|||||||
self
|
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> {
|
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||||
self.set_auth();
|
self.set_auth();
|
||||||
match self.request.take() {
|
match self.request.take() {
|
||||||
|
@ -72,35 +72,16 @@ impl ReqProps {
|
|||||||
self.api_builder.send().await
|
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)?;
|
let res = self.send().await.map_err(ApiError::RequestError)?;
|
||||||
if res.status().is_success() {
|
if res.status().is_success() {
|
||||||
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
||||||
Ok(body)
|
Ok(self.parse(body))
|
||||||
} else {
|
} else {
|
||||||
Err(ApiError::IncorrectRequest(res))
|
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> {
|
pub fn parse(&self, xml: String) -> Vec<String> {
|
||||||
let cursor = Cursor::new(xml);
|
let cursor = Cursor::new(xml);
|
||||||
let parser = EventReader::new(cursor);
|
let parser = EventReader::new(cursor);
|
||||||
|
@ -1,34 +1,47 @@
|
|||||||
async fn upload_file(path: &str) -> Result<(), Box<dyn Error>> {
|
use xml::reader::{EventReader, XmlEvent};
|
||||||
dotenv().ok();
|
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();
|
pub struct UploadFile {
|
||||||
api_endpoint.push_str("/remote.php/dav/files/");
|
api_builder: ApiBuilder,
|
||||||
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")?;
|
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();
|
let mut buffer = Vec::new();
|
||||||
file.read_to_end(&mut buffer)?;
|
file.read_to_end(&mut buffer).unwrap();
|
||||||
|
self.api_builder.set_body(buffer);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
// Create a reqwest client
|
pub async fn send(&mut self) -> Result<Response, Error> {
|
||||||
let client = Client::new();
|
self.api_builder.send().await
|
||||||
|
}
|
||||||
|
|
||||||
// Send the request
|
pub async fn send_with_err(&mut self) -> Result<String, ApiError> {
|
||||||
let response = client
|
let res = self.send().await.map_err(ApiError::RequestError)?;
|
||||||
.request(reqwest::Method::PUT, api_endpoint)
|
if res.status().is_success() {
|
||||||
.basic_auth(username, Some(password))
|
let body = res.text().await.map_err(ApiError::EmptyError)?;
|
||||||
.body(buffer)
|
Ok(body)
|
||||||
.send()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Handle the response
|
|
||||||
if response.status().is_success() {
|
|
||||||
let body = response.text().await?;
|
|
||||||
println!("Response body: {}", body);
|
|
||||||
} else {
|
} else {
|
||||||
println!("Request failed with status code: {}", response.status());
|
Err(ApiError::IncorrectRequest(res))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user