Initial commit
This commit is contained in:
commit
603d952def
|
@ -0,0 +1 @@
|
||||||
|
/target
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,14 @@
|
||||||
|
[package]
|
||||||
|
name = "actix-web-addons"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
authors = ["Franklin E. Blanco"]
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-web = "4.1.0"
|
||||||
|
serde_json = { version = "1" }
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
|
@ -0,0 +1,25 @@
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct MessageResource{
|
||||||
|
pub key: Option<String>,
|
||||||
|
pub message: String
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MessageResource{
|
||||||
|
pub fn new_from_str_with_type(msg: &str) -> MessageResource{
|
||||||
|
MessageResource { key: None, message: String::from(msg) }
|
||||||
|
}
|
||||||
|
pub fn new_empty() -> MessageResource{
|
||||||
|
MessageResource { key: None, message: String::from("") }
|
||||||
|
}
|
||||||
|
pub fn new_from_str(msg: &str) -> MessageResource{
|
||||||
|
MessageResource { key: None, message: String::from(msg) }
|
||||||
|
}
|
||||||
|
pub fn new_from_err(errstr: String) -> MessageResource{
|
||||||
|
MessageResource { key: None, message: errstr }
|
||||||
|
}
|
||||||
|
pub fn new(key: &str, msg: &str) -> MessageResource{
|
||||||
|
MessageResource { key: Some(String::from(key)), message: String::from(msg) }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod message;
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod typed_response;
|
|
@ -0,0 +1,49 @@
|
||||||
|
use actix_web::{HttpResponse, http::StatusCode, web, HttpRequest, HttpResponseBuilder, body::BoxBody, Responder};
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::dtos::message::MessageResource;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct TypedHttpResponse<B: Serialize> {
|
||||||
|
pub response: HttpResponse<Option<web::Json<Result<B, Vec<MessageResource>>>>>,
|
||||||
|
}
|
||||||
|
impl<B: Serialize> TypedHttpResponse<B> {
|
||||||
|
// Returns a response with the json struct inside + Status code
|
||||||
|
pub fn return_standard_response(status_code: StatusCode, body: B) -> TypedHttpResponse<B>{
|
||||||
|
TypedHttpResponse {
|
||||||
|
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Ok(body))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Returns a response with the json error list inside + Status code
|
||||||
|
pub fn return_standard_error_list(status_code: StatusCode, body: Vec<MessageResource>) -> TypedHttpResponse<B>{
|
||||||
|
TypedHttpResponse {
|
||||||
|
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(body))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn return_standard_error(status_code: StatusCode, body: MessageResource) -> TypedHttpResponse<B>{
|
||||||
|
TypedHttpResponse {
|
||||||
|
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(vec![body]))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Returns an empty response with status code
|
||||||
|
pub fn return_empty_response(status_code: StatusCode) -> TypedHttpResponse<B>{
|
||||||
|
TypedHttpResponse {
|
||||||
|
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T: Serialize> Responder for TypedHttpResponse<T>
|
||||||
|
{
|
||||||
|
type Body = BoxBody;
|
||||||
|
#[inline]
|
||||||
|
fn respond_to(self, _: &HttpRequest) -> HttpResponse<Self::Body> {
|
||||||
|
let mut builder = HttpResponseBuilder::new(self.response.status());
|
||||||
|
match self.response.body() {
|
||||||
|
Some(body) => {match body.0.as_ref() {
|
||||||
|
Ok(value) => builder.json(value),
|
||||||
|
Err(errors) => builder.json(errors),
|
||||||
|
}},
|
||||||
|
None => {builder.finish()}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod extensions;
|
||||||
|
pub mod dtos;
|
Loading…
Reference in New Issue