Skip to content
Snippets Groups Projects
Commit 60661fbe authored by HGEpro's avatar HGEpro
Browse files

added rust file

parent aef6b64c
No related branches found
No related tags found
No related merge requests found
use reqwest;
use serde::{Serialize, Deserialize};
use serde_json;
const APIURL: &str = "https://librehomework-api.herokuapp.com/";
#[derive(Deserialize, Serialize)]
pub struct ApiResponse {
error: Option<String>,
data: Option<String>,
}
impl std::convert::From<reqwest::Error> for ApiResponse {
fn from(e: reqwest::Error) -> Self {
ApiResponse {
error: Some(e.to_string()),
data: None,
}
}
}
#[tauri::command]
pub async fn request(url: &str, method: Option<&str>, body: Option<String>) -> Result<(ApiResponse, u16), String> {
let mut _url = url.to_string();
let client = reqwest::Client::new();
if &url.len() > &8 && &url[0..8] != "https://" {
_url = APIURL.to_owned() + &_url;
}
println!("{}", &_url);
let res = match method {
Some(m) => if m == "POST" {
client.post(_url).json(&body).send().await
} else {
client.get(_url).send().await
},
None => client.get(_url).send().await
}.map_err(|e| e.to_string())?;
let status = res.status().as_u16();
let apires: ApiResponse = res.json().await.map_err(|e| e.to_string())?;
Ok( (apires, status) )
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment