Hide Mouth::send_data as private, use wrapper functions to send stuff
This commit is contained in:
parent
52b54bfb84
commit
af80b3b62f
2 changed files with 48 additions and 46 deletions
61
src/ear.rs
61
src/ear.rs
|
@ -7,7 +7,7 @@ use std::net::{TcpListener, TcpStream};
|
|||
use urlencoding::decode;
|
||||
|
||||
use crate::database;
|
||||
use crate::mouth::{Mouth, Status};
|
||||
use crate::mouth::Mouth;
|
||||
|
||||
/// Corresponds to a listener object
|
||||
pub struct Ear {
|
||||
|
@ -51,10 +51,7 @@ impl Ear {
|
|||
|
||||
match parse_uri(&req) {
|
||||
Ok(req) => handle_request(req, sender),
|
||||
Err(req) => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
Err(req) => sender.send_bad_request(""),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -102,10 +99,7 @@ fn handle_request(request: Request, mut sender: Mouth) {
|
|||
match request.method.as_str() {
|
||||
"GET" => handle_get_request(request, sender),
|
||||
"POST" => handle_post_request(request, sender),
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
_ => sender.send_bad_request(""),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -115,32 +109,25 @@ fn handle_get_request(request: Request, mut sender: Mouth) {
|
|||
"titre" => match database::search_title_from_titre(&request.requested_data) {
|
||||
Ok(result) => {
|
||||
if result.len() == 0 {
|
||||
sender.send_data(Status::NotFound, serde_json::json!(result));
|
||||
sender.send_not_found(result);
|
||||
} else {
|
||||
sender.send_data(Status::OK, serde_json::json!(result));
|
||||
sender.send_ok(result);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
sender.send_data(Status::InternalError, serde_json::json!(format!("{}", err)))
|
||||
}
|
||||
Err(err) => sender.send_internal_error(format!("{}", err)),
|
||||
},
|
||||
"favoris" => match database::search_favorites_from_titre(&request.requested_data) {
|
||||
Ok(result) => {
|
||||
if result.len() == 0 {
|
||||
sender.send_data(Status::NotFound, serde_json::json!(result));
|
||||
sender.send_not_found(result);
|
||||
} else {
|
||||
sender.send_data(Status::OK, serde_json::json!(result));
|
||||
sender.send_ok(result);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
sender.send_data(Status::InternalError, serde_json::json!(format!("{}", err)))
|
||||
}
|
||||
Err(err) => sender.send_internal_error(format!("{}", err)),
|
||||
},
|
||||
"historique" => todo!(),
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
_ => sender.send_bad_request(""),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -151,35 +138,21 @@ fn handle_post_request(request: Request, mut sender: Mouth) {
|
|||
let mirabelid: i32 = match request.requested_data.trim().parse() {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!(
|
||||
"La requête est mal formée, veuillez lire la documentation."
|
||||
),
|
||||
);
|
||||
sender.send_bad_request("");
|
||||
return;
|
||||
}
|
||||
};
|
||||
match database::add_favorite(mirabelid) {
|
||||
Ok(result) => sender.send_data(
|
||||
Status::OK,
|
||||
serde_json::json!(format!("Favori inséré avec succès : {}", result.titre)),
|
||||
Ok(result) => sender.send_ok(
|
||||
format!("Favori inséré avec succès : {}", result.titre),
|
||||
),
|
||||
Err(err) => match err {
|
||||
database::DatabaseError::NotFound => {
|
||||
sender.send_data(Status::NotFound, serde_json::json!(format!("{}", err)))
|
||||
}
|
||||
database::DatabaseError::FoundDuplicate => {
|
||||
sender.send_data(Status::BadRequest, serde_json::json!(format!("{}", err)))
|
||||
}
|
||||
_ => sender
|
||||
.send_data(Status::InternalError, serde_json::json!(format!("{}", err))),
|
||||
database::DatabaseError::NotFound => sender.send_not_found(format!("{}", err)),
|
||||
database::DatabaseError::FoundDuplicate => sender.send_bad_request(format!("{}", err).as_str()),
|
||||
_ => sender.send_internal_error(format!("{}", err)),
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
_ => sender.send_bad_request(""),
|
||||
}
|
||||
}
|
||||
|
|
33
src/mouth.rs
33
src/mouth.rs
|
@ -3,8 +3,9 @@
|
|||
use std::{fmt::Display, io::Write, net::TcpStream};
|
||||
|
||||
use serde_json::Value;
|
||||
use serde::Serialize;
|
||||
|
||||
pub enum Status {
|
||||
enum Status {
|
||||
OK,
|
||||
BadRequest,
|
||||
InternalError,
|
||||
|
@ -33,11 +34,39 @@ impl<'a> Mouth<'a> {
|
|||
}
|
||||
|
||||
/// Send response data in JSON to a client.
|
||||
pub fn send_data(&mut self, status_code: Status, data: Value) {
|
||||
fn send_data(&mut self, status_code: Status, data: Value) {
|
||||
let contents = data.to_string();
|
||||
let length = contents.len();
|
||||
let response = format!("{status_code}\r\nContent-Type: application/json\r\nContent-Length: {length}\r\n\r\n{contents}");
|
||||
|
||||
self.stream.write_all(response.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
/// Send OK response to a client.
|
||||
pub fn send_ok<T>(&mut self, data: T) where T: Serialize {
|
||||
self.send_data(Status::OK, serde_json::json!(data));
|
||||
}
|
||||
|
||||
/// Send BadRequest response to a client.
|
||||
pub fn send_bad_request(&mut self, message: &str) {
|
||||
let data: Value = if message.is_empty() {
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation.")
|
||||
} else {
|
||||
serde_json::json!("")
|
||||
};
|
||||
self.send_data(
|
||||
Status::BadRequest,
|
||||
data,
|
||||
);
|
||||
}
|
||||
|
||||
/// Send InternalError response to a client.
|
||||
pub fn send_internal_error<T>(&mut self, data: T) where T: Serialize {
|
||||
self.send_data(Status::InternalError, serde_json::json!(data));
|
||||
}
|
||||
|
||||
/// Send NotFound response to a client.
|
||||
pub fn send_not_found<T>(&mut self, data: T) where T: Serialize {
|
||||
self.send_data(Status::NotFound, serde_json::json!(data));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue