Compare commits

...

3 commits

Author SHA1 Message Date
LordMZTE 16a484243d add random function
Some checks are pending
troll/uff uff xD
2021-12-26 14:25:35 +01:00
LordMZTE 141f3b96d1 make versions less restrictive 2021-12-26 12:58:59 +01:00
LordMZTE 4f2449f2a1 change dayuploads to int & bump version 2021-12-26 12:55:17 +01:00
4 changed files with 47 additions and 16 deletions

View file

@ -1,19 +1,19 @@
[package]
name = "libjens"
version = "1.0.0"
version = "1.1.0"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
env_logger = "0.9.0"
lazy_static = "1.4.0"
log = "0.4.14"
once_cell = "1.9.0"
reqwest = "0.11.8"
serde = { version = "1.0.132", features = ["derive"] }
serde_json = "1.0.73"
thiserror = "1.0.30"
tokio = { version = "1.15.0", features = ["macros", "fs", "rt-multi-thread"] }
url = "2.2.2"
env_logger = "^0.9"
lazy_static = "^1.4"
log = "^0.4"
once_cell = "^1"
reqwest = "^0.11"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
thiserror = "^1.0"
tokio = { version = "^1.15", features = ["macros", "fs", "rt-multi-thread"] }
url = "^2.2"

12
rustfmt.toml Normal file
View file

@ -0,0 +1,12 @@
unstable_features = true
binop_separator = "Back"
format_code_in_doc_comments = true
format_macro_matchers = true
format_strings = true
imports_layout = "HorizontalVertical"
match_block_trailing_comma = true
merge_imports = true
normalize_comments = true
use_field_init_shorthand = true
use_try_shorthand = true
wrap_comments = true

View file

@ -1,6 +1,7 @@
use serde::{
de::{self, Visitor},
Deserialize, Deserializer,
Deserialize,
Deserializer,
};
use thiserror::Error;
@ -64,7 +65,7 @@ pub struct User {
pub id: Option<String>,
pub tokenhash: Option<String>,
pub userdir: Option<String>,
pub dayuploads: String,
pub dayuploads: u16,
}
impl User {
@ -111,6 +112,11 @@ where
deserializer.deserialize_any(Vis)
}
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct RandomResp {
pub meme: Meme,
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -1,4 +1,4 @@
use crate::api::{Category, CatsResp, Meme, MemesResp, User, UsersResp};
use crate::api::{Category, CatsResp, Meme, MemesResp, RandomResp, User, UsersResp};
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Url;
@ -22,7 +22,7 @@ macro_rules! init_cache {
None => {
let x = Arc::new($init_fn);
cell.get_or_init(|| x)
}
},
})
}};
}
@ -36,7 +36,8 @@ impl JMClient {
JMClientBuilder::default()
}
/// clears the cache. all requests will be made again after this has been called.
/// clears the cache. all requests will be made again after this has been
/// called.
pub async fn clear_cache(&mut self) {
self.cache.lock().await.clear();
}
@ -61,6 +62,7 @@ impl JMClient {
}))
}
/// Request the categories from the server. Result will be cached.
pub async fn get_cats(&self) -> Result<Arc<Vec<Category>>, JMClientError> {
self.get_api_json(
&self.cache.lock().await.cats,
@ -70,6 +72,7 @@ impl JMClient {
.await
}
/// Request the memes from the server. Result will be cached.
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
@ -77,12 +80,22 @@ impl JMClient {
.await
}
/// Request the users from the server. Result will be cached.
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
}
/// Requests a random meme from the server. Result is not cached!
pub async fn get_random(&self) -> Result<Meme, JMClientError> {
info!("Requesting random meme from server");
let res = self.http.get(self.endpoint.join("random")?).send().await?;
Ok(serde_json::from_slice::<RandomResp>(&res.bytes().await?)?.meme)
}
}
impl Default for JMClient {
fn default() -> Self {