This commit is contained in:
Dominic Grimm 2023-01-27 22:12:06 +01:00
parent f38d80d030
commit ff24aa7a2f
No known key found for this signature in database
GPG key ID: 6F294212DEAAC530

View file

@ -285,7 +285,7 @@ struct ApiTeachersResponse {
}
#[derive(Deserialize, Debug)]
pub struct ApiReportStudent {
pub struct ApiReportUser {
pub name: String,
#[serde(rename = "longName")]
pub long_name: String,
@ -746,7 +746,7 @@ impl Client {
Ok(resp.data.elements)
}
pub async fn student_reports(&self, class_id: i32) -> Result<Vec<ApiReportStudent>> {
pub async fn student_reports(&self, class_id: i32) -> Result<Vec<ApiReportUser>> {
#[derive(Deserialize, Debug)]
struct Data {
finished: bool,
@ -816,7 +816,80 @@ impl Client {
.from_reader(records.as_bytes());
Ok(reader
.deserialize::<ApiReportStudent>()
.deserialize::<ApiReportUser>()
.collect::<Result<Vec<_>, _>>()?)
}
pub async fn teacher_reports(&self, class_id: i32) -> Result<Vec<ApiReportUser>> {
#[derive(Deserialize, Debug)]
struct Data {
finished: bool,
error: bool,
#[serde(rename = "messageId")]
message_id: i32,
#[serde(rename = "reportParams")]
report_params: String,
}
#[derive(Deserialize, Debug)]
struct Response {
data: Data,
}
let mut url = self.webuntis_url.join("reports.do")?;
url.query_pairs_mut()
.append_pair("name", "Teacher")
.append_pair("format", "csv")
.append_pair("elementsForDate", "true");
let resp: Response = reqwest::Client::new()
.get(url)
.header(reqwest::header::USER_AGENT, &self.user_agent)
.header(reqwest::header::ACCEPT, "application/json")
.header(
reqwest::header::COOKIE,
self.session.as_ref().context("Not logged in")?,
)
.header(
reqwest::header::AUTHORIZATION,
Client::construct_bearer(&self.authorization.as_ref().context("Not logged in")?),
)
.send()
.await?
.json()
.await?;
if resp.data.error {
bail!("Error generating report");
} else if !resp.data.finished {
bail!("Report not finished");
}
let mut records_url = self.webuntis_url.join("reports.do")?;
records_url.set_query(Some(&resp.data.report_params));
records_url
.query_pairs_mut()
.append_pair("msgId", &resp.data.message_id.to_string());
let records = reqwest::Client::new()
.get(records_url)
.header(reqwest::header::USER_AGENT, &self.user_agent)
.header(reqwest::header::ACCEPT, "text/csv")
.header(
reqwest::header::COOKIE,
self.session.as_ref().context("Not logged in")?,
)
.header(
reqwest::header::AUTHORIZATION,
Client::construct_bearer(&self.authorization.as_ref().context("Not logged in")?),
)
.send()
.await?
.text()
.await?;
let mut reader = csv::ReaderBuilder::new()
.delimiter(b'\t')
.from_reader(records.as_bytes());
Ok(reader
.deserialize::<ApiReportUser>()
.collect::<Result<Vec<_>, _>>()?)
}
}