Small refactor
This commit is contained in:
parent
b55e05eeb4
commit
72843ff99a
@ -13,11 +13,11 @@ pub struct JsonError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<A> JsonResult<A> {
|
impl<A> JsonResult<A> {
|
||||||
pub fn error(reason: String) -> Json<JsonResult<A>> {
|
|
||||||
Json(JsonResult::Error(JsonError { reason }))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn ok(data: A) -> Json<JsonResult<A>> {
|
pub fn ok(data: A) -> Json<JsonResult<A>> {
|
||||||
Json(JsonResult::Ok(data))
|
Json(JsonResult::Ok(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn err(reason: String) -> Json<JsonResult<A>> {
|
||||||
|
Json(JsonResult::Error(JsonError { reason }))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,34 +4,17 @@ use reqwest::Client;
|
|||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use self::session::ensure_session;
|
use self::{json_result::JsonResult, session::ensure_session};
|
||||||
|
|
||||||
pub mod json_result;
|
pub mod json_result;
|
||||||
mod session;
|
mod session;
|
||||||
pub mod users;
|
pub mod users;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub enum ConnectionResult {
|
|
||||||
Ok(),
|
|
||||||
Error(ConnectionError),
|
|
||||||
}
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
|
||||||
pub struct ConnectionError {
|
|
||||||
reason: String,
|
|
||||||
}
|
|
||||||
fn new_error(reason: String) -> ConnectionResult {
|
|
||||||
ConnectionResult::Error(ConnectionError { reason })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tries to connect to the online classroom, and get a session cookie
|
/// Tries to connect to the online classroom, and get a session cookie
|
||||||
#[get("/classroom/connect")]
|
#[get("/classroom/connect")]
|
||||||
pub async fn connection() -> (Status, Json<ConnectionResult>) {
|
pub async fn connection() -> (Status, Json<JsonResult<()>>) {
|
||||||
match ensure_session().await {
|
match ensure_session().await {
|
||||||
Ok(_) => (Status::Ok, Json(ConnectionResult::Ok())),
|
Ok(_) => (Status::Ok, JsonResult::ok(())),
|
||||||
Err(err) => (Status::Ok, Json(new_error(err))),
|
Err(err) => (Status::Ok, JsonResult::err(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,11 +7,10 @@ static SESSION_COOKIE: OnceCell<String> = OnceCell::new();
|
|||||||
static SESSION_TIME: OnceCell<u64> = OnceCell::new();
|
static SESSION_TIME: OnceCell<u64> = OnceCell::new();
|
||||||
|
|
||||||
/// Makes a request to the online classroom, and returns the html string
|
/// Makes a request to the online classroom, and returns the html string
|
||||||
pub async fn request(url: String) -> Result<String, String> {
|
pub async fn _request(url: String) -> Result<String, String> {
|
||||||
todo!()
|
Ok(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Makes sure that the session cookie is set, and that it is valid
|
/// Makes sure that the session cookie is set, and that it is valid
|
||||||
pub async fn ensure_session() -> Result<(), String> {
|
pub async fn ensure_session() -> Result<(), String> {
|
||||||
let last_usage_time = match SESSION_TIME.get() {
|
let last_usage_time = match SESSION_TIME.get() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
|
|
||||||
use super::{ json_result::JsonResult};
|
use super::json_result::JsonResult;
|
||||||
|
|
||||||
// Instead of requesting pages and managing session & cookies manually,
|
// Instead of requesting pages and managing session & cookies manually,
|
||||||
// create a wrapper that:
|
// create a wrapper that:
|
||||||
|
Loading…
Reference in New Issue
Block a user