1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
extern crate reqwest; extern crate serde; extern crate serde_json; use serde_json::json; use serde::Deserialize; use std::collections::HashMap; use reqwest::blocking::Response; use reqwest::Error; #[derive(Deserialize, Debug)] struct ApiError { code: i32, message: String, } #[derive(Deserialize, Debug)] enum ApiResponse<T> { #[serde(rename = "success")] Success(T), #[serde(rename = "error")] Error(ApiError), } #[derive(Deserialize, Debug)] struct ApiCredentials { token: String } #[derive(Debug, Clone)] pub struct Client { instance_url: String, api_key: Option<String>, client: reqwest::blocking::Client, } impl Client { pub fn new(instance_url: String) -> Self { Client { instance_url, api_key: None, client: reqwest::blocking::Client::builder() .cookie_store(true) .build() .expect("Can't build http client"), } } pub fn url(&self, path: &str) -> String { format!("{}/{}", self.instance_url, path) } pub fn login_with_credential(&mut self, username: String, password: String) -> Result<Self, reqwest::Error> { let mut api_client = self.clone(); match api_client.client .post(&self.url("login")) .header("Accept", "application/json") .json(&json!({ "login": username, "password": password })) .send() { Ok(response) => { let result = response.json::<ApiResponse<ApiCredentials>>()?; match result { ApiResponse::Success(data) => { api_client.api_key = Some(data.token); } ApiResponse::Error(_) => {} } Ok(api_client) } Err(err) => { println!("Error : {:?}", err); Err(err) } } } pub fn login_yunohost(&mut self, username: String, password: String) -> Result<Self, reqwest::Error> { let mut api_client = self.clone(); let mut body: HashMap<String, String> = HashMap::new(); body.insert("user".to_string(), username); body.insert("password".to_string(), password); let response = api_client.client .post(&"https://apps.coop1d.com/yunohost/sso/".to_string()) .header( "User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0", ) .header( "Referer", "https://apps.coop1d.com/yunohost/sso/", ) .header( "Origin", "https://apps.coop1d.com", ) .form(&body) .send()?; Ok(api_client) } } #[cfg(test)] mod tests { use crate::Client; use reqwest::Error; #[test] fn create_client() { match Client::new("https://apps.coop1d.com/BOS/api/index.php".to_string()) .login_yunohost("alissonebos".to_string(), "alissonebos".to_string()) { Ok(mut client) => { println!("Yunohost {:?}", client); match client.login_with_credential("alissonebos".to_string(), "alissonebos".to_string()) { Ok(client) => { println!("Dolibarr {:?}", client) } Err(err) => { println!("{:?}", err) } } } Err(err) => println!("Err {:?}", err) } } }