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]]
name = "ugc-scraper"
version = "0.1.1"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14ae17e7f4fb13d132b973b6e449dbc782f2e118c3c86bbb93210aea19bd0972"
checksum = "3be88e517d2c0e368dc65c377c9fd60583fb430ab4fe5bc7b348120e0e9df42f"
dependencies = [
"reqwest",
"scraper",

View file

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

View file

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

View file

@ -5,11 +5,13 @@ use axum::{routing::get, Json, Router};
use main_error::MainResult;
use std::env::var;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
use steamid_ng::{SteamID, SteamIDError};
use thiserror::Error;
use tracing::{debug, error, instrument};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use ugc_scraper::data::GameMode;
use ugc_scraper::{ScrapeError, UgcClient};
#[derive(Clone, Default)]
@ -61,6 +63,7 @@ async fn main() -> MainResult {
.route("/player/:id", get(player))
.route("/player/:id/history", get(player_history))
.route("/teams/:format", get(teams))
.route("/transactions/:format", get(transactions))
.route("/team/:id", get(team))
.route("/team/:id/roster", get(team_roster))
.route("/team/:id/matches", get(team_matches))
@ -107,11 +110,8 @@ 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?,
let mode = match GameMode::from_str(&format) {
Ok(mode) => mode,
_ => {
return Err(ApiError::Mallformared(format!(
"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))
}