feat: expose pokemon api
This commit is contained in:
208
src/app.rs
Normal file
208
src/app.rs
Normal file
@@ -0,0 +1,208 @@
|
||||
use crate::{
|
||||
config::AppConfig, error::AppError, pokemon::PokeApiClient, service::PokedexService,
|
||||
translation::TranslationClient,
|
||||
};
|
||||
use axum::{
|
||||
Json, Router,
|
||||
extract::{Path, State},
|
||||
routing::get,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
service: Arc<PokedexService>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
#[must_use]
|
||||
pub fn new(service: PokedexService) -> Self {
|
||||
Self {
|
||||
service: Arc::new(service),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_config(config: &AppConfig) -> Result<Self, AppError> {
|
||||
let pokemon = PokeApiClient::new(&config.pokeapi_base_url, config.request_timeout)?;
|
||||
let translations =
|
||||
TranslationClient::new(&config.translations_base_url, config.request_timeout)?;
|
||||
Ok(Self::new(PokedexService::new(pokemon, translations)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_app(state: AppState) -> Router {
|
||||
Router::new()
|
||||
.route("/health", get(health))
|
||||
.route("/pokemon/{name}", get(get_pokemon))
|
||||
.route("/pokemon/translated/{name}", get(get_translated_pokemon))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
async fn health() -> Json<HealthResponse> {
|
||||
Json(HealthResponse { status: "ok" })
|
||||
}
|
||||
|
||||
async fn get_pokemon(
|
||||
State(state): State<AppState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<crate::domain::PokemonInfo>, AppError> {
|
||||
state.service.get_basic(&name).await.map(Json)
|
||||
}
|
||||
|
||||
async fn get_translated_pokemon(
|
||||
State(state): State<AppState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Json<crate::domain::PokemonInfo>, AppError> {
|
||||
state.service.get_translated(&name).await.map(Json)
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct HealthResponse {
|
||||
status: &'static str,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{AppState, create_app};
|
||||
use crate::{pokemon::PokeApiClient, service::PokedexService, translation::TranslationClient};
|
||||
use axum::{
|
||||
body::{Body, to_bytes},
|
||||
http::{Request, StatusCode},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::time::Duration;
|
||||
use tower::ServiceExt;
|
||||
use wiremock::{Mock, MockServer, ResponseTemplate, matchers};
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_endpoint_reports_ok() {
|
||||
let pokeapi = MockServer::start().await;
|
||||
let translations = MockServer::start().await;
|
||||
let app = create_app(state_for(&pokeapi, &translations));
|
||||
|
||||
let response = app
|
||||
.oneshot(request("/health"))
|
||||
.await
|
||||
.expect("request should be handled");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
json_body(response).await,
|
||||
serde_json::json!({ "status": "ok" })
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pokemon_endpoint_returns_basic_pokemon_information() {
|
||||
let pokeapi = MockServer::start().await;
|
||||
let translations = MockServer::start().await;
|
||||
mount_species(&pokeapi, "mewtwo", true, "rare", "Created by science.").await;
|
||||
let app = create_app(state_for(&pokeapi, &translations));
|
||||
|
||||
let response = app
|
||||
.oneshot(request("/pokemon/mewtwo"))
|
||||
.await
|
||||
.expect("request should be handled");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
json_body(response).await,
|
||||
serde_json::json!({
|
||||
"name": "mewtwo",
|
||||
"description": "Created by science.",
|
||||
"habitat": "rare",
|
||||
"isLegendary": true
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn translated_endpoint_returns_translated_description() {
|
||||
let pokeapi = MockServer::start().await;
|
||||
let translations = MockServer::start().await;
|
||||
mount_species(&pokeapi, "pikachu", false, "forest", "Electric cheeks.").await;
|
||||
Mock::given(matchers::method("POST"))
|
||||
.and(matchers::path("/translate/shakespeare.json"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"contents": { "translated": "Electric cheeks, prithee." }
|
||||
})))
|
||||
.mount(&translations)
|
||||
.await;
|
||||
let app = create_app(state_for(&pokeapi, &translations));
|
||||
|
||||
let response = app
|
||||
.oneshot(request("/pokemon/translated/pikachu"))
|
||||
.await
|
||||
.expect("request should be handled");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
assert_eq!(
|
||||
json_body(response).await["description"],
|
||||
"Electric cheeks, prithee."
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pokemon_endpoint_maps_missing_pokemon_to_404() {
|
||||
let pokeapi = MockServer::start().await;
|
||||
let translations = MockServer::start().await;
|
||||
Mock::given(matchers::method("GET"))
|
||||
.and(matchers::path("/pokemon-species/nope"))
|
||||
.respond_with(ResponseTemplate::new(404))
|
||||
.mount(&pokeapi)
|
||||
.await;
|
||||
let app = create_app(state_for(&pokeapi, &translations));
|
||||
|
||||
let response = app
|
||||
.oneshot(request("/pokemon/nope"))
|
||||
.await
|
||||
.expect("request should be handled");
|
||||
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(json_body(response).await["error"], "pokemon not found");
|
||||
}
|
||||
|
||||
async fn mount_species(
|
||||
server: &MockServer,
|
||||
name: &str,
|
||||
is_legendary: bool,
|
||||
habitat: &str,
|
||||
description: &str,
|
||||
) {
|
||||
Mock::given(matchers::method("GET"))
|
||||
.and(matchers::path(format!("/pokemon-species/{name}")))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
|
||||
"name": name,
|
||||
"is_legendary": is_legendary,
|
||||
"habitat": { "name": habitat },
|
||||
"flavor_text_entries": [
|
||||
{ "flavor_text": description, "language": { "name": "en" } }
|
||||
]
|
||||
})))
|
||||
.mount(server)
|
||||
.await;
|
||||
}
|
||||
|
||||
fn state_for(pokeapi: &MockServer, translations: &MockServer) -> AppState {
|
||||
AppState::new(PokedexService::new(
|
||||
PokeApiClient::new(pokeapi.uri(), Duration::from_secs(1)).expect("client should build"),
|
||||
TranslationClient::new(translations.uri(), Duration::from_secs(1))
|
||||
.expect("client should build"),
|
||||
))
|
||||
}
|
||||
|
||||
fn request(path: &str) -> Request<Body> {
|
||||
Request::builder()
|
||||
.uri(path)
|
||||
.body(Body::empty())
|
||||
.expect("request should build")
|
||||
}
|
||||
|
||||
async fn json_body(response: axum::response::Response) -> Value {
|
||||
let bytes = to_bytes(response.into_body(), 1024 * 1024)
|
||||
.await
|
||||
.expect("body should be readable");
|
||||
serde_json::from_slice(&bytes).expect("body should be valid JSON")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user