add random function
Some checks are pending
troll/uff uff xD

This commit is contained in:
LordMZTE 2021-12-26 14:25:35 +01:00
parent 141f3b96d1
commit 16a484243d
3 changed files with 19 additions and 2 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "libjens"
version = "1.0.1"
version = "1.1.0"
authors = ["LordMZTE <lord@mzte.de>"]
edition = "2018"

View file

@ -112,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;
@ -62,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,
@ -71,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
@ -78,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 {