librus_api/
lib.rs

1use reqwest::Client;
2use serde_json;
3use serde::__private::fmt::Debug;
4use serde::{Serialize, Deserialize};
5mod structs;
6
7use crate::structs::{me::*,grades::*,lessons::*,users::*,events::*};
8
9
10#[derive(Serialize, Deserialize, Debug)]
11struct LoginRequest {
12    action: String,
13    login: String,
14    pass: String,
15}
16
17pub struct UserAuthenticated{
18    pub client: Client,
19    pub status: bool,
20}
21
22impl UserAuthenticated{
23    // let client = 2; - how to set default variable in implementations (impl) Rust
24    pub fn hello(self){
25        println!("Hello Impl");
26        println!("{}", self.status)
27    }
28    async fn __get_api(&self, endpoint: &str) -> String{
29        let response = self.client.get("https://synergia.librus.pl/gateway/api/2.0/".to_owned() + &endpoint)
30            .header("Content-Type", "application/json")
31            .send()
32            .await
33            .unwrap();
34        let result = response.text().await.unwrap();
35        result
36    }
37    pub async fn me(&self) -> ResponseMe{
38        let json_string = self.__get_api("Me").await;
39        let me: ResponseMe = serde_json::from_str(&json_string).unwrap();
40        me
41    }
42    pub async fn grades(&self) -> ResponseGrades{
43        let json_string = self.__get_api("Grades").await;
44        let grades: ResponseGrades = serde_json::from_str(&json_string).unwrap();
45        grades
46    }
47
48    pub async fn grade_category(&self, num: i32) -> ResponseGradesCategories{
49        let json_string = self.__get_api(&("Grades/Categories/".to_owned() + &num.to_string())).await;
50        let category: ResponseGradesCategories = serde_json::from_str(&json_string).unwrap();
51        category
52    }
53    pub async fn grade_comment(&self, num: i32) -> ResponseGradesComments{
54        let json_string = self.__get_api(&("Grades/Comments/".to_owned() + &num.to_string())).await;
55        let comment: ResponseGradesComments = serde_json::from_str(&json_string).unwrap();
56        comment
57    }
58    pub async fn lesson(&self, num: i32) -> ResponseLesson{
59        let json_string = self.__get_api(&("Lessons/".to_owned() + &num.to_string())).await;
60        let lesson: ResponseLesson = serde_json::from_str(&json_string).unwrap();
61        lesson
62    }
63    pub async fn lesson_subject(&self, num: i32) -> ResponseLessonSubject{
64        let json_string = self.__get_api(&("Subjects/".to_owned() + &num.to_string())).await;
65        let lesson: ResponseLessonSubject = serde_json::from_str(&json_string).unwrap();
66        lesson
67    }
68    pub async fn attendances(&self) -> ResponseAttendances{
69        let json_string = self.__get_api(&("Attendances/".to_owned())).await;
70        let attendance: ResponseAttendances = serde_json::from_str(&json_string).unwrap();
71        attendance
72    }
73    pub async fn attendances_type(&self) -> ResponseAttendancesType{
74        let json_string = self.__get_api(&("Attendances/Types/".to_owned())).await;
75        let attendance: ResponseAttendancesType = serde_json::from_str(&json_string).unwrap();
76        attendance
77    }
78    /* Unavailable for this time, add in the next updates */
79    // pub async fn timetables(&self) -> ResponseTimetables{
80    //     let json_string = self.__get_api(&("Timetables/".to_owned())).await;
81    //     let timetables: ResponseTimetables = serde_json::from_str(&json_string).unwrap();
82    //     timetables
83    // }
84    
85    pub async fn homeworks(&self) -> ResponseHomeworks{
86        let json_string = self.__get_api(&("HomeWorks/".to_owned())).await;
87        let homeworks: ResponseHomeworks = serde_json::from_str(&json_string).unwrap();
88        homeworks
89    }
90    pub async fn user(&self, num: i32) -> ResponseUser{
91        let json_string = self.__get_api(&("Users/".to_owned() + &num.to_string())).await;
92        let user: ResponseUser = serde_json::from_str(&json_string).unwrap();
93        user
94    }
95    pub async fn user_me(&self) -> ResponseUser{
96        let json_string = self.__get_api("Users").await;
97        let user: ResponseUser = serde_json::from_str(&json_string).unwrap();
98        user
99    }
100
101}
102
103pub fn hello() -> String {
104    println!("Welcome");
105    "Hello\n".to_string()
106}
107
108pub async fn test_connect(){
109    let _result = reqwest::get("https://example.com").await;
110    match _result {
111        Ok(_v) => println!("[Connect] Test passed"),
112        Err(_e) => println!("[Connect] Network Error"),
113    };
114}
115
116
117
118pub async fn test_cookie(){
119    let resp = reqwest::get("http://webtest.5v.pl/cookie/").await.unwrap();
120
121    // Get the headers
122    let header = resp.headers();
123    let cookie_header = easycookie::set_header(header.clone()).await;
124    let first_cookie = cookie_header.get_cookie("random").await;
125    let second_cookie = cookie_header.get_cookie("random2").await;
126    println!("First Cookie Name: {:?} Value {:?}", "random", first_cookie.get_value());
127    println!("Second Cookie Name: {:?} Value {:?}", "random2", second_cookie.get_value());
128
129}
130pub async fn get_token(_login: &str, _pass: &str) -> UserAuthenticated{
131    let testauth_url = "https://api.librus.pl/OAuth/Authorization?client_id=46&response_type=code&scope=mydata";
132    let auth_url = "https://api.librus.pl/OAuth/Authorization?client_id=46";
133    let grand_url = "https://api.librus.pl/OAuth/Authorization/Grant?client_id=46";
134    let login_request = LoginRequest {
135        action: "login".to_string(),
136        login: _login.to_string(),
137        pass: _pass.to_string(),
138    };
139    let client = Client::builder().cookie_store(true).build().unwrap();
140    // First Authentication
141    let _ = client.get(testauth_url).send().await;
142
143    // Second Authentication
144    let _response = client.post(auth_url)
145        .form(&login_request)
146        .send()
147        .await.unwrap();
148
149    // Authenticate User from Cookie
150    let _ = client.get(grand_url).send().await;
151    let mut _status = false;
152    let token_info = client.get("https://synergia.librus.pl/gateway/api/2.0/Auth/TokenInfo/").send().await.unwrap();
153    if token_info.status() == 200{
154        // println!("Token Info: {}", token_info.text().await.unwrap());
155        _status = true;
156    }
157    else{
158        println!("[Librus API] Failed Authentication");
159        _status = false;
160    }
161    UserAuthenticated{client, status: _status}
162
163}
164
165
166#[test]
167fn test() {
168    hello();
169}