From 96d8868bf7ace6c92dffeb5d581dd296176a1ddb Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 21 Apr 2024 15:23:17 +0300 Subject: [PATCH] Initial commit --- .editorconfig | 17 ++++++++++ .gitignore | 15 +++++++++ Cargo.toml | 17 ++++++++++ LICENSE | 9 ++++++ README.md | 3 ++ examples/minecraft-async/Cargo.toml | 10 ++++++ examples/minecraft-async/src/main.rs | 12 ++++++++ examples/minecraft-sync/Cargo.toml | 9 ++++++ examples/minecraft-sync/src/main.rs | 12 ++++++++ rust-toolchain.toml | 2 ++ rustfmt.toml | 8 +++++ src/lib.rs | 46 ++++++++++++++++++++++++++++ src/minecraft.rs | 31 +++++++++++++++++++ src/sync.rs | 35 +++++++++++++++++++++ 14 files changed, 226 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 examples/minecraft-async/Cargo.toml create mode 100644 examples/minecraft-async/src/main.rs create mode 100644 examples/minecraft-sync/Cargo.toml create mode 100644 examples/minecraft-sync/src/main.rs create mode 100644 rust-toolchain.toml create mode 100644 rustfmt.toml create mode 100644 src/lib.rs create mode 100644 src/minecraft.rs create mode 100644 src/sync.rs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5df031b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +# Editor configuration, see https://editorconfig.org +root = true + +[*] +charset = utf-8 +indent_style = tab +indent_size = 4 +insert_final_newline = false +trim_trailing_whitespace = true + +[*.md] +max_line_length = off +trim_trailing_whitespace = false + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ae5aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# ---> Rust +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..6bce41e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "playerdb-rs" +version = "0.1.0" +edition = "2021" +authors = ["Wynd"] + +[workspace] +members = ["examples/*"] + +[dependencies] +serde = { version = "1.0.198", features = ["derive"] } +serde_json = { version = "1.0.116" } +reqwest = { version = "0.12.4", features = ["json", "blocking"] } +anyhow = { version = "1.0.82" } + +[features] +sync = [] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9f5381e --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 wynd + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb982fb --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# playerdb-rs + +[playerdb](https://playerdb.co/) client with support for async/sync. \ No newline at end of file diff --git a/examples/minecraft-async/Cargo.toml b/examples/minecraft-async/Cargo.toml new file mode 100644 index 0000000..ccba2e4 --- /dev/null +++ b/examples/minecraft-async/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "minecraft-async" +version = "1.0.0" +edition = "2021" +authors = ["Wynd"] + +[dependencies] +playerdb-rs = { path = "../../"} +tokio = { version = "1.37", features = ["full"] } +anyhow = { version = "1" } diff --git a/examples/minecraft-async/src/main.rs b/examples/minecraft-async/src/main.rs new file mode 100644 index 0000000..d77fe19 --- /dev/null +++ b/examples/minecraft-async/src/main.rs @@ -0,0 +1,12 @@ +use playerdb_rs::PlayerDBApi; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let api = PlayerDBApi::default(); + + let res = api.get_minecraft_player("wyndftw").await?; + + print!("{:#?}", res); + + Ok(()) +} diff --git a/examples/minecraft-sync/Cargo.toml b/examples/minecraft-sync/Cargo.toml new file mode 100644 index 0000000..f32c397 --- /dev/null +++ b/examples/minecraft-sync/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "minecraft-sync" +version = "1.0.0" +edition = "2021" +authors = ["Wynd"] + +[dependencies] +playerdb-rs = { path = "../../", features = ["sync"] } + diff --git a/examples/minecraft-sync/src/main.rs b/examples/minecraft-sync/src/main.rs new file mode 100644 index 0000000..36ee9ed --- /dev/null +++ b/examples/minecraft-sync/src/main.rs @@ -0,0 +1,12 @@ +use playerdb_rs::sync::PlayerDBApi; + +fn main() { + let api = PlayerDBApi::default(); + + let res = api.get_minecraft_player("wyndftw"); + + match res { + Ok(x) => println!("{:#?}", x), + Err(e) => eprintln!("{e}"), + } +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..f27b9a0 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel= "nightly" \ No newline at end of file diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..3116f8a --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,8 @@ +unstable_features = true +reorder_imports = true +hard_tabs = true +control_brace_style = "ClosingNextLine" +imports_granularity = "Crate" +group_imports = "StdExternalCrate" +edition = "2021" +newline_style = "Unix" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..17ebc3b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,46 @@ +use std::sync::Arc; + +use anyhow::Result; + +pub mod minecraft; +#[cfg(feature = "sync")] +pub mod sync; + +pub(crate) static BASE_URI: &str = "https://playerdb.co/api/player"; +pub(crate) static APP_USER_AGENT: &str = + concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); + +pub struct PlayerDBApi { + agent: Arc, +} + +impl Default for PlayerDBApi { + fn default() -> Self { + Self { + agent: Arc::new( + reqwest::Client::builder() + .user_agent(APP_USER_AGENT) + .build() + .expect("creating reqwest client"), + ), + } + } +} + +impl PlayerDBApi { + pub async fn get_minecraft_player( + &self, + player_id: &str, + ) -> Result { + let url = format!("{}{}/{}", BASE_URI, minecraft::PLATFORM, player_id); + let res = self + .agent + .get(url) + .send() + .await? + .json::() + .await?; + + Ok(res) + } +} diff --git a/src/minecraft.rs b/src/minecraft.rs new file mode 100644 index 0000000..907a86c --- /dev/null +++ b/src/minecraft.rs @@ -0,0 +1,31 @@ +#![allow(dead_code)] +use std::collections::HashMap; + +use serde::Deserialize; + +pub(crate) static PLATFORM: &str = "/minecraft"; + +#[derive(Deserialize, Debug)] +pub struct MinecraftResponse { + pub code: String, + pub message: String, + pub success: bool, + pub data: MinecraftData, +} + +#[derive(Deserialize, Debug)] +pub struct MinecraftData { + pub player: MinecraftPlayerData, +} + +#[derive(Deserialize, Debug)] +pub struct MinecraftPlayerData { + pub username: String, + pub id: String, + pub raw_id: String, + pub avatar: String, + pub skin_texture: String, + + #[serde(flatten)] + extra: HashMap, +} diff --git a/src/sync.rs b/src/sync.rs new file mode 100644 index 0000000..fe7c41a --- /dev/null +++ b/src/sync.rs @@ -0,0 +1,35 @@ +use std::sync::Arc; + +use anyhow::Result; + +use crate::{minecraft, APP_USER_AGENT, BASE_URI}; + +pub struct PlayerDBApi { + agent: Arc, +} + +impl Default for PlayerDBApi { + fn default() -> Self { + Self { + agent: Arc::new( + reqwest::blocking::Client::builder() + .user_agent(APP_USER_AGENT) + .build() + .expect("creating reqwest client"), + ), + } + } +} + +impl PlayerDBApi { + pub fn get_minecraft_player(&self, player_id: &str) -> Result { + let url = format!("{}{}/{}", BASE_URI, minecraft::PLATFORM, player_id); + let res = self + .agent + .get(url) + .send()? + .json::()?; + + Ok(res) + } +}