Added documentation with 1 test

This commit is contained in:
Franklin 2022-08-23 09:37:34 -04:00
parent d35580e71c
commit 30ba69d025
4 changed files with 31 additions and 11 deletions

View File

@ -8,8 +8,6 @@ license = "MIT"
[lib] [lib]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
actix-web = "4.1.0" actix-web = "4.1.0"
serde_json = { version = "1" } serde_json = { version = "1" }

View File

@ -1,5 +1,10 @@
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
/// This is for sending errors back from requests.
/// This struct contains an optional key just in
/// case you want to deal with internationalization
/// It was left as optional just in case you don't
/// Have the time to yet...
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct MessageResource{ pub struct MessageResource{
pub key: Option<String>, pub key: Option<String>,
@ -7,9 +12,6 @@ pub struct MessageResource{
} }
impl MessageResource{ impl MessageResource{
pub fn new_from_str_with_type(msg: &str) -> MessageResource{
MessageResource { key: None, message: String::from(msg) }
}
pub fn new_empty() -> MessageResource{ pub fn new_empty() -> MessageResource{
MessageResource { key: None, message: String::from("") } MessageResource { key: None, message: String::from("") }
} }

View File

@ -3,12 +3,27 @@ use serde::Serialize;
use crate::dtos::message::MessageResource; use crate::dtos::message::MessageResource;
///
pub struct TypedHttpResponse<B: Serialize> { /// ```
///
/// use actix_web::{web::{Path}, http::StatusCode};
/// use actix_web_utils::extensions::typed_response::TypedHttpResponse;
/// use actix_web_utils::dtos::message::MessageResource;
///
/// //Sample route
/// pub async fn testroute(number: Path<i32>) -> TypedHttpResponse<String> {
/// if(*number > 0){
/// return TypedHttpResponse::return_standard_response(StatusCode::OK, String::from("This is my test response!"));
/// }
/// TypedHttpResponse::return_empty_response(StatusCode::BAD_REQUEST);
/// }
///
/// ```
pub struct TypedHttpResponse<B: Serialize = String> {
pub response: HttpResponse<Option<web::Json<Result<B, Vec<MessageResource>>>>>, pub response: HttpResponse<Option<web::Json<Result<B, Vec<MessageResource>>>>>,
} }
impl<B: Serialize> TypedHttpResponse<B> { impl<B: Serialize> TypedHttpResponse<B> {
/// Returns a response with the json struct inside + Status code /// Returns a response with the json struct you define inside + Status code
pub fn return_standard_response(status_code: StatusCode, body: B) -> TypedHttpResponse<B>{ pub fn return_standard_response(status_code: StatusCode, body: B) -> TypedHttpResponse<B>{
TypedHttpResponse { TypedHttpResponse {
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Ok(body)))) response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Ok(body))))
@ -20,18 +35,22 @@ impl<B: Serialize> TypedHttpResponse<B> {
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(body)))) response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(body))))
} }
} }
/// This is to return a MessageResource wrapped inside an HttpResponse
pub fn return_standard_error(status_code: StatusCode, body: MessageResource) -> TypedHttpResponse<B>{ pub fn return_standard_error(status_code: StatusCode, body: MessageResource) -> TypedHttpResponse<B>{
TypedHttpResponse { TypedHttpResponse {
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), Some(web::Json(Err(vec![body])))) 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 /// Returns an empty http response with a status code
/// This is a bad practice, but I still left it here
/// as it is useful for debugging & specific cases
pub fn return_empty_response(status_code: StatusCode) -> TypedHttpResponse<B>{ pub fn return_empty_response(status_code: StatusCode) -> TypedHttpResponse<B>{
TypedHttpResponse { TypedHttpResponse {
response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), None) response: HttpResponse::with_body(StatusCode::from_u16(u16::from(status_code)).unwrap(), None)
} }
} }
} }
impl<T: Serialize> Responder for TypedHttpResponse<T> impl<T: Serialize> Responder for TypedHttpResponse<T>
{ {
type Body = BoxBody; type Body = BoxBody;
@ -47,3 +66,4 @@ impl<T: Serialize> Responder for TypedHttpResponse<T>
} }
} }
} }