mod files;
pub mod fingerprint;
mod fingerprints;
mod games;
mod minecraft;
pub mod models;
mod mods;
pub use fingerprint::{
Murmur2Hasher, cf_fingerprint, cf_fingerprint_file, cf_fingerprint_read_seek,
cf_fingerprint_reader, cf_fingerprint_with_len,
};
use reqwest::header::HeaderValue;
use serde::Serialize;
use serde::de::DeserializeOwned;
use self::models::{DataEnvelope, PaginatedData};
use crate::error::{Error, Result};
use crate::http::HttpCore;
use crate::mirror::Mirror;
use crate::util::{impl_redacted_debug, trim_trailing_slash};
pub const DEFAULT_BASE_URL: &str = "https://api.curseforge.com";
const API_KEY_HEADER: &str = "x-api-key";
#[derive(Clone)]
pub struct CurseForgeClient {
base_url: String,
api_key: Option<HeaderValue>,
http: HttpCore,
}
impl_redacted_debug!(CurseForgeClient {
base_url, http;
redacted:;
optional_redacted: api_key,
});
impl CurseForgeClient {
pub fn new(api_key: impl Into<String>) -> Result<Self> {
let key = api_key.into();
Self::from_http(HttpCore::new()?, Some(&key))
}
pub fn with_mirror(mirror: Mirror) -> Result<Self> {
Self::from_http(HttpCore::builder().mirror(mirror).build()?, None)
}
pub fn from_http(http: HttpCore, api_key: Option<&str>) -> Result<Self> {
match api_key.filter(|k| !k.trim().is_empty()) {
Some(key) => {
let mut val = HeaderValue::from_str(key)?;
val.set_sensitive(true);
Ok(Self {
base_url: DEFAULT_BASE_URL.to_string(),
api_key: Some(val),
http,
})
}
None if http.mirror().is_rewritten(DEFAULT_BASE_URL) => Ok(Self {
base_url: DEFAULT_BASE_URL.to_string(),
api_key: None,
http,
}),
None => Err(Error::InvalidInput(
"CurseForge official API requires an x-api-key (or a mirror rewriting api.curseforge.com)"
.to_string(),
)),
}
}
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = trim_trailing_slash(base_url);
self
}
pub fn base_url(&self) -> &str {
&self.base_url
}
pub fn http(&self) -> &HttpCore {
&self.http
}
fn ensure_target_auth(&self, url: &str) -> Result<()> {
if !self.http.mirror().is_rewritten(url)
&& self.api_key.is_none()
&& self.base_url == DEFAULT_BASE_URL
{
return Err(Error::InvalidInput(
"CurseForge official API requires an x-api-key".to_string(),
));
}
Ok(())
}
async fn send_json<F, T>(&self, path: &str, mut build: F) -> Result<T>
where
F: FnMut(&reqwest::Client, &str) -> reqwest::RequestBuilder,
T: DeserializeOwned,
{
let url = format!("{}{}", self.base_url, path);
self.ensure_target_auth(&url)?;
self.http
.send_json_with(&url, |client, target| {
let req = build(client, target);
if let Some(ref key) = self.api_key
&& target.starts_with(&self.base_url)
{
req.header(API_KEY_HEADER, key.clone())
} else {
req
}
})
.await
}
pub(crate) async fn get_data<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
let env: DataEnvelope<T> = self
.send_json(path, |client, target| client.get(target))
.await?;
Ok(env.data)
}
pub(crate) async fn get_data_query<Q: Serialize + ?Sized, T: DeserializeOwned>(
&self,
path: &str,
query: &Q,
) -> Result<T> {
let env: DataEnvelope<T> = self
.send_json(path, |client, target| client.get(target).query(query))
.await?;
Ok(env.data)
}
pub(crate) async fn get_paginated<Q: Serialize + ?Sized, T: DeserializeOwned>(
&self,
path: &str,
query: &Q,
) -> Result<PaginatedData<T>> {
self.send_json(path, |client, target| client.get(target).query(query))
.await
}
pub(crate) async fn post_data<B: Serialize + ?Sized, T: DeserializeOwned>(
&self,
path: &str,
body: &B,
) -> Result<T> {
let env: DataEnvelope<T> = self
.send_json(path, |client, target| client.post(target).json(body))
.await?;
Ok(env.data)
}
}