Refactor pms_serverstatus to add User-Agent header
All checks were successful
Build and push container image / build (push) Successful in 7m17s

This commit is contained in:
toast 2023-12-12 12:35:57 +11:00
parent 3996bad37d
commit f835d92519
3 changed files with 76 additions and 4 deletions

65
Cargo.lock generated
View File

@ -186,6 +186,16 @@ dependencies = [
"serde_json",
]
[[package]]
name = "cargo_toml"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d1ece59890e746567b467253aea0adbe8a21784d0b025d8a306f66c391c2957"
dependencies = [
"serde",
"toml",
]
[[package]]
name = "cc"
version = "1.0.83"
@ -761,8 +771,9 @@ dependencies = [
[[package]]
name = "kon"
version = "0.1.0"
version = "0.1.2"
dependencies = [
"cargo_toml",
"lazy_static",
"poise",
"reqwest",
@ -1426,6 +1437,15 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_spanned"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12022b835073e5b11e90a14f86838ceb1c8fb0325b72416845c487ac0fa95e80"
dependencies = [
"serde",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@ -1826,6 +1846,40 @@ dependencies = [
"tracing",
]
[[package]]
name = "toml"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"winnow",
]
[[package]]
name = "tower-service"
version = "0.3.2"
@ -2353,6 +2407,15 @@ version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
[[package]]
name = "winnow"
version = "0.5.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c830786f7720c2fd27a1a0e27a709dbd3c4d009b56d098fc742d4f4eab91fe2"
dependencies = [
"memchr",
]
[[package]]
name = "winreg"
version = "0.50.0"

View File

@ -1,12 +1,13 @@
[package]
name = "kon"
version = "0.1.0"
version = "0.1.2"
rust-version = "1.74"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cargo_toml = "0.17.1"
lazy_static = "1.4.0"
poise = "0.5.7"
reqwest = "0.11.22"

View File

@ -1,6 +1,7 @@
use crate::{Error, COLOR};
use reqwest::get;
use cargo_toml::Manifest;
use reqwest::{Client, header::USER_AGENT};
use std::collections::HashMap;
use serde_json::Value;
use tokio::join;
@ -48,7 +49,14 @@ pub async fn status(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
}
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
let response = get(url).await?.json::<HashMap<String, Value>>().await?;
let bot_version = Manifest::from_path("Cargo.toml").unwrap().package.unwrap().version.unwrap();
let client = Client::new();
let req = client.get(url)
.header(USER_AGENT, format!("Kon/{}/Rust", bot_version))
.send()
.await?;
let response = req.json::<HashMap<String, Value>>().await?;
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
Ok(servers)