From 17f5698790917584b61793255f95a779fe613698 Mon Sep 17 00:00:00 2001 From: vishnukvnvv Date: Mon, 9 Oct 2023 16:01:37 +0530 Subject: [PATCH 1/3] update hello world service --- bundler/src/handlers/hello_world.rs | 15 +++++---------- bundler/src/models/mod.rs | 1 + bundler/src/server.rs | 4 ---- bundler/src/services/hello_world_service.rs | 9 +++++---- bundler/src/services/mod.rs | 1 + 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/bundler/src/handlers/hello_world.rs b/bundler/src/handlers/hello_world.rs index ab0955f7..8a22a15c 100644 --- a/bundler/src/handlers/hello_world.rs +++ b/bundler/src/handlers/hello_world.rs @@ -1,14 +1,9 @@ -use actix_web::web::{Data, Json}; +use actix_web::{Error, HttpResponse}; -use crate::errors::errors::ApiError; -use crate::models::hello_world::HelloWorld; use crate::models::response::BaseResponse; -use crate::provider::helpers::respond_json; -use crate::services::hello_world_service::HelloWorldService; +use crate::services::HelloWorldService; -pub async fn hello_world( - service: Data, -) -> Result>, ApiError> { - let hello = service.hello_world()?; - respond_json(hello) +pub async fn hello_world() -> Result { + let hello = HelloWorldService::hello_world()?; + Ok(HttpResponse::Ok().json(BaseResponse::init(hello))) } diff --git a/bundler/src/models/mod.rs b/bundler/src/models/mod.rs index 9b8ed7eb..96037f2f 100644 --- a/bundler/src/models/mod.rs +++ b/bundler/src/models/mod.rs @@ -11,5 +11,6 @@ pub mod transfer; pub mod wallet; pub use currency::Currency; +pub use hello_world::HelloWorld; pub use metadata::Metadata; pub use transaction_type::TransactionType; diff --git a/bundler/src/server.rs b/bundler/src/server.rs index fa7766ee..88ad75aa 100644 --- a/bundler/src/server.rs +++ b/bundler/src/server.rs @@ -11,12 +11,10 @@ use crate::db::connection::DatabaseConnection; use crate::models::config::server::Server; use crate::provider::Web3Client; use crate::routes::routes; -use crate::services::hello_world_service::HelloWorldService; use crate::{CONFIG, PROVIDER}; #[derive(Clone)] pub struct ToadService { - pub hello_world_service: HelloWorldService, pub web3_client: Web3Client, pub db_pool: Pool, } @@ -28,7 +26,6 @@ pub async fn init_services() -> ToadService { let client = Arc::new(PROVIDER.clone()); ToadService { - hello_world_service: HelloWorldService {}, web3_client: Web3Client::new(client.clone()), db_pool: DatabaseConnection::init().await, } @@ -47,7 +44,6 @@ pub async fn run(service: ToadService, server: Server) -> std::io::Result<()> { App::new() .wrap(Logger::default()) .configure(routes) - .app_data(Data::new(service.hello_world_service.clone())) .app_data(Data::new(service.web3_client.clone())) .app_data(Data::new(service.db_pool.clone())) }) diff --git a/bundler/src/services/hello_world_service.rs b/bundler/src/services/hello_world_service.rs index 56a9b71c..fcdd1613 100644 --- a/bundler/src/services/hello_world_service.rs +++ b/bundler/src/services/hello_world_service.rs @@ -1,11 +1,12 @@ -use crate::errors::errors::ApiError; -use crate::models::hello_world::HelloWorld; +use actix_web::Error; + +use crate::models::HelloWorld; #[derive(Clone)] -pub struct HelloWorldService {} +pub struct HelloWorldService; impl HelloWorldService { - pub fn hello_world(&self) -> Result { + pub fn hello_world() -> Result { Ok(HelloWorld { name: "Hello world!".to_string(), }) diff --git a/bundler/src/services/mod.rs b/bundler/src/services/mod.rs index 2371c804..c8858d5a 100644 --- a/bundler/src/services/mod.rs +++ b/bundler/src/services/mod.rs @@ -9,6 +9,7 @@ pub mod wallet_service; pub use admin_service::AdminService; pub use balance_service::BalanceService; +pub use hello_world_service::HelloWorldService; pub use mint_service::MintService; pub use token_metadata_service::TokenMetadataService; pub use transfer_service::TransferService; From 3b899e57d53fb27b18fca75d61fb3b541f3d9e3c Mon Sep 17 00:00:00 2001 From: vishnukvnvv Date: Mon, 9 Oct 2023 16:09:08 +0530 Subject: [PATCH 2/3] remove unused functions --- bundler/src/provider/helpers.rs | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/bundler/src/provider/helpers.rs b/bundler/src/provider/helpers.rs index 9484ed84..fb234b4c 100644 --- a/bundler/src/provider/helpers.rs +++ b/bundler/src/provider/helpers.rs @@ -1,29 +1,14 @@ -use std::collections::hash_map::DefaultHasher; -use std::hash::{Hash, Hasher}; - use actix_web::http::header::HeaderName; -use actix_web::web::Json; use actix_web::HttpRequest; use ethers::providers::Middleware; use ethers::types::Address; use rand::distributions::Alphanumeric; use rand::Rng; -use serde::Serialize; +use std::collections::hash_map::DefaultHasher; +use std::hash::{Hash, Hasher}; -use crate::errors::errors::ApiError; -use crate::models::response::BaseResponse; use crate::{CONFIG, PROVIDER}; -pub fn respond_json(data: T) -> Result>, ApiError> -where - T: Serialize, -{ - Ok(Json(BaseResponse { - data, - err: Default::default(), - })) -} - pub fn get_user(req: HttpRequest) -> String { req.headers() .get(HeaderName::from_static("user")) From 03d30483135571273f567a4ba0f6dff255c76276 Mon Sep 17 00:00:00 2001 From: vishnukvnvv Date: Wed, 11 Oct 2023 13:11:02 +0530 Subject: [PATCH 3/3] test cases added --- bundler/src/handlers/hello_world.rs | 19 +++++++++++++++++++ bundler/src/models/hello_world.rs | 2 +- bundler/src/services/hello_world_service.rs | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/bundler/src/handlers/hello_world.rs b/bundler/src/handlers/hello_world.rs index 8a22a15c..f3333fcb 100644 --- a/bundler/src/handlers/hello_world.rs +++ b/bundler/src/handlers/hello_world.rs @@ -7,3 +7,22 @@ pub async fn hello_world() -> Result { let hello = HelloWorldService::hello_world()?; Ok(HttpResponse::Ok().json(BaseResponse::init(hello))) } + +#[cfg(test)] +mod test { + use actix_web::{test, web, App}; + use reqwest::StatusCode; + + use crate::handlers::hello_world::hello_world; + + #[actix_web::test] + async fn test_hello_world() { + let app = test::init_service( + App::new().service(web::scope("v1").route("hello", web::get().to(hello_world))), + ) + .await; + let req = test::TestRequest::get().uri("/v1/hello").to_request(); + let result = test::call_service(&app, req).await; + assert_eq!(result.status(), StatusCode::OK); + } +} diff --git a/bundler/src/models/hello_world.rs b/bundler/src/models/hello_world.rs index 7a3c6b7b..ad90e78d 100644 --- a/bundler/src/models/hello_world.rs +++ b/bundler/src/models/hello_world.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct HelloWorld { pub name: String, } diff --git a/bundler/src/services/hello_world_service.rs b/bundler/src/services/hello_world_service.rs index fcdd1613..dfe1e7ec 100644 --- a/bundler/src/services/hello_world_service.rs +++ b/bundler/src/services/hello_world_service.rs @@ -12,3 +12,20 @@ impl HelloWorldService { }) } } + +#[cfg(test)] +mod test { + use crate::models::HelloWorld; + use crate::services::HelloWorldService; + + #[actix_web::test] + async fn test_hello_world_service() { + let data = HelloWorldService::hello_world().unwrap(); + assert_eq!( + data, + HelloWorld { + name: "Hello world!".to_string() + } + ) + } +}