Skip to content

Commit

Permalink
refactor: 🚧 restructure current code
Browse files Browse the repository at this point in the history
  • Loading branch information
Bricklou committed Jul 19, 2023
1 parent 74efc4b commit 861e5fc
Show file tree
Hide file tree
Showing 17 changed files with 477 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
CURSE_API_TOKEN = "$2a$10$FSMPrnX2TyC9kluMfAWvHuGqGxa7qKuvXpClTB/vB8LE3fVu9ic9e"
61 changes: 55 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
[package]
name = "flowupdater-json-creator"
name = "flowupdater-json-creator"
description = "Create JSON for flow-updater in a TUI"
authors = ["Zuygui", "Bricklou"]
license = "MIT"
version = "1.1.1"
edition = "2021"
readme = "README.md"
homepage = "https://github.com/zuygui/flowupdater-json-creator"
repository = "https://github.com/zuygui/flowupdater-json-creator"
keywords = ["cli", "tui", "json", "flow-updater", "flowupdater", "curseforge", "eternalapi"]
categories = ["command-line-utilities", "development-tools"]
authors = ["Zuygui", "Bricklou"]
license = "MIT"
version = "1.1.1"
edition = "2021"
readme = "README.md"
homepage = "https://github.com/zuygui/flowupdater-json-creator"
repository = "https://github.com/zuygui/flowupdater-json-creator"
keywords = [
"cli",
"tui",
"json",
"flow-updater",
"flowupdater",
"curseforge",
"eternalapi",
]
categories = ["command-line-utilities", "development-tools"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "macros"] }
serde = { version = "1", features = ["derive"] }
reqwest = { version = "0.11", features = ["json", "rustls-tls"] }
serde_json = { version = "1.0.96" }
requestty = { version = "0.5.0" }
tokio = { version = "1", default-features = false, features = [
"rt-multi-thread",
"macros",
] }
serde = { version = "1", features = ["derive"] }
reqwest = { version = "0.11", features = ["json", "rustls-tls"] }
serde_json = { version = "1.0.96" }
requestty = { version = "0.5.0" }
thiserror = { version = "1" }
time = { version = "0.3", features = ["serde", "serde-well-known"] }

[profile.release]
codegen-units = 1
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const TOKEN: &str = env!("CURSE_API_TOKEN");
File renamed without changes.
38 changes: 38 additions & 0 deletions src/curse_api/minecraft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::errors::CreatorError;

use super::{CurseApi, CURSE_API_URL};
use serde::Deserialize;
use time::OffsetDateTime;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MinecraftVersion {
pub id: u32,
pub game_version_id: u32,
pub version_string: String,
pub jar_download_url: String,
pub json_download_url: String,
pub approved: bool,
#[serde(with = "time::serde::rfc3339")]
pub date_modified: OffsetDateTime,
pub game_version_type_id: u32,
pub game_version_status: u32,
pub game_version_type_status: u32,
}

#[derive(Debug, serde::Deserialize)]
pub struct MinecraftVersionsList {
pub data: Vec<MinecraftVersion>
}

impl CurseApi {
pub async fn get_minecraft_versions(&self) -> Result<MinecraftVersionsList, CreatorError> {
let versions = self.http_client.get(format!("{}{}", CURSE_API_URL, "minecraft/version").as_str())
.send()
.await?
.json::<MinecraftVersionsList>()
.await?;

Ok(versions)
}
}
28 changes: 28 additions & 0 deletions src/curse_api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use reqwest::header::HeaderMap;

use crate::errors::CreatorError;

mod minecraft;
mod modloader;

pub static CURSE_API_URL: &str = "https://api.curseforge.com/v1/";

pub struct CurseApi {
http_client: reqwest::Client,
}

impl CurseApi {
pub fn new<S>(api_token: S) -> Result<Self, CreatorError>
where
S: AsRef<str>,
{
let mut headers = HeaderMap::new();
headers.insert("X-Api-Key", api_token.as_ref().parse().unwrap());

let http_client = reqwest::Client::builder()
.default_headers(headers)
.build()?;

Ok(Self { http_client })
}
}
5 changes: 5 additions & 0 deletions src/curse_api/modloader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use super::CurseApi;

impl CurseApi {

}
35 changes: 35 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[derive(thiserror::Error, Debug)]
pub enum CreatorError {
#[error("Invalid Mod Loader")]
InvalidModLoader,

#[error("Invalid Minecraft Version")]
InvalidMinecraftVersion,

#[error("Prompt Error: {0}")]
PromptError(requestty::ErrorKind),

#[error("IO Error: {0}")]
IoError(std::io::Error),

#[error("{0}")]
HttpError(reqwest::Error)
}

impl From<requestty::ErrorKind> for CreatorError {
fn from(err: requestty::ErrorKind) -> Self {
CreatorError::PromptError(err)
}
}

impl From<std::io::Error> for CreatorError {
fn from(err: std::io::Error) -> Self {
CreatorError::IoError(err)
}
}

impl From<reqwest::Error> for CreatorError {
fn from(err: reqwest::Error) -> Self {
CreatorError::HttpError(err)
}
}
Loading

0 comments on commit 861e5fc

Please sign in to comment.