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 json::JsonValue;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use crate::tenor_types::TenorError;
|
use crate::tenor_types::TenorError;
|
||||||
|
|
||||||
use crate::tenor_types::{ContentFilter, ArRange, MediaFilter};
|
use crate::tenor_types::{ContentFilter, ArRange, MediaFilter};
|
||||||
|
|
||||||
pub struct Tenor {
|
pub struct Tenor {
|
||||||
|
@ -34,22 +35,30 @@ pub struct Tenor {
|
||||||
/// Use a non-zero, non-empty value from next, returned by the API response, to get the next set of results
|
/// Use a non-zero, non-empty value from next, returned by the API response, to get the next set of results
|
||||||
pos: Option<String>,
|
pos: Option<String>,
|
||||||
/// Tenor api key to be used in queries
|
/// Tenor api key to be used in queries
|
||||||
key: Option<String>
|
key: String
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tenor {
|
impl Tenor {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Result<Self, TenorError> {
|
||||||
Tenor {
|
dotenv().ok();
|
||||||
country: None,
|
let key = match env::var("TENORV2_TOKEN") {
|
||||||
locale: None,
|
Ok(key) => key,
|
||||||
contentfilter: None,
|
Err(_) => return Err(TenorError::KeyError("Could not find TENORV2_TOKEN".to_string()))
|
||||||
media_filter: None,
|
};
|
||||||
ar_range: None,
|
|
||||||
random: None,
|
Ok(
|
||||||
limit: None,
|
Tenor {
|
||||||
pos: None,
|
country: None,
|
||||||
key: None
|
locale: None,
|
||||||
}
|
contentfilter: None,
|
||||||
|
media_filter: None,
|
||||||
|
ar_range: None,
|
||||||
|
random: None,
|
||||||
|
limit: None,
|
||||||
|
pos: None,
|
||||||
|
key
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replaces current country with the passed one
|
/// Replaces current country with the passed one
|
||||||
|
@ -117,20 +126,15 @@ impl Tenor {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn key(mut self, key: String) -> Self {
|
pub fn key(mut self, key: String) -> Self {
|
||||||
self.key = Some(key);
|
self.key = key;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn search(self, query: &str) -> Result<JsonValue, TenorError> {
|
pub async fn search(self, query: &str) -> Result<JsonValue, TenorError> {
|
||||||
let q: String = form_urlencoded::byte_serialize(query.as_bytes()).collect();
|
let q: String = form_urlencoded::byte_serialize(query.as_bytes()).collect();
|
||||||
|
|
||||||
let base_url = "https://tenor.googleapis.com/v2/search?";
|
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() {
|
if self.country.is_some() {
|
||||||
request.push_str(&format!("&country={}", self.country.unwrap()));
|
request.push_str(&format!("&country={}", self.country.unwrap()));
|
||||||
|
|
Loading…
Reference in a new issue