Create internals dir and optimize usage of Reqwest
All checks were successful
Build and push container image / build (push) Successful in 11m10s

This commit is contained in:
toast 2024-03-16 09:41:33 +11:00
parent b5847fcd88
commit 38bfcf62a9
9 changed files with 46 additions and 23 deletions

2
Cargo.lock generated
View File

@ -837,7 +837,7 @@ dependencies = [
[[package]] [[package]]
name = "kon" name = "kon"
version = "0.1.20" version = "0.2.0"
dependencies = [ dependencies = [
"cargo_toml", "cargo_toml",
"gamedig", "gamedig",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "kon" name = "kon"
version = "0.1.20" version = "0.2.0"
rust-version = "1.75" rust-version = "1.75"
edition = "2021" edition = "2021"

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
Error, Error,
utils::EMBED_COLOR, internals::utils::EMBED_COLOR,
models::gameservers::Gameservers models::gameservers::Gameservers
}; };

View File

@ -2,18 +2,14 @@ use crate::{
Error, Error,
models::gameservers::Gameservers, models::gameservers::Gameservers,
commands::gameserver::ac_server_name, commands::gameserver::ac_server_name,
utils::BOT_VERSION, internals::utils::EMBED_COLOR,
utils::EMBED_COLOR internals::http::HttpClient
}; };
use std::{ use std::{
collections::HashMap, collections::HashMap,
env::var env::var
}; };
use reqwest::{
Client,
header::USER_AGENT
};
use tokio::join; use tokio::join;
use poise::CreateReply; use poise::CreateReply;
use serenity::builder::CreateEmbed; use serenity::builder::CreateEmbed;
@ -45,11 +41,9 @@ struct MinecraftPlayers {
} }
async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> { async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
let client = Client::new(); let client = HttpClient::new();
let req = client.get(url) let req = client.get(url).await?;
.header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION))
.send()
.await?;
let response = req.json::<HashMap<String, Value>>().await?; let response = req.json::<HashMap<String, Value>>().await?;
let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone(); let servers = response["data"].as_array().unwrap()[0]["servers_statuses"]["data"].as_array().unwrap().clone();
@ -57,11 +51,8 @@ async fn pms_serverstatus(url: &str) -> Result<Vec<Value>, Error> {
} }
async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error> { async fn gs_query_minecraft(server_ip: &str) -> Result<MinecraftQueryData, Error> {
let client = Client::new(); let client = HttpClient::new();
let req = client.get(format!("https://api.mcsrvstat.us/2/{}", server_ip)) let req = client.get(&format!("https://api.mcsrvstat.us/2/{}", server_ip)).await?;
.header(USER_AGENT, format!("Kon/{}/Rust", &**BOT_VERSION))
.send()
.await?;
if req.status().is_success() { if req.status().is_success() {
let data: MinecraftQueryData = req.json().await?; let data: MinecraftQueryData = req.json().await?;

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
Error, Error,
utils::{ internals::utils::{
format_duration, format_duration,
concat_message, concat_message,
BOT_VERSION BOT_VERSION
@ -35,7 +35,7 @@ pub async fn uptime(ctx: poise::Context<'_, (), Error>) -> Result<(), Error> {
} }
let stat_msg = vec![ let stat_msg = vec![
format!("**{} {}**", _bot.name, &**BOT_VERSION), format!("**{} {}**", _bot.name, BOT_VERSION.as_str()),
format!(">>> System: `{}`", format_duration(sys_uptime)), format!(">>> System: `{}`", format_duration(sys_uptime)),
format!("Process: `{}`", format_duration(proc_uptime)) format!("Process: `{}`", format_duration(proc_uptime))
]; ];

30
src/internals/http.rs Normal file
View File

@ -0,0 +1,30 @@
use std::sync::Arc;
use once_cell::sync::Lazy;
use reqwest::{
Client,
header::USER_AGENT
};
static CUSTOM_USER_AGENT: Lazy<String> = Lazy::new(||
format!("Kon/{}/Rust", super::utils::BOT_VERSION.as_str())
);
pub struct HttpClient {
client: Arc<Client>
}
impl HttpClient {
pub fn new() -> Self {
Self {
client: Arc::new(Client::new())
}
}
pub async fn get(&self, url: &str) -> Result<reqwest::Response, reqwest::Error> {
let req = self.client.get(url)
.header(USER_AGENT, CUSTOM_USER_AGENT.as_str())
.send()
.await?;
Ok(req)
}
}

2
src/internals/mod.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod utils;
pub mod http;

View File

@ -1,7 +1,7 @@
mod commands; mod commands;
mod controllers; mod controllers;
mod models; mod models;
mod utils; mod internals;
use poise::serenity_prelude::{self as serenity}; use poise::serenity_prelude::{self as serenity};
use std::{ use std::{
@ -33,7 +33,7 @@ async fn on_ready(
let message = CreateMessage::new(); let message = CreateMessage::new();
let ready_embed = CreateEmbed::new() let ready_embed = CreateEmbed::new()
.color(utils::EMBED_COLOR) .color(internals::utils::EMBED_COLOR)
.thumbnail(ready.user.avatar_url().unwrap_or_default()) .thumbnail(ready.user.avatar_url().unwrap_or_default())
.author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone()); .author(CreateEmbedAuthor::new(format!("{} is ready!", ready.user.name)).clone());