mod error;
mod structs;
use reqwest::Client as HttpClient;
pub use crate::error::Error;
pub use crate::structs::announcements::{ResponseSchoolNotices, SchoolNotice};
pub use crate::structs::events::{Homework, ResponseHomeworks};
pub use crate::structs::grades::{
Grade, GradeCategory, GradeComment, ResponseGrades, ResponseGradesCategories,
ResponseGradesComments,
};
pub use crate::structs::lessons::{
Attendance, AttendanceType, Lesson, LessonSubject, ResponseAttendances,
ResponseAttendancesType, ResponseLesson, ResponseLessonSubject,
};
pub use crate::structs::me::{Me, ResponseMe};
pub use crate::structs::messages::{
Attachment, InboxMessage, MessageDetail, OutboxMessage, UnreadCounts,
};
pub use crate::structs::users::{ResponseUser, User};
use crate::structs::messages::{
ResponseInboxMessages, ResponseMessageDetail, ResponseOutboxMessages, ResponseUnreadCounts,
};
pub type Result<T> = std::result::Result<T, Error>;
const SYNERGIA_API_BASE: &str = "https://synergia.librus.pl/gateway/api/2.0/";
const MESSAGES_API_BASE: &str = "https://wiadomosci.librus.pl/api/";
const AUTH_URL: &str = "https://api.librus.pl/OAuth/Authorization?client_id=46";
const AUTH_TEST_URL: &str =
"https://api.librus.pl/OAuth/Authorization?client_id=46&response_type=code&scope=mydata";
const AUTH_GRANT_URL: &str = "https://api.librus.pl/OAuth/Authorization/Grant?client_id=46";
const TOKEN_INFO_URL: &str = "https://synergia.librus.pl/gateway/api/2.0/Auth/TokenInfo/";
const MESSAGES_INIT_URL: &str = "https://synergia.librus.pl/wiadomosci3";
#[derive(Default)]
pub struct ClientBuilder {
username: Option<String>,
password: Option<String>,
}
impl ClientBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
pub fn password(mut self, password: impl Into<String>) -> Self {
self.password = Some(password.into());
self
}
pub async fn build(self) -> Result<Client> {
let username = self.username.ok_or(Error::MissingCredentials("username"))?;
let password = self.password.ok_or(Error::MissingCredentials("password"))?;
Client::authenticate(&username, &password).await
}
}
pub struct Client {
http: HttpClient,
messages_initialized: bool,
}
impl Client {
pub async fn from_env() -> Result<Self> {
let username = std::env::var("LIBRUS_USERNAME")
.map_err(|_| Error::MissingEnvVar("LIBRUS_USERNAME"))?;
let password = std::env::var("LIBRUS_PASSWORD")
.map_err(|_| Error::MissingEnvVar("LIBRUS_PASSWORD"))?;
Self::authenticate(&username, &password).await
}
pub async fn new(username: &str, password: &str) -> Result<Self> {
Self::authenticate(username, password).await
}
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}
async fn authenticate(username: &str, password: &str) -> Result<Self> {
let http = HttpClient::builder()
.cookie_store(true)
.build()
.map_err(Error::HttpClient)?;
let form_params = [("action", "login"), ("login", username), ("pass", password)];
http.get(AUTH_TEST_URL)
.send()
.await
.map_err(Error::Request)?;
http.post(AUTH_URL)
.form(&form_params)
.send()
.await
.map_err(Error::Request)?;
http.get(AUTH_GRANT_URL)
.send()
.await
.map_err(Error::Request)?;
let token_response = http
.get(TOKEN_INFO_URL)
.send()
.await
.map_err(Error::Request)?;
if token_response.status() != 200 {
return Err(Error::Authentication);
}
Ok(Self {
http,
messages_initialized: false,
})
}
async fn get_api(&self, endpoint: &str) -> Result<String> {
let url = format!("{}{}", SYNERGIA_API_BASE, endpoint);
let response = self
.http
.get(&url)
.header("Content-Type", "application/json")
.send()
.await
.map_err(Error::Request)?;
let status = response.status();
let text = response.text().await.map_err(Error::Request)?;
if !status.is_success() {
return Err(Error::ApiError {
status: status.as_u16(),
body: text,
});
}
Ok(text)
}
async fn get_messages_api(&self, endpoint: &str) -> Result<String> {
let url = format!("{}{}", MESSAGES_API_BASE, endpoint);
let response = self.http.get(&url).send().await.map_err(Error::Request)?;
let status = response.status();
let text = response.text().await.map_err(Error::Request)?;
if !status.is_success() {
return Err(Error::ApiError {
status: status.as_u16(),
body: text,
});
}
Ok(text)
}
async fn ensure_messages_initialized(&mut self) -> Result<()> {
if self.messages_initialized {
return Ok(());
}
self.http
.get(MESSAGES_INIT_URL)
.send()
.await
.map_err(Error::Request)?;
self.messages_initialized = true;
Ok(())
}
pub async fn me(&self) -> Result<ResponseMe> {
let json = self.get_api("Me").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn grades(&self) -> Result<ResponseGrades> {
let json = self.get_api("Grades").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn grade_category(&self, id: i32) -> Result<ResponseGradesCategories> {
let json = self.get_api(&format!("Grades/Categories/{}", id)).await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn grade_comment(&self, id: i32) -> Result<ResponseGradesComments> {
let json = self.get_api(&format!("Grades/Comments/{}", id)).await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn lesson(&self, id: i32) -> Result<ResponseLesson> {
let json = self.get_api(&format!("Lessons/{}", id)).await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn subject(&self, id: i32) -> Result<ResponseLessonSubject> {
let json = self.get_api(&format!("Subjects/{}", id)).await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn attendances(&self) -> Result<ResponseAttendances> {
let json = self.get_api("Attendances/").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn attendance_types(&self) -> Result<ResponseAttendancesType> {
let json = self.get_api("Attendances/Types/").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn homeworks(&self) -> Result<ResponseHomeworks> {
let json = self.get_api("HomeWorks/").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn school_notices(&self) -> Result<ResponseSchoolNotices> {
let json = self.get_api("SchoolNotices").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn school_notices_page(
&self,
page: u32,
limit: u32,
) -> Result<ResponseSchoolNotices> {
let endpoint = format!("SchoolNotices?page={}&limit={}", page, limit);
let json = self.get_api(&endpoint).await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn school_notices_latest(&self, limit: usize) -> Result<Vec<SchoolNotice>> {
if limit == 0 {
return Ok(Vec::new());
}
let page_size: u32 = 50;
let mut page = 1;
let mut all = Vec::new();
loop {
let resp = self.school_notices_page(page, page_size).await?;
if resp.school_notices.is_empty() {
break;
}
let count = resp.school_notices.len();
all.extend(resp.school_notices);
if count < page_size as usize {
break;
}
page += 1;
}
all.sort_by(|a, b| b.creation_date.cmp(&a.creation_date));
all.truncate(limit);
Ok(all)
}
pub async fn user(&self, id: i32) -> Result<ResponseUser> {
let json = self.get_api(&format!("Users/{}", id)).await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn current_user(&self) -> Result<ResponseUser> {
let json = self.get_api("Users").await?;
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})
}
pub async fn unread_counts(&mut self) -> Result<UnreadCounts> {
self.ensure_messages_initialized().await?;
let json = self.get_messages_api("inbox/unreadMessagesCount").await?;
let resp: ResponseUnreadCounts = serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})?;
Ok(resp.data)
}
pub async fn inbox_messages(&mut self, page: u32, limit: u32) -> Result<Vec<InboxMessage>> {
self.ensure_messages_initialized().await?;
let endpoint = format!("inbox/messages?page={}&limit={}", page, limit);
let json = self.get_messages_api(&endpoint).await?;
let resp: ResponseInboxMessages =
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})?;
Ok(resp.data)
}
pub async fn outbox_messages(&mut self, page: u32, limit: u32) -> Result<Vec<OutboxMessage>> {
self.ensure_messages_initialized().await?;
let endpoint = format!("outbox/messages?page={}&limit={}", page, limit);
let json = self.get_messages_api(&endpoint).await?;
let resp: ResponseOutboxMessages =
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})?;
Ok(resp.data)
}
pub async fn message(&mut self, message_id: &str) -> Result<MessageDetail> {
self.ensure_messages_initialized().await?;
let endpoint = format!("inbox/messages/{}", message_id);
let json = self.get_messages_api(&endpoint).await?;
let resp: ResponseMessageDetail =
serde_json::from_str(&json).map_err(|e| Error::Parse {
source: e,
body: json,
})?;
Ok(resp.data)
}
pub async fn attachment(&mut self, attachment_id: &str, message_id: &str) -> Result<Vec<u8>> {
self.ensure_messages_initialized().await?;
let url = format!(
"https://wiadomosci.librus.pl/api/attachments/{}/messages/{}",
attachment_id, message_id
);
let response = self.http.get(&url).send().await.map_err(Error::Request)?;
let status = response.status();
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
return Err(Error::ApiError {
status: status.as_u16(),
body,
});
}
let bytes = response.bytes().await.map_err(Error::Request)?;
Ok(bytes.to_vec())
}
pub fn decode_message_content(content: &str) -> Option<String> {
use base64::{engine::general_purpose::STANDARD, Engine};
STANDARD
.decode(content)
.ok()
.and_then(|bytes| String::from_utf8(bytes).ok())
}
pub fn notice_content_to_text(content: &str) -> String {
let mut out = String::with_capacity(content.len());
let mut in_tag = false;
for ch in content.chars() {
match ch {
'<' => in_tag = true,
'>' => in_tag = false,
_ if !in_tag => out.push(ch),
_ => {}
}
}
let out = out
.replace(" ", " ")
.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace(""", "\"")
.replace("'", "'");
out.trim().to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use base64::Engine;
#[test]
fn test_decode_message_content() {
let encoded = base64::engine::general_purpose::STANDARD.encode("Hello, World!");
let decoded = Client::decode_message_content(&encoded);
assert_eq!(decoded, Some("Hello, World!".to_string()));
}
#[test]
fn test_decode_invalid_content() {
let decoded = Client::decode_message_content("not valid base64!!!");
assert!(decoded.is_none());
}
#[test]
fn test_notice_content_to_text() {
let html = "<p>Hello <b>World</b> & friends</p>";
let text = Client::notice_content_to_text(html);
assert_eq!(text, "Hello World & friends");
}
}