feat: Reporting
This commit is contained in:
@@ -3,11 +3,14 @@ mod models;
|
||||
mod schedule;
|
||||
mod teacher_absence;
|
||||
mod all;
|
||||
mod report;
|
||||
|
||||
use std::sync::RwLock;
|
||||
|
||||
pub use models::*;
|
||||
|
||||
use crate::report::{report_impl, ReportLocation};
|
||||
|
||||
uniffi::setup_scaffolding!();
|
||||
|
||||
#[derive(uniffi::Object)]
|
||||
@@ -43,4 +46,9 @@ impl JecnaSuplClient {
|
||||
let provider = self.provider_url.read().unwrap();
|
||||
all::get_all_impl(&provider)
|
||||
}
|
||||
|
||||
pub fn report(&self, content: String, class: String, report_location: ReportLocation) -> Result<(), SuplError> {
|
||||
let provider = self.provider_url.read().unwrap();
|
||||
report_impl(&provider, content, class, report_location)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ pub struct DailyData {
|
||||
#[serde(rename = "takesPlace")]
|
||||
pub takes_place: String,
|
||||
#[serde(rename = "reservedRooms")]
|
||||
pub reserved_rooms: Vec<String>,
|
||||
pub reserved_rooms: Vec<Option<String>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, uniffi::Record, Clone)]
|
||||
|
||||
61
src/report.rs
Normal file
61
src/report.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::SuplError;
|
||||
|
||||
#[derive(uniffi::Enum)]
|
||||
pub enum ReportLocation {
|
||||
Timetable,
|
||||
TeacherAbsence,
|
||||
TakesPlace
|
||||
}
|
||||
|
||||
impl ReportLocation {
|
||||
fn to_str(&self) -> &'static str {
|
||||
match self {
|
||||
ReportLocation::Timetable => "TIMETABLE",
|
||||
ReportLocation::TeacherAbsence => "ABSENCE",
|
||||
ReportLocation::TakesPlace => "TAKES_PLACE"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ReportRequest {
|
||||
content: String,
|
||||
class: String,
|
||||
location: String,
|
||||
}
|
||||
|
||||
pub fn report_impl(
|
||||
provider: &str,
|
||||
content: String,
|
||||
class: String,
|
||||
report_location: ReportLocation,
|
||||
) -> Result<(), SuplError> {
|
||||
let trimmed = provider.trim_end_matches('/');
|
||||
let url = format!("{}/versioned/v3", trimmed);
|
||||
|
||||
let body = ReportRequest {
|
||||
content,
|
||||
class,
|
||||
location: report_location.to_str().to_string(),
|
||||
};
|
||||
|
||||
let json = serde_json::to_string(&body)
|
||||
.map_err(|e| SuplError::ParseError { reason: e.to_string() })?;
|
||||
|
||||
let response = minreq::post(url)
|
||||
.with_header("Content-Type", "application/json")
|
||||
.with_body(json)
|
||||
.send()
|
||||
.map_err(|e| SuplError::NetworkError { reason: e.to_string() })?;
|
||||
|
||||
if response.status_code >= 400 {
|
||||
return Err(SuplError::RuntimeError {
|
||||
reason: format!("Server returned HTTP {}", response.status_code),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user