major refactoring

This commit is contained in:
TotallyNot 2024-04-04 15:59:10 +02:00
parent 01bbe37876
commit 75fc19d0f7
10 changed files with 404 additions and 222 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "torn-api"
version = "0.6.7"
version = "0.7.0"
edition = "2021"
rust-version = "1.75.0"
authors = ["Pyrit [2111649]"]
@ -39,7 +39,7 @@ reqwest = { version = "0.11", default-features = false, features = [ "json" ], o
awc = { version = "3", default-features = false, optional = true }
rust_decimal = { version = "1", default-features = false, optional = true, features = [ "serde" ] }
torn-api-macros = { path = "../torn-api-macros", version = "0.2" }
torn-api-macros = { path = "../torn-api-macros", version = "0.3" }
[dev-dependencies]
actix-rt = { version = "2.7.0" }

View file

@ -111,18 +111,14 @@ impl ApiResponse {
}
}
pub trait ApiSelection: Send + Sync {
pub trait ApiSelection: Send + Sync + 'static {
type Response: From<ApiResponse> + Send + Sync;
fn raw_value(self) -> &'static str;
fn category() -> &'static str;
}
pub trait ApiCategoryResponse: Send + Sync {
type Selection: ApiSelection;
fn from_response(response: ApiResponse) -> Self;
}
pub struct DirectExecutor<C> {
key: String,
_marker: std::marker::PhantomData<C>,

View file

@ -2,9 +2,7 @@ use std::collections::HashMap;
use async_trait::async_trait;
use crate::{
ApiCategoryResponse, ApiClientError, ApiRequest, ApiResponse, ApiSelection, DirectExecutor,
};
use crate::{ApiClientError, ApiRequest, ApiResponse, ApiSelection, DirectExecutor};
pub struct ApiProvider<'a, C, E>
where
@ -39,7 +37,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::user::Response::from_response)
}
#[cfg(feature = "user")]
@ -61,9 +58,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::user::Response::from_response)))
.collect()
}
#[cfg(feature = "faction")]
@ -79,7 +73,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::faction::Response::from_response)
}
#[cfg(feature = "faction")]
@ -101,9 +94,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::faction::Response::from_response)))
.collect()
}
#[cfg(feature = "market")]
@ -119,7 +109,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::market::Response::from_response)
}
#[cfg(feature = "market")]
@ -141,9 +130,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::market::Response::from_response)))
.collect()
}
#[cfg(feature = "torn")]
@ -159,7 +145,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::torn::Response::from_response)
}
#[cfg(feature = "torn")]
@ -181,9 +166,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::torn::Response::from_response)))
.collect()
}
#[cfg(feature = "key")]
@ -199,7 +181,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::key::Response::from_response)
}
}
@ -215,7 +196,7 @@ where
client: &C,
request: ApiRequest<A>,
id: Option<String>,
) -> Result<ApiResponse, Self::Error>
) -> Result<A::Response, Self::Error>
where
A: ApiSelection;
@ -224,7 +205,7 @@ where
client: &C,
request: ApiRequest<A>,
ids: Vec<I>,
) -> HashMap<I, Result<ApiResponse, Self::Error>>
) -> HashMap<I, Result<A::Response, Self::Error>>
where
A: ApiSelection,
I: ToString + std::hash::Hash + std::cmp::Eq;
@ -242,7 +223,7 @@ where
client: &C,
request: ApiRequest<A>,
id: Option<String>,
) -> Result<ApiResponse, Self::Error>
) -> Result<A::Response, Self::Error>
where
A: ApiSelection,
{
@ -250,7 +231,7 @@ where
let value = client.request(url).await.map_err(ApiClientError::Client)?;
Ok(ApiResponse::from_value(value)?)
Ok(ApiResponse::from_value(value)?.into())
}
async fn execute_many<A, I>(
@ -258,7 +239,7 @@ where
client: &C,
request: ApiRequest<A>,
ids: Vec<I>,
) -> HashMap<I, Result<ApiResponse, Self::Error>>
) -> HashMap<I, Result<A::Response, Self::Error>>
where
A: ApiSelection,
I: ToString + std::hash::Hash + std::cmp::Eq,
@ -272,7 +253,11 @@ where
(
i,
value.and_then(|v| ApiResponse::from_value(v).map_err(Into::into)),
value.and_then(|v| {
ApiResponse::from_value(v)
.map(Into::into)
.map_err(Into::into)
}),
)
}))
.await;

View file

@ -2,9 +2,7 @@ use std::collections::HashMap;
use async_trait::async_trait;
use crate::{
ApiCategoryResponse, ApiClientError, ApiRequest, ApiResponse, ApiSelection, DirectExecutor,
};
use crate::{ApiClientError, ApiRequest, ApiResponse, ApiSelection, DirectExecutor};
pub struct ApiProvider<'a, C, E>
where
@ -37,7 +35,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::user::Response::from_response)
}
#[cfg(feature = "user")]
@ -59,9 +56,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::user::Response::from_response)))
.collect()
}
#[cfg(feature = "faction")]
@ -77,7 +71,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::faction::Response::from_response)
}
#[cfg(feature = "faction")]
@ -99,9 +92,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::faction::Response::from_response)))
.collect()
}
#[cfg(feature = "market")]
@ -117,7 +107,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::market::Response::from_response)
}
#[cfg(feature = "market")]
@ -139,9 +128,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::market::Response::from_response)))
.collect()
}
#[cfg(feature = "torn")]
@ -157,7 +143,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::torn::Response::from_response)
}
#[cfg(feature = "torn")]
@ -179,9 +164,6 @@ where
self.executor
.execute_many(self.client, builder.request, Vec::from_iter(ids))
.await
.into_iter()
.map(|(k, v)| (k, v.map(crate::torn::Response::from_response)))
.collect()
}
#[cfg(feature = "key")]
@ -197,7 +179,6 @@ where
self.executor
.execute(self.client, builder.request, builder.id)
.await
.map(crate::key::Response::from_response)
}
}
@ -213,7 +194,7 @@ where
client: &C,
request: ApiRequest<A>,
id: Option<String>,
) -> Result<ApiResponse, Self::Error>
) -> Result<A::Response, Self::Error>
where
A: ApiSelection;
@ -222,7 +203,7 @@ where
client: &C,
request: ApiRequest<A>,
ids: Vec<I>,
) -> HashMap<I, Result<ApiResponse, Self::Error>>
) -> HashMap<I, Result<A::Response, Self::Error>>
where
A: ApiSelection,
I: ToString + std::hash::Hash + std::cmp::Eq + Send + Sync;
@ -240,7 +221,7 @@ where
client: &C,
request: ApiRequest<A>,
id: Option<String>,
) -> Result<ApiResponse, Self::Error>
) -> Result<A::Response, Self::Error>
where
A: ApiSelection,
{
@ -248,7 +229,7 @@ where
let value = client.request(url).await.map_err(ApiClientError::Client)?;
Ok(ApiResponse::from_value(value)?)
Ok(ApiResponse::from_value(value)?.into())
}
async fn execute_many<A, I>(
@ -256,7 +237,7 @@ where
client: &C,
request: ApiRequest<A>,
ids: Vec<I>,
) -> HashMap<I, Result<ApiResponse, Self::Error>>
) -> HashMap<I, Result<A::Response, Self::Error>>
where
A: ApiSelection,
I: ToString + std::hash::Hash + std::cmp::Eq + Send + Sync,
@ -270,7 +251,11 @@ where
(
i,
value.and_then(|v| ApiResponse::from_value(v).map_err(Into::into)),
value.and_then(|v| {
ApiResponse::from_value(v)
.map(Into::into)
.map_err(Into::into)
}),
)
}))
.await;