api: transactions

This commit is contained in:
Robin Appelman 2023-11-19 15:05:45 +01:00
commit 6a88ce7a7a
4 changed files with 32 additions and 9 deletions

4
api-server/Cargo.lock generated
View file

@ -1742,9 +1742,9 @@ dependencies = [
[[package]] [[package]]
name = "ugc-scraper" name = "ugc-scraper"
version = "0.1.1" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14ae17e7f4fb13d132b973b6e449dbc782f2e118c3c86bbb93210aea19bd0972" checksum = "3be88e517d2c0e368dc65c377c9fd60583fb430ab4fe5bc7b348120e0e9df42f"
dependencies = [ dependencies = [
"reqwest", "reqwest",
"scraper", "scraper",

View file

@ -6,8 +6,8 @@ edition = "2021"
[dependencies] [dependencies]
tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread", "rt"] } tokio = { version = "1.34.0", features = ["macros", "rt-multi-thread", "rt"] }
main_error = "0.1.2" main_error = "0.1.2"
#ugc-scraper = { path = "../", version = "0.1" } #ugc-scraper = { path = "../", version = "0.2.0" }
ugc-scraper = "0.1.1" ugc-scraper = "0.2.0"
axum = "0.6.20" axum = "0.6.20"
steamid-ng = "1.0.0" steamid-ng = "1.0.0"
thiserror = "1.0.50" thiserror = "1.0.50"

View file

@ -16,6 +16,10 @@ Get team history for player
Get teams by format (`9v9`, `6v6`, `4v4`, `2v2`) Get teams by format (`9v9`, `6v6`, `4v4`, `2v2`)
### `/transactions/:format`
Get roster transactions by format (`9v9`, `6v6`, `4v4`, `2v2`)
### `/team/:id` ### `/team/:id`
Get team info by id Get team info by id

View file

@ -5,11 +5,13 @@ use axum::{routing::get, Json, Router};
use main_error::MainResult; use main_error::MainResult;
use std::env::var; use std::env::var;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use steamid_ng::{SteamID, SteamIDError}; use steamid_ng::{SteamID, SteamIDError};
use thiserror::Error; use thiserror::Error;
use tracing::{debug, error, instrument}; use tracing::{debug, error, instrument};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use ugc_scraper::data::GameMode;
use ugc_scraper::{ScrapeError, UgcClient}; use ugc_scraper::{ScrapeError, UgcClient};
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -61,6 +63,7 @@ async fn main() -> MainResult {
.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("/teams/:format", get(teams))
.route("/transactions/:format", get(transactions))
.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))
@ -107,11 +110,8 @@ async fn teams(
Path(format): Path<String>, Path(format): Path<String>,
State(state): State<AppState>, State(state): State<AppState>,
) -> Result<impl IntoResponse, ApiError> { ) -> Result<impl IntoResponse, ApiError> {
let response = match format.as_str() { let mode = match GameMode::from_str(&format) {
"9v9" => state.client.teams_9v9().await?, Ok(mode) => mode,
"6v6" => state.client.teams_6v6().await?,
"4v4" => state.client.teams_4v4().await?,
"2v2" => state.client.teams_2v2().await?,
_ => { _ => {
return Err(ApiError::Mallformared(format!( return Err(ApiError::Mallformared(format!(
"invalid game mode {}", "invalid game mode {}",
@ -119,6 +119,25 @@ async fn teams(
))) )))
} }
}; };
let response = state.client.teams(mode).await?;
Ok(Json(response))
}
#[instrument(skip(state))]
async fn transactions(
Path(format): Path<String>,
State(state): State<AppState>,
) -> Result<impl IntoResponse, ApiError> {
let mode = match GameMode::from_str(&format) {
Ok(mode) => mode,
_ => {
return Err(ApiError::Mallformared(format!(
"invalid game mode {}",
format
)))
}
};
let response = state.client.transactions(mode).await?;
Ok(Json(response)) Ok(Json(response))
} }