-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use axum::{extract::Path, http::StatusCode, response::IntoResponse, routing::get}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize, Debug, Clone)] | ||
struct PokeWeight { | ||
weight: u32, | ||
} | ||
|
||
impl PokeWeight { | ||
fn extract_weight(&self) -> f64 { | ||
self.weight as f64 / 10.0 | ||
} | ||
} | ||
|
||
async fn poke_weight(Path(pokedex): Path<u32>) -> impl IntoResponse { | ||
let poke_weight = reqwest::get(format!("https://pokeapi.co/api/v/pokemon/{pokedex}")) | ||
.await | ||
.unwrap() | ||
.json::<PokeWeight>() | ||
.await | ||
.unwrap(); | ||
|
||
format!("{}", poke_weight.extract_weight()) | ||
} | ||
|
||
async fn poke_drop(Path(pokedex): Path<u32>) -> impl IntoResponse { | ||
let poke_weight = reqwest::get(format!("https://pokeapi.co/api/v2/pokemon/{pokedex}")) | ||
.await | ||
.unwrap() | ||
.json::<PokeWeight>() | ||
.await | ||
.unwrap(); | ||
|
||
println!( | ||
"{}", | ||
poke_weight.extract_weight() * (9.825f64 * 10.0 * 2.0).sqrt() | ||
); | ||
format!( | ||
"{}", | ||
poke_weight.extract_weight() * (9.825f64 * 10.0 * 2.0).sqrt() | ||
) | ||
} | ||
|
||
pub fn router() -> axum::Router { | ||
axum::Router::new() | ||
.route("/8", get(|| async { StatusCode::OK })) | ||
.route("/8/weight/:pokedex", get(poke_weight)) | ||
.route("/8/drop/:pokedex", get(poke_drop)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use axum::http::StatusCode; | ||
use axum_test_helper::TestClient; | ||
|
||
#[tokio::test] | ||
async fn day8_health() { | ||
let app = router(); | ||
|
||
let client = TestClient::new(app); | ||
let res = client.get("/8").send().await; | ||
assert_eq!(res.status(), StatusCode::OK); | ||
} | ||
|
||
#[tokio::test] | ||
async fn day8_poke_weight() { | ||
let app = router(); | ||
|
||
let client = TestClient::new(app); | ||
let res = client.get("/8/weight/25").send().await; | ||
assert_eq!(res.status(), StatusCode::OK); | ||
assert_eq!(res.text().await, "6"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn day8_poke_drop() { | ||
let app = router(); | ||
|
||
let client = TestClient::new(app); | ||
let res = client.get("/8/drop/25").send().await; | ||
assert_eq!(res.status(), StatusCode::OK); | ||
assert!(res.text().await.parse::<f64>().unwrap() - 84.10707461325713 <= 0.001); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters