[BE] Log http request responses made to the online classroom

This commit is contained in:
Araozu 2023-10-05 12:31:02 -05:00
parent 6906e5f788
commit 8dbb0cd940
2 changed files with 55 additions and 8 deletions

View File

@ -1,10 +1,13 @@
use crate::online_classroom::register_course::register_course;
use crate::{
json_result::JsonResult,
model::{classroom_user::{ClassroomPersonCreate, ClassroomCourseRegistration}, person::PersonLink},
model::{
classroom_user::{ClassroomCourseRegistration, ClassroomPersonCreate},
person::PersonLink,
},
online_classroom::{create_user::create, get_courses::ClassroomCourse},
};
use rocket::{http::Status, serde::json::Json};
use crate::online_classroom::register_course::register_course;
#[options("/classroom/user")]
pub fn create_user_options() -> Status {
@ -12,7 +15,9 @@ pub fn create_user_options() -> Status {
}
#[post("/classroom/user", format = "json", data = "<data>")]
pub async fn create_user(data: Json<ClassroomPersonCreate>) -> (Status, Json<JsonResult<PersonLink>>) {
pub async fn create_user(
data: Json<ClassroomPersonCreate>,
) -> (Status, Json<JsonResult<PersonLink>>) {
match create(&data.0).await {
Ok(p) => return (Status::Ok, JsonResult::ok(p)),
Err(err) => return (Status::InternalServerError, JsonResult::err(err)),
@ -28,10 +33,15 @@ pub async fn get_courses(user_id: i32) -> (Status, Json<JsonResult<Vec<Classroom
}
#[options("/classroom/course/<_u>")]
pub fn register_course_contr_options(_u: i32) -> Status {Status::Ok}
pub fn register_course_contr_options(_u: i32) -> Status {
Status::Ok
}
#[post("/classroom/course/<user_id>", format = "json", data = "<data>")]
pub async fn register_course_contr(user_id: i32, data: Json<ClassroomCourseRegistration>) -> (Status, Json<JsonResult<()>>) {
pub async fn register_course_contr(
user_id: i32,
data: Json<ClassroomCourseRegistration>,
) -> (Status, Json<JsonResult<()>>) {
match register_course(user_id, &data.surname_first_letter, &data.courses).await {
Ok(()) => return (Status::Ok, JsonResult::ok(())),
Err(err) => return (Status::InternalServerError, JsonResult::err(err)),

View File

@ -1,3 +1,4 @@
use chrono::{DateTime, Local, TimeZone, Utc};
use lazy_static::lazy_static;
use std::time::{SystemTime, UNIX_EPOCH};
@ -39,7 +40,19 @@ pub async fn request(url: String) -> Result<String, String> {
};
match response.text() {
Ok(t) => Ok(t),
Ok(t) => {
// Get current time and date in iso
let now: DateTime<Local> = Local::now();
let now = now.to_rfc3339();
// Write html to file
let r = std::fs::write(format!("request-logs/{}.html", now), &t);
if let Err(err) = r {
eprintln!("Error writing request html to file: {:?}", err)
}
Ok(t)
}
Err(err) => Err(format!("Error getting text from response: {:?}", err)),
}
}
@ -80,7 +93,19 @@ pub async fn create_user_request(url: String, body: String) -> Result<String, St
}
match response.text() {
Ok(t) => Ok(t),
Ok(t) => {
// Get current time and date in iso
let now: DateTime<Local> = Local::now();
let now = now.to_rfc3339();
// Write html to file
let r = std::fs::write(format!("request-logs/{}.html", now), &t);
if let Err(err) = r {
eprintln!("Error writing request html to file: {:?}", err)
}
Ok(t)
},
Err(err) => Err(format!("Error getting text from response: {:?}", err)),
}
}
@ -108,7 +133,19 @@ pub async fn register_courses_request(url: String, body: String) -> Result<Strin
};
match response.text() {
Ok(t) => Ok(t),
Ok(t) => {
// Get current time and date in iso
let now: DateTime<Local> = Local::now();
let now = now.to_rfc3339();
// Write html to file
let r = std::fs::write(format!("request-logs/{}.html", now), &t);
if let Err(err) = r {
eprintln!("Error writing request html to file: {:?}", err)
}
Ok(t)
},
Err(err) => Err(format!("Error getting text from response: {:?}", err)),
}
}