api: team list

This commit is contained in:
Robin Appelman 2023-11-18 22:01:16 +01:00
commit 880ac28b57

View file

@ -23,6 +23,8 @@ enum ApiError {
SteamId(#[from] SteamIDError), SteamId(#[from] SteamIDError),
#[error(transparent)] #[error(transparent)]
Scrape(#[from] ScrapeError), Scrape(#[from] ScrapeError),
#[error("mallformed request")]
Mallformared(String),
} }
impl IntoResponse for ApiError { impl IntoResponse for ApiError {
@ -32,6 +34,9 @@ impl IntoResponse for ApiError {
Self::SteamId(err) => { Self::SteamId(err) => {
(StatusCode::UNPROCESSABLE_ENTITY, format!("{:#}", err)).into_response() (StatusCode::UNPROCESSABLE_ENTITY, format!("{:#}", err)).into_response()
} }
Self::Mallformared(err) => {
(StatusCode::UNPROCESSABLE_ENTITY, format!("{:#}", err)).into_response()
}
Self::Scrape(ScrapeError::NotFound) => (StatusCode::NOT_FOUND, "").into_response(), Self::Scrape(ScrapeError::NotFound) => (StatusCode::NOT_FOUND, "").into_response(),
Self::Scrape(err) => { Self::Scrape(err) => {
(StatusCode::INTERNAL_SERVER_ERROR, format!("{:#}", err)).into_response() (StatusCode::INTERNAL_SERVER_ERROR, format!("{:#}", err)).into_response()
@ -55,6 +60,7 @@ async fn main() -> MainResult {
.route("/", get(handler)) .route("/", get(handler))
.route("/player/:id", get(player)) .route("/player/:id", get(player))
.route("/player/:id/history", get(player_history)) .route("/player/:id/history", get(player_history))
.route("/teams/:format", get(teams))
.route("/team/:id", get(team)) .route("/team/:id", get(team))
.route("/team/:id/roster", get(team_roster)) .route("/team/:id/roster", get(team_roster))
.route("/team/:id/matches", get(team_matches)) .route("/team/:id/matches", get(team_matches))
@ -95,6 +101,26 @@ async fn player_history(
Ok(Json(response)) Ok(Json(response))
} }
#[instrument(skip(state))]
async fn teams(
Path(format): Path<String>,
State(state): State<AppState>,
) -> Result<impl IntoResponse, ApiError> {
let response = match format.as_str() {
"9v9" => state.client.teams_9v9().await?,
"6v6" => state.client.teams_6v6().await?,
"4v4" => state.client.teams_4v4().await?,
"2v2" => state.client.teams_2v2().await?,
_ => {
return Err(ApiError::Mallformared(format!(
"invalid game mode {}",
format
)))
}
};
Ok(Json(response))
}
#[instrument(skip(state))] #[instrument(skip(state))]
async fn team( async fn team(
Path(id): Path<u32>, Path(id): Path<u32>,