Improve testability and remove boxing
Some checks failed
Core Repos/ImageApi/pipeline/pr-master There was a failure building this commit
Some checks failed
Core Repos/ImageApi/pipeline/pr-master There was a failure building this commit
Leverage generics to remove the extra heap allocation for the response handlers using Dao's. Also moved some of the environment variables to app state to allow for easier testing.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use actix_web::body::MessageBody;
|
||||
use actix_web::{body::BoxBody, HttpResponse};
|
||||
use serde::de::DeserializeOwned;
|
||||
use actix_web::{
|
||||
body::{BoxBody, MessageBody},
|
||||
HttpResponse,
|
||||
};
|
||||
|
||||
use crate::database::{models::User, UserDao};
|
||||
use std::cell::RefCell;
|
||||
@@ -65,17 +66,3 @@ impl BodyReader for HttpResponse<BoxBody> {
|
||||
std::str::from_utf8(&body).unwrap().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TypedBodyReader<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn read_body(self) -> T;
|
||||
}
|
||||
|
||||
impl<T: DeserializeOwned> TypedBodyReader<T> for HttpResponse<BoxBody> {
|
||||
fn read_body(self) -> T {
|
||||
let body = self.read_to_str();
|
||||
serde_json::from_value(serde_json::Value::String(body.clone())).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user