Initial commit

master
Wynd 2024-04-21 15:23:17 +03:00
commit 96d8868bf7
14 changed files with 226 additions and 0 deletions

17
.editorconfig 100644
View File

@ -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

15
.gitignore vendored 100644
View File

@ -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

17
Cargo.toml 100644
View File

@ -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 = []

9
LICENSE 100644
View File

@ -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.

3
README.md 100644
View File

@ -0,0 +1,3 @@
# playerdb-rs
[playerdb](https://playerdb.co/) client with support for async/sync.

View File

@ -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" }

View File

@ -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(())
}

View File

@ -0,0 +1,9 @@
[package]
name = "minecraft-sync"
version = "1.0.0"
edition = "2021"
authors = ["Wynd"]
[dependencies]
playerdb-rs = { path = "../../", features = ["sync"] }

View File

@ -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}"),
}
}

View File

@ -0,0 +1,2 @@
[toolchain]
channel= "nightly"

8
rustfmt.toml 100644
View File

@ -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"

46
src/lib.rs 100644
View File

@ -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<reqwest::Client>,
}
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<minecraft::MinecraftResponse> {
let url = format!("{}{}/{}", BASE_URI, minecraft::PLATFORM, player_id);
let res = self
.agent
.get(url)
.send()
.await?
.json::<minecraft::MinecraftResponse>()
.await?;
Ok(res)
}
}

31
src/minecraft.rs 100644
View File

@ -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<String, serde_json::Value>,
}

35
src/sync.rs 100644
View File

@ -0,0 +1,35 @@
use std::sync::Arc;
use anyhow::Result;
use crate::{minecraft, APP_USER_AGENT, BASE_URI};
pub struct PlayerDBApi {
agent: Arc<reqwest::blocking::Client>,
}
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<minecraft::MinecraftResponse> {
let url = format!("{}{}/{}", BASE_URI, minecraft::PLATFORM, player_id);
let res = self
.agent
.get(url)
.send()?
.json::<minecraft::MinecraftResponse>()?;
Ok(res)
}
}