Update to Actix 4
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
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use actix_web::dev::{Body, ResponseBody};
|
||||
use serde::Deserialize;
|
||||
use actix_web::body::MessageBody;
|
||||
use actix_web::{body::BoxBody, HttpResponse};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::database::{models::User, UserDao};
|
||||
use std::cell::RefCell;
|
||||
@@ -55,32 +56,26 @@ impl UserDao for TestUserDao {
|
||||
}
|
||||
|
||||
pub trait BodyReader {
|
||||
fn read_to_str(&self) -> &str;
|
||||
fn read_to_str(self) -> String;
|
||||
}
|
||||
|
||||
impl BodyReader for ResponseBody<Body> {
|
||||
fn read_to_str(&self) -> &str {
|
||||
match self {
|
||||
ResponseBody::Body(Body::Bytes(ref b)) => std::str::from_utf8(b).unwrap(),
|
||||
_ => panic!("Unknown response body"),
|
||||
}
|
||||
impl BodyReader for HttpResponse<BoxBody> {
|
||||
fn read_to_str(self) -> String {
|
||||
let body = self.into_body().try_into_bytes().unwrap();
|
||||
std::str::from_utf8(&body).unwrap().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TypedBodyReader<'a, T>
|
||||
pub trait TypedBodyReader<T>
|
||||
where
|
||||
T: Deserialize<'a>,
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
fn read_body(&'a self) -> T;
|
||||
fn read_body(self) -> T;
|
||||
}
|
||||
|
||||
impl<'a, T: Deserialize<'a>> TypedBodyReader<'a, T> for ResponseBody<Body> {
|
||||
fn read_body(&'a self) -> T {
|
||||
match self {
|
||||
ResponseBody::Body(Body::Bytes(ref b)) => {
|
||||
serde_json::from_str(std::str::from_utf8(b).unwrap()).unwrap()
|
||||
}
|
||||
_ => panic!("Unknown response body"),
|
||||
}
|
||||
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