fix: use our own result types instead of tenors types
add user auth
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use reqwest::Client;
|
||||
use tokio::sync::RwLock;
|
||||
use lru_time_cache::LruCache;
|
||||
use urlencoding::encode as url_encode;
|
||||
use reqwest::Client;
|
||||
use revolt_coalesced::{CoalescionService, CoalescionServiceConfig};
|
||||
use tokio::sync::RwLock;
|
||||
use urlencoding::encode as url_encode;
|
||||
|
||||
pub mod types;
|
||||
|
||||
@@ -20,13 +20,13 @@ pub struct Tenor {
|
||||
pub key: Arc<str>,
|
||||
pub client: Client,
|
||||
pub coalescion: CoalescionService<String, Result<Arc<types::SearchResponse>, TenorError>>,
|
||||
pub cache: Arc<RwLock<LruCache<String, Arc<types::SearchResponse>>>>
|
||||
pub cache: Arc<RwLock<LruCache<String, Arc<types::SearchResponse>>>>,
|
||||
}
|
||||
|
||||
impl Tenor {
|
||||
pub fn new(key: &str) -> Self {
|
||||
// 1 hour, 1k queries
|
||||
let cache = LruCache::with_expiry_duration_and_capacity(Duration::from_secs(60*60), 1000);
|
||||
let cache = LruCache::with_expiry_duration_and_capacity(Duration::from_secs(60 * 60), 1000);
|
||||
|
||||
Self {
|
||||
key: Arc::from(key),
|
||||
@@ -34,18 +34,23 @@ impl Tenor {
|
||||
coalescion: CoalescionService::from_config(CoalescionServiceConfig {
|
||||
max_concurrent: Some(100),
|
||||
queue_requests: true,
|
||||
max_queue: None
|
||||
max_queue: None,
|
||||
}),
|
||||
cache: Arc::new(RwLock::new(cache)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn search(&self, query: &str, locale: &str, position: Option<&str>) -> Result<Arc<types::SearchResponse>, TenorError> {
|
||||
pub async fn search(
|
||||
&self,
|
||||
query: &str,
|
||||
locale: &str,
|
||||
position: Option<&str>,
|
||||
) -> Result<Arc<types::SearchResponse>, TenorError> {
|
||||
let unique_key = format!("{query}:{locale}:{position:?}");
|
||||
|
||||
if self.cache.read().await.contains_key(&unique_key) {
|
||||
if let Some(response) = self.cache.write().await.get(&unique_key) {
|
||||
return Ok(response.clone())
|
||||
return Ok(response.clone());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +59,7 @@ impl Tenor {
|
||||
|
||||
async move {
|
||||
let mut url = format!(
|
||||
"{TENOR_API_BASE_URL}/search?key={}&q={}&client_key=Gifbox&locale={}&contentfilter=medium&limit=1",
|
||||
"{TENOR_API_BASE_URL}/search?key={}&q={}&client_key=Gifbox&media_filter=webm,tinywebm&locale={}&contentfilter=medium&limit=1",
|
||||
&self.key,
|
||||
url_encode(query),
|
||||
url_encode(locale),
|
||||
@@ -86,44 +91,3 @@ impl Tenor {
|
||||
(*res).clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use revolt_config::config;
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn test() {
|
||||
let config = config().await;
|
||||
|
||||
let tenor = Tenor::new(&config.api.security.tenor_key);
|
||||
|
||||
let results =tenor.search("amog", "en_US", None).await.unwrap();
|
||||
|
||||
let result = &results.results[0];
|
||||
|
||||
println!("{:?}", result.media_formats.iter().next().unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn test_2() {
|
||||
let config = config().await;
|
||||
let tenor = Tenor::new(&config.api.security.tenor_key);
|
||||
|
||||
let mut tasks = Vec::new();
|
||||
|
||||
for i in 1..1001 {
|
||||
let tenor = tenor.clone();
|
||||
println!("creating search task {i}");
|
||||
|
||||
tasks.push((i, tokio::spawn(async move {
|
||||
tenor.search(&format!("amog-{i}"), "en_US", None).await
|
||||
})));
|
||||
};
|
||||
|
||||
for (i, task) in tasks {
|
||||
task.await.unwrap().unwrap();
|
||||
println!("Got result for {i}");
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user