client is now not static anymore

This commit is contained in:
LordMZTE 2021-05-27 18:01:30 +02:00
parent 71be49db24
commit 92a77051ce
4 changed files with 143 additions and 159 deletions

View file

@ -1,5 +1,9 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Deserializer, de::{self, Visitor}};
use serde::{
de::{self, Visitor},
Deserialize,
Deserializer,
};
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UpResp {
@ -60,8 +64,8 @@ impl User {
pub fn get_id(&self) -> Option<&String> {
self.id
.as_ref()
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
.or_else(|| self.tokenhash.as_ref())
.or_else(|| self.userdir.as_ref())
}
}
@ -92,7 +96,8 @@ where
where
E: de::Error,
{
v.parse().map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &self))
v.parse()
.map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &self))
}
}

130
src/client.rs Normal file
View file

@ -0,0 +1,130 @@
use crate::api::{Category, CatsResp, Meme, MemesResp, User, UsersResp};
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Url;
use serde::de::DeserializeOwned;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::Mutex;
#[derive(Debug)]
pub struct JMClient {
pub http: reqwest::Client,
cache: Mutex<Cache>,
endpoint: Url,
}
macro_rules! init_cache {
($cell:expr, $init_fn:expr) => {{
let cell = &$cell;
Arc::clone(match cell.get() {
Some(x) => x,
None => {
let x = Arc::new($init_fn);
cell.get_or_init(|| x)
},
})
}};
}
impl JMClient {
pub fn new() -> Self {
Self::builder().build()
}
pub fn builder() -> JMClientBuilder {
JMClientBuilder::default()
}
async fn get_api_json<R: DeserializeOwned, T, F: FnOnce(R) -> T>(
&self,
cache: &OnceCell<Arc<T>>,
endpoint: &str,
res_to_data: F,
) -> Result<Arc<T>, JMClientError> {
Ok(init_cache!(cache, {
info!("Requesting {} from server", endpoint);
let url = self.endpoint.join(endpoint)?;
let res = self.http.get(url).send().await?;
let res = serde_json::from_slice(&res.bytes().await?)?;
res_to_data(res)
}))
}
pub async fn get_cats(&self) -> Result<Arc<Vec<Category>>, JMClientError> {
self.get_api_json(
&self.cache.lock().await.cats,
"categories",
|r: CatsResp| r.categories,
)
.await
}
pub async fn get_memes(&self) -> Result<Arc<Vec<Meme>>, JMClientError> {
self.get_api_json(&self.cache.lock().await.memes, "memes", |r: MemesResp| {
r.memes
})
.await
}
pub async fn get_users(&self) -> Result<Arc<Vec<User>>, JMClientError> {
self.get_api_json(&self.cache.lock().await.users, "users", |r: UsersResp| {
r.users
})
.await
}
}
impl Default for JMClient {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Error)]
pub enum JMClientError {
#[error("Error making http request: {0}")]
Http(#[from] reqwest::Error),
#[error("Error deserializing JensMemes response: {0}")]
Deserialize(#[from] serde_json::Error),
#[error("Failed parsing URL to make request to JensMemes: {0}")]
UrlParse(#[from] url::ParseError),
}
#[derive(Debug, Default)]
struct Cache {
users: OnceCell<Arc<Vec<User>>>,
cats: OnceCell<Arc<Vec<Category>>>,
memes: OnceCell<Arc<Vec<Meme>>>,
}
#[derive(Debug, Default)]
pub struct JMClientBuilder {
client: Option<reqwest::Client>,
endpoint: Option<Url>,
}
impl JMClientBuilder {
pub fn build(self) -> JMClient {
JMClient {
http: self.client.unwrap_or_else(reqwest::Client::new),
endpoint: self.endpoint.unwrap_or_else(|| {
// unwrapping is fine here, as the hardcoded input is known to work.
Url::parse(crate::util::consts::API_ENDPOINT).unwrap()
}),
cache: Mutex::new(Cache::default()),
}
}
pub fn client(mut self, client: reqwest::Client) -> Self {
self.client = Some(client);
self
}
pub fn endpoint(mut self, endpoint: impl Into<Url>) -> Self {
self.endpoint = Some(endpoint.into());
self
}
}

View file

@ -1,2 +1,5 @@
pub mod api;
pub mod client;
pub mod util;
pub use client::JMClient;

View file

@ -1,150 +1,9 @@
use crate::api::Meme;
use anyhow::bail;
use reqwest::Client;
use std::str::FromStr;
#[macro_export]
macro_rules! init_once_cell {
($cell:ident, $init_fn:expr) => {
match $cell.get() {
Some(x) => x,
None => {
let x = $init_fn;
$cell.get_or_init(|| x)
},
}
};
}
pub mod consts {
pub const API_ENDPOINT: &str = "https://api.tilera.xyz/jensmemes/v1";
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
}
/// ways to communicyte with the JM API
pub mod api {
use super::consts;
use crate::api::{Category, CatsResp, Meme, MemesResp, User, UsersResp};
use anyhow::Result;
use lazy_static::lazy_static;
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Client;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use thiserror::Error;
use url::Url;
// cached api responses
static USERS: OnceCell<Vec<User>> = OnceCell::new();
static CATS: OnceCell<Vec<Category>> = OnceCell::new();
lazy_static! {
// is this type long enough yet?
static ref MEMES: Mutex<HashMap<(Option<String>, Option<String>), Arc<Vec<Meme>>>> =
Mutex::new(HashMap::new());
}
#[derive(Error, Debug)]
#[error("Error Deserializing JSON api response: \n{json}\nError: {error}")]
pub struct ApiDeserializeError {
pub json: String,
#[source]
pub error: serde_json::Error,
}
pub async fn cats(http: &Client) -> Result<&Vec<Category>> {
Ok(init_once_cell!(CATS, {
info!("Requesting categories from server");
let res = http
.get(&format!("{}/categories", consts::API_ENDPOINT))
.send()
.await?;
let cats = try_deserialize_api_reponse::<CatsResp>(&res.bytes().await?)?;
cats.categories.into_iter().collect()
}))
}
pub async fn memes<'a>(
http: &Client,
cat_filter: Option<String>,
usr_filter: Option<String>,
) -> Result<Arc<Vec<Meme>>> {
let filters = (cat_filter, usr_filter);
if let Some(m) = MEMES.lock().unwrap().get(&filters) {
return Ok(m.clone());
}
let mut url = Url::options().parse(&format!("{}/memes", consts::API_ENDPOINT))?;
let mut pairs = url.query_pairs_mut();
if let Some(cat) = filters.0.as_ref() {
pairs.append_pair("category", cat);
}
if let Some(usr) = filters.1.as_ref() {
pairs.append_pair("user", usr);
}
// drop required in order to move the URL into the request
drop(pairs);
info!("Requesting memes from server");
let res = http.get(url).send().await?;
let memes = try_deserialize_api_reponse::<MemesResp>(&res.bytes().await?)?;
Ok(MEMES
.lock()
.unwrap()
.entry(filters)
.or_insert(Arc::new(memes.memes))
.clone())
}
pub async fn users(http: &Client) -> Result<&Vec<User>> {
Ok(init_once_cell!(USERS, {
info!("Requesting users from server");
let res = http
.get(&format!("{}/users", consts::API_ENDPOINT))
.send()
.await?;
let users = try_deserialize_api_reponse::<UsersResp>(&res.bytes().await?)?;
users.users
}))
}
/// tries to deserialize `json` as `T`, and if it fails converts the error
/// to a `ApiDeserializeError`
pub fn try_deserialize_api_reponse<'a, T: serde::Deserialize<'a>>(json: &'a [u8]) -> Result<T> {
let result = serde_json::from_slice::<T>(&json);
result.map_err(|error| match std::str::from_utf8(json) {
Err(e) => e.into(),
Ok(json) => ApiDeserializeError {
json: json.into(),
error,
}
.into(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_deserialize_error() {
let incorrect_json = r#"
{
"foo": "bar"
}
"#;
let result = try_deserialize_api_reponse::<UsersResp>(incorrect_json.as_bytes());
println!("{}", result.unwrap_err());
}
}
pub const API_ENDPOINT: &str = "https://api.tilera.xyz/jensmemes/v1/";
}
pub enum MemeSorting {
@ -191,16 +50,3 @@ impl FromStr for MemeSorting {
}
}
}
pub async fn assert_user_exists(http: &Client, user: &str) -> anyhow::Result<()> {
if !api::users(http).await?.iter().any(|u| u.name == user) {
bail!(consts::NO_SUCH_USER_ERROR);
}
Ok(())
}
pub async fn assert_category_exists(http: &Client, cat: &str) -> anyhow::Result<()> {
if !api::cats(http).await?.iter().any(|c| c.id == cat) {
bail!(consts::NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}