From 2fb5f217b3900e0ff70742d6f46a8c13f7f26072 Mon Sep 17 00:00:00 2001 From: gudaoxuri Date: Mon, 15 Aug 2022 18:29:47 -0700 Subject: [PATCH] Add http business status code for asynchronous processing. --- src/basic/result.rs | 1 + src/web/web_resp.rs | 12 ++++++++++-- tests/test_web_server.rs | 20 ++++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/basic/result.rs b/src/basic/result.rs index 26f34c38..ad0b8426 100644 --- a/src/basic/result.rs +++ b/src/basic/result.rs @@ -5,3 +5,4 @@ use core::result::Result; pub type TardisResult = Result; pub const TARDIS_RESULT_SUCCESS_CODE: &str = "200"; +pub const TARDIS_RESULT_ACCEPTED_CODE: &str = "202"; diff --git a/src/web/web_resp.rs b/src/web/web_resp.rs index f3fe6a10..3ff5ff77 100644 --- a/src/web/web_resp.rs +++ b/src/web/web_resp.rs @@ -1,5 +1,5 @@ use crate::basic::error::TardisError; -use crate::basic::result::TARDIS_RESULT_SUCCESS_CODE; +use crate::basic::result::{TARDIS_RESULT_ACCEPTED_CODE, TARDIS_RESULT_SUCCESS_CODE}; use crate::serde::{Deserialize, Serialize}; use crate::TardisFuns; use poem::http::StatusCode; @@ -82,7 +82,7 @@ pub fn mapping_http_code_to_error(http_code: StatusCode, msg: &str) -> Option None, + code if code.as_u16() < 400 => None, _ => Some(TardisError::custom( http_code.as_str(), &format!("[Tardis.WebServer] Process error: {}", msg), @@ -120,6 +120,14 @@ where })) } + pub fn accepted(data: T) -> TardisApiResult { + TardisApiResult::Ok(Json(TardisResp { + code: TARDIS_RESULT_ACCEPTED_CODE.to_string(), + msg: "".to_string(), + data: Some(data), + })) + } + pub fn err(error: TardisError) -> TardisApiResult { TardisApiResult::Err(error.into()) } diff --git a/tests/test_web_server.rs b/tests/test_web_server.rs index c892fa9e..3220eedd 100644 --- a/tests/test_web_server.rs +++ b/tests/test_web_server.rs @@ -13,7 +13,7 @@ use tardis::basic::config::{CacheConfig, DBConfig, FrameworkConfig, MQConfig, Ma use tardis::basic::dto::TardisContext; use tardis::basic::error::TardisError; use tardis::basic::field::TrimString; -use tardis::basic::result::{TardisResult, TARDIS_RESULT_SUCCESS_CODE}; +use tardis::basic::result::{TardisResult, TARDIS_RESULT_ACCEPTED_CODE, TARDIS_RESULT_SUCCESS_CODE}; use tardis::serde::{Deserialize, Serialize}; use tardis::test::test_container::TardisTestContainer; use tardis::web::context_extractor::{TardisContextExtractor, TOKEN_FLAG}; @@ -162,9 +162,16 @@ async fn start_serv(web_url: &str, redis_url: &str) -> TardisResult<()> { async fn test_basic(url: &str) -> TardisResult<()> { // Normal - let response = TardisFuns::web_client().get::>(format!("{}/todo/todos/1", url).as_str(), None).await?.body.unwrap(); - assert_eq!(response.code, TARDIS_RESULT_SUCCESS_CODE); - assert_eq!(response.data.unwrap().code.to_string(), "code1"); + let response = TardisFuns::web_client().get::>(format!("{}/todo/todos/1", url).as_str(), None).await?; + assert_eq!(response.code, 200); + assert_eq!(response.body.as_ref().unwrap().code, TARDIS_RESULT_SUCCESS_CODE); + assert_eq!(response.body.as_ref().unwrap().data.as_ref().unwrap().code.to_string(), "code1"); + + // Accepted + let response = TardisFuns::web_client().get::>(format!("{}/todo/todos/1/async", url).as_str(), None).await?; + assert_eq!(response.code, 200); + assert_eq!(response.body.as_ref().unwrap().code, TARDIS_RESULT_ACCEPTED_CODE); + assert_eq!(response.body.as_ref().unwrap().data.as_ref().unwrap(), "/todos/1/status"); // Business Error let response = TardisFuns::web_client().get::>(format!("{}/todo/todos/1/err", url).as_str(), None).await?.body.unwrap(); @@ -707,6 +714,11 @@ impl TodosApi { }) } + #[oai(path = "/todos/:id/async", method = "get")] + async fn get_async(&self, id: Path) -> TardisApiResult { + TardisResp::accepted(format!("/todos/{}/status", id.0)) + } + #[oai(path = "/todos/:id/err", method = "get")] async fn get_by_error(&self, id: Path) -> TardisApiResult { TardisResp::err(TardisError::conflict("异常", ""))