feat: change how key works
This commit is contained in:
parent
5c9033a502
commit
ddf5b39fe8
1 changed files with 27 additions and 23 deletions
|
@ -1,9 +1,10 @@
|
|||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
|
||||
use dotenv::dotenv;
|
||||
use json::JsonValue;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use crate::tenor_types::TenorError;
|
||||
|
||||
use crate::tenor_types::{ContentFilter, ArRange, MediaFilter};
|
||||
|
||||
pub struct Tenor {
|
||||
|
@ -34,11 +35,18 @@ pub struct Tenor {
|
|||
/// Use a non-zero, non-empty value from next, returned by the API response, to get the next set of results
|
||||
pos: Option<String>,
|
||||
/// Tenor api key to be used in queries
|
||||
key: Option<String>
|
||||
key: String
|
||||
}
|
||||
|
||||
impl Tenor {
|
||||
pub fn new() -> Self {
|
||||
pub fn new() -> Result<Self, TenorError> {
|
||||
dotenv().ok();
|
||||
let key = match env::var("TENORV2_TOKEN") {
|
||||
Ok(key) => key,
|
||||
Err(_) => return Err(TenorError::KeyError("Could not find TENORV2_TOKEN".to_string()))
|
||||
};
|
||||
|
||||
Ok(
|
||||
Tenor {
|
||||
country: None,
|
||||
locale: None,
|
||||
|
@ -48,8 +56,9 @@ impl Tenor {
|
|||
random: None,
|
||||
limit: None,
|
||||
pos: None,
|
||||
key: None
|
||||
key
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/// Replaces current country with the passed one
|
||||
|
@ -117,20 +126,15 @@ impl Tenor {
|
|||
}
|
||||
|
||||
pub fn key(mut self, key: String) -> Self {
|
||||
self.key = Some(key);
|
||||
self.key = key;
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn search(self, query: &str) -> Result<JsonValue, TenorError> {
|
||||
let q: String = form_urlencoded::byte_serialize(query.as_bytes()).collect();
|
||||
|
||||
let base_url = "https://tenor.googleapis.com/v2/search?";
|
||||
let api_key = match self.key {
|
||||
Some(key) => key,
|
||||
None => return Err(TenorError::KeyError("API key undefined".into()))
|
||||
};
|
||||
|
||||
let mut request = format!("{base_url}q={q}&key={api_key}");
|
||||
let mut request = format!("{}q={}&key={}", base_url, q, self.key);
|
||||
|
||||
if self.country.is_some() {
|
||||
request.push_str(&format!("&country={}", self.country.unwrap()));
|
||||
|
|
Loading…
Reference in a new issue