use IntoSelector
trait for key pool requests
This commit is contained in:
parent
6da09226a6
commit
1589811de3
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "torn-key-pool"
|
name = "torn-key-pool"
|
||||||
version = "0.6.0"
|
version = "0.6.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["Pyrit [2111649]"]
|
authors = ["Pyrit [2111649]"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -29,8 +29,8 @@ where
|
||||||
Response(ResponseError),
|
Response(ResponseError),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ApiKey: Sync + Send {
|
pub trait ApiKey: Sync + Send + std::fmt::Debug + Clone {
|
||||||
type IdType: PartialEq + Eq + std::hash::Hash + Send + Sync;
|
type IdType: PartialEq + Eq + std::hash::Hash + Send + Sync + std::fmt::Debug + Clone;
|
||||||
|
|
||||||
fn value(&self) -> &str;
|
fn value(&self) -> &str;
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ where
|
||||||
Self::Key(_) | Self::UserId(_) | Self::Id(_) => None,
|
Self::Key(_) | Self::UserId(_) | Self::Id(_) => None,
|
||||||
Self::Has(domain) => domain.fallback().map(Self::Has),
|
Self::Has(domain) => domain.fallback().map(Self::Has),
|
||||||
Self::OneOf(domains) => {
|
Self::OneOf(domains) => {
|
||||||
let fallbacks: Vec<_> = domains.into_iter().filter_map(|d| d.fallback()).collect();
|
let fallbacks: Vec<_> = domains.iter().filter_map(|d| d.fallback()).collect();
|
||||||
if fallbacks.is_empty() {
|
if fallbacks.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
@ -176,7 +176,7 @@ where
|
||||||
{
|
{
|
||||||
storage: &'a S,
|
storage: &'a S,
|
||||||
comment: Option<&'a str>,
|
comment: Option<&'a str>,
|
||||||
domain: S::Domain,
|
selector: KeySelector<S::Key, S::Domain>,
|
||||||
_marker: std::marker::PhantomData<C>,
|
_marker: std::marker::PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,10 +184,14 @@ impl<'a, C, S> KeyPoolExecutor<'a, C, S>
|
||||||
where
|
where
|
||||||
S: KeyPoolStorage,
|
S: KeyPoolStorage,
|
||||||
{
|
{
|
||||||
pub fn new(storage: &'a S, domain: S::Domain, comment: Option<&'a str>) -> Self {
|
pub fn new(
|
||||||
|
storage: &'a S,
|
||||||
|
selector: KeySelector<S::Key, S::Domain>,
|
||||||
|
comment: Option<&'a str>,
|
||||||
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
storage,
|
storage,
|
||||||
domain,
|
selector,
|
||||||
comment,
|
comment,
|
||||||
_marker: std::marker::PhantomData,
|
_marker: std::marker::PhantomData,
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ use torn_api::{
|
||||||
ApiRequest, ApiResponse, ApiSelection, ResponseError,
|
ApiRequest, ApiResponse, ApiSelection, ResponseError,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{ApiKey, KeyPoolError, KeyPoolExecutor, KeyPoolStorage};
|
use crate::{ApiKey, KeyPoolError, KeyPoolExecutor, KeyPoolStorage, IntoSelector};
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
impl<'client, C, S> RequestExecutor<C> for KeyPoolExecutor<'client, C, S>
|
impl<'client, C, S> RequestExecutor<C> for KeyPoolExecutor<'client, C, S>
|
||||||
|
@ -30,7 +30,7 @@ where
|
||||||
loop {
|
loop {
|
||||||
let key = self
|
let key = self
|
||||||
.storage
|
.storage
|
||||||
.acquire_key(self.domain.clone())
|
.acquire_key(self.selector.clone())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| KeyPoolError::Storage(Arc::new(e)))?;
|
.map_err(|e| KeyPoolError::Storage(Arc::new(e)))?;
|
||||||
let url = request.url(key.value(), id.as_deref());
|
let url = request.url(key.value(), id.as_deref());
|
||||||
|
@ -66,7 +66,7 @@ where
|
||||||
{
|
{
|
||||||
let keys = match self
|
let keys = match self
|
||||||
.storage
|
.storage
|
||||||
.acquire_many_keys(self.domain.clone(), ids.len() as i64)
|
.acquire_many_keys(self.selector.clone(), ids.len() as i64)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(keys) => keys,
|
Ok(keys) => keys,
|
||||||
|
@ -114,7 +114,7 @@ where
|
||||||
Ok(res) => return (id, Ok(res)),
|
Ok(res) => return (id, Ok(res)),
|
||||||
};
|
};
|
||||||
|
|
||||||
key = match self.storage.acquire_key(self.domain.clone()).await {
|
key = match self.storage.acquire_key(self.selector.clone()).await {
|
||||||
Ok(k) => k,
|
Ok(k) => k,
|
||||||
Err(why) => return (id, Err(Self::Error::Storage(Arc::new(why)))),
|
Err(why) => return (id, Err(Self::Error::Storage(Arc::new(why)))),
|
||||||
};
|
};
|
||||||
|
@ -150,25 +150,26 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn torn_api(&self, domain: S::Domain) -> ApiProvider<C, KeyPoolExecutor<C, S>> {
|
pub fn torn_api<I>(&self, selector: I) -> ApiProvider<C, KeyPoolExecutor<C, S>> where I: IntoSelector<S::Key, S::Domain> {
|
||||||
ApiProvider::new(
|
ApiProvider::new(
|
||||||
&self.client,
|
&self.client,
|
||||||
KeyPoolExecutor::new(&self.storage, domain, self.comment.as_deref()),
|
KeyPoolExecutor::new(&self.storage, selector.into_selector(), self.comment.as_deref()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WithStorage {
|
pub trait WithStorage {
|
||||||
fn with_storage<'a, S>(
|
fn with_storage<'a, S, I>(
|
||||||
&'a self,
|
&'a self,
|
||||||
storage: &'a S,
|
storage: &'a S,
|
||||||
domain: S::Domain,
|
selector: I
|
||||||
) -> ApiProvider<Self, KeyPoolExecutor<Self, S>>
|
) -> ApiProvider<Self, KeyPoolExecutor<Self, S>>
|
||||||
where
|
where
|
||||||
Self: ApiClient + Sized,
|
Self: ApiClient + Sized,
|
||||||
S: KeyPoolStorage + 'static,
|
S: KeyPoolStorage + 'static,
|
||||||
|
I: IntoSelector<S::Key, S::Domain>
|
||||||
{
|
{
|
||||||
ApiProvider::new(self, KeyPoolExecutor::new(storage, domain, None))
|
ApiProvider::new(self, KeyPoolExecutor::new(storage, selector.into_selector(), None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ use torn_api::{
|
||||||
ApiRequest, ApiResponse, ApiSelection, ResponseError,
|
ApiRequest, ApiResponse, ApiSelection, ResponseError,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{ApiKey, KeyPoolError, KeyPoolExecutor, KeyPoolStorage};
|
use crate::{ApiKey, IntoSelector, KeyPoolError, KeyPoolExecutor, KeyPoolStorage};
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<'client, C, S> RequestExecutor<C> for KeyPoolExecutor<'client, C, S>
|
impl<'client, C, S> RequestExecutor<C> for KeyPoolExecutor<'client, C, S>
|
||||||
|
@ -30,7 +30,7 @@ where
|
||||||
loop {
|
loop {
|
||||||
let key = self
|
let key = self
|
||||||
.storage
|
.storage
|
||||||
.acquire_key(self.domain.clone())
|
.acquire_key(self.selector.clone())
|
||||||
.await
|
.await
|
||||||
.map_err(|e| KeyPoolError::Storage(Arc::new(e)))?;
|
.map_err(|e| KeyPoolError::Storage(Arc::new(e)))?;
|
||||||
let url = request.url(key.value(), id.as_deref());
|
let url = request.url(key.value(), id.as_deref());
|
||||||
|
@ -66,7 +66,7 @@ where
|
||||||
{
|
{
|
||||||
let keys = match self
|
let keys = match self
|
||||||
.storage
|
.storage
|
||||||
.acquire_many_keys(self.domain.clone(), ids.len() as i64)
|
.acquire_many_keys(self.selector.clone(), ids.len() as i64)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(keys) => keys,
|
Ok(keys) => keys,
|
||||||
|
@ -114,7 +114,7 @@ where
|
||||||
Ok(res) => return (id, Ok(res)),
|
Ok(res) => return (id, Ok(res)),
|
||||||
};
|
};
|
||||||
|
|
||||||
key = match self.storage.acquire_key(self.domain.clone()).await {
|
key = match self.storage.acquire_key(self.selector.clone()).await {
|
||||||
Ok(k) => k,
|
Ok(k) => k,
|
||||||
Err(why) => return (id, Err(Self::Error::Storage(Arc::new(why)))),
|
Err(why) => return (id, Err(Self::Error::Storage(Arc::new(why)))),
|
||||||
};
|
};
|
||||||
|
@ -150,25 +150,36 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn torn_api(&self, domain: S::Domain) -> ApiProvider<C, KeyPoolExecutor<C, S>> {
|
pub fn torn_api<I>(&self, selector: I) -> ApiProvider<C, KeyPoolExecutor<C, S>>
|
||||||
|
where
|
||||||
|
I: IntoSelector<S::Key, S::Domain>,
|
||||||
|
{
|
||||||
ApiProvider::new(
|
ApiProvider::new(
|
||||||
&self.client,
|
&self.client,
|
||||||
KeyPoolExecutor::new(&self.storage, domain, self.comment.as_deref()),
|
KeyPoolExecutor::new(
|
||||||
|
&self.storage,
|
||||||
|
selector.into_selector(),
|
||||||
|
self.comment.as_deref(),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WithStorage {
|
pub trait WithStorage {
|
||||||
fn with_storage<'a, S>(
|
fn with_storage<'a, S, I>(
|
||||||
&'a self,
|
&'a self,
|
||||||
storage: &'a S,
|
storage: &'a S,
|
||||||
domain: S::Domain,
|
selector: I,
|
||||||
) -> ApiProvider<Self, KeyPoolExecutor<Self, S>>
|
) -> ApiProvider<Self, KeyPoolExecutor<Self, S>>
|
||||||
where
|
where
|
||||||
Self: ApiClient + Sized,
|
Self: ApiClient + Sized,
|
||||||
S: KeyPoolStorage + Send + Sync + 'static,
|
S: KeyPoolStorage + Send + Sync + 'static,
|
||||||
|
I: IntoSelector<S::Key, S::Domain>,
|
||||||
{
|
{
|
||||||
ApiProvider::new(self, KeyPoolExecutor::new(storage, domain, None))
|
ApiProvider::new(
|
||||||
|
self,
|
||||||
|
KeyPoolExecutor::new(storage, selector.into_selector(), None),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue