Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Hello service along with test cases #507

Open
wants to merge 3 commits into
base: release/eleanor-rigby
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions bundler/src/handlers/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
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;

pub async fn hello_world(
service: Data<HelloWorldService>,
) -> Result<Json<BaseResponse<HelloWorld>>, ApiError> {
let hello = service.hello_world()?;
respond_json(hello)
use crate::services::HelloWorldService;

pub async fn hello_world() -> Result<HttpResponse, Error> {
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);
}
}
2 changes: 1 addition & 1 deletion bundler/src/models/hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct HelloWorld {
pub name: String,
}
1 change: 1 addition & 0 deletions bundler/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
19 changes: 2 additions & 17 deletions bundler/src/provider/helpers.rs
Original file line number Diff line number Diff line change
@@ -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<T>(data: T) -> Result<Json<BaseResponse<T>>, 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"))
Expand Down
4 changes: 0 additions & 4 deletions bundler/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Postgres>,
}
Expand All @@ -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,
}
Expand All @@ -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()))
})
Expand Down
26 changes: 22 additions & 4 deletions bundler/src/services/hello_world_service.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
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<HelloWorld, ApiError> {
pub fn hello_world() -> Result<HelloWorld, Error> {
Ok(HelloWorld {
name: "Hello world!".to_string(),
})
}
}

#[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()
}
)
}
}
1 change: 1 addition & 0 deletions bundler/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down